From 8529fae3bd64f302ccc1b1a4d00137fa8d3a8ffe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C5=81ukasz=20Nowak?= <luke@nexedi.com>
Date: Tue, 2 Mar 2010 09:47:19 +0000
Subject: [PATCH]  - do not depend anymore on erp5.recipe.createsite

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@33257 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 .../erp5.recipe.standaloneinstance/setup.py   |   1 -
 .../recipe/standaloneinstance/__init__.py     |  40 ++++++-
 .../create_erp5_instance.py                   | 112 ++++++++++++++++++
 3 files changed, 148 insertions(+), 5 deletions(-)
 create mode 100644 buildout/local-eggs/erp5.recipe.standaloneinstance/src/erp5/recipe/standaloneinstance/create_erp5_instance.py

diff --git a/buildout/local-eggs/erp5.recipe.standaloneinstance/setup.py b/buildout/local-eggs/erp5.recipe.standaloneinstance/setup.py
index e246e3a461..5875236c10 100644
--- a/buildout/local-eggs/erp5.recipe.standaloneinstance/setup.py
+++ b/buildout/local-eggs/erp5.recipe.standaloneinstance/setup.py
@@ -32,7 +32,6 @@ setup(
     namespace_packages = ['erp5', 'erp5.recipe'],
     install_requires = [
         'plone.recipe.zope2instance',
-        'erp5.recipe.createsite',
     ],
     zip_safe=False,
     entry_points = {'zc.buildout': ['default = %s:Recipe' % name]},
diff --git a/buildout/local-eggs/erp5.recipe.standaloneinstance/src/erp5/recipe/standaloneinstance/__init__.py b/buildout/local-eggs/erp5.recipe.standaloneinstance/src/erp5/recipe/standaloneinstance/__init__.py
index 549225409c..f6805e8bca 100644
--- a/buildout/local-eggs/erp5.recipe.standaloneinstance/src/erp5/recipe/standaloneinstance/__init__.py
+++ b/buildout/local-eggs/erp5.recipe.standaloneinstance/src/erp5/recipe/standaloneinstance/__init__.py
@@ -17,12 +17,11 @@ import os, sys
 from string import Template
 import zc.buildout
 import plone.recipe.zope2instance
-import erp5.recipe.createsite
 
 class WithMinusTemplate(Template):
   idpattern = '[_a-z][-_a-z0-9]*'
 
-class Recipe(plone.recipe.zope2instance.Recipe, erp5.recipe.createsite.Recipe):
+class Recipe(plone.recipe.zope2instance.Recipe):
   def __init__(self, buildout, name, options):
     standalone_location = options.get('instancehome')
     if not standalone_location:
@@ -162,11 +161,42 @@ class Recipe(plone.recipe.zope2instance.Recipe, erp5.recipe.createsite.Recipe):
 
     if not os.path.exists(options['zodb-path'].strip()) or \
         force_zodb_update:
-      # initialise ERP5 part only in case if ZODB not exist yet or it is forced
-      erp5.recipe.createsite.Recipe.install(self)
+      self.update_zodb()
     # we return nothing, as this is totally standalone installation
     return []
 
+  def update_zodb(self):
+    options = self.options
+    zopectl_path = os.path.join(options['bin-directory'],
+                  options['control-script'])
+    script_name = os.path.join(os.path.dirname(__file__),
+                 'create_erp5_instance.py')
+    argv = [zopectl_path, 'run', script_name]
+
+    if options.get('portal_id'):
+      argv.extend(['--portal_id', options['portal_id']])
+    if options.get('erp5_sql_connection_string'):
+      argv.extend(['--erp5_sql_connection_string',
+            options['erp5_sql_connection_string']])
+
+    if options.get('cmf_activity_sql_connection_string'):
+      argv.extend(['--cmf_activity_sql_connection_string',
+         options['cmf_activity_sql_connection_string']])
+    if options.get('erp5_catalog_storage'):
+      argv.extend(['--erp5_catalog_storage',
+            options['erp5_catalog_storage']])
+    if options.get('user'):
+      # XXX read rom zope2instance section ?
+      argv.extend(['--initial-user',
+            options['user']])
+
+    argv.extend(['--bt5-path',
+          os.path.join(options['bt5-path'])])
+    argv.extend([bt for bt in options.get('bt5', '').split('\n') if bt])
+
+    assert os.spawnl(
+       os.P_WAIT, zopectl_path, *argv ) == 0
+
   def build_zope_conf(self):
     options = self.options
     location = options['instancehome']
@@ -189,4 +219,6 @@ class Recipe(plone.recipe.zope2instance.Recipe, erp5.recipe.createsite.Recipe):
     file(zope_conf_path, 'w').write(result)
 
   def update(self):
+#    if self.options.get('force-zodb-update','false').strip().lower() == 'true':
+#      return self.install()
     return plone.recipe.zope2instance.Recipe.update(self)
diff --git a/buildout/local-eggs/erp5.recipe.standaloneinstance/src/erp5/recipe/standaloneinstance/create_erp5_instance.py b/buildout/local-eggs/erp5.recipe.standaloneinstance/src/erp5/recipe/standaloneinstance/create_erp5_instance.py
new file mode 100644
index 0000000000..f2beb04efb
--- /dev/null
+++ b/buildout/local-eggs/erp5.recipe.standaloneinstance/src/erp5/recipe/standaloneinstance/create_erp5_instance.py
@@ -0,0 +1,112 @@
+# Create an ERP5 instance 
+# usage: zopectl run create_erp5_instance [options] [business templates]
+
+import os
+from optparse import OptionParser
+from urllib import unquote
+
+from Testing.makerequest import makerequest
+from AccessControl.SecurityManagement import newSecurityManager
+
+parser = OptionParser()
+parser.add_option("-p", "--portal_id", dest="portal_id",
+                  help="The ID of the Portal", default="erp5")
+parser.add_option("--erp5_sql_connection_string",
+                  dest="erp5_sql_connection_string",
+                  help="Connection String used for ZSQLCatalog "
+                       "(use %20 for space)",
+                  default="test test")
+parser.add_option("--cmf_activity_sql_connection_string",
+                  dest="cmf_activity_sql_connection_string",
+                  help="Connection String used for CMFActivity")
+parser.add_option("--erp5_catalog_storage",
+                  dest="erp5_catalog_storage",
+                  help="Business Template for Catalog Storage")
+parser.add_option("-u", "--initial-user",
+                  dest="user_and_pass",
+                  help="User and Password, separated by :",
+                  default="zope:zope")
+parser.add_option("--bt5-path",
+                  dest="bt5_path",
+                  help="Path to folder containing business templates",
+                  default="bt5")
+
+(options, args) = parser.parse_args()
+
+# cmf activity connection string defaults to zsqlcatalog's one
+if not options.cmf_activity_sql_connection_string:
+  options.cmf_activity_sql_connection_string = \
+            options.erp5_sql_connection_string
+
+# connection strings have to contain a space, for conveniance, this space can
+# be replaced by %20 character.
+options.erp5_sql_connection_string =\
+      unquote(options.erp5_sql_connection_string)
+options.cmf_activity_sql_connection_string =\
+      unquote(options.cmf_activity_sql_connection_string)
+username, password = options.user_and_pass.split(':')
+
+try:
+  import transaction
+except ImportError:
+  class Transaction:
+    def commit(self):
+      return get_transaction().commit()
+  transaction = Transaction()
+
+app = makerequest(app)
+
+user = app.acl_users.getUserById(username)
+if user is None:
+  uf = app.acl_users
+  uf._doAddUser(username, password, ['Manager', 'Member', 'Assignee',
+                                     'Assignor', 'Author'], [])
+  user = uf.getUserById(username)
+
+newSecurityManager(None, user.__of__(app.acl_users))
+
+portal = getattr(app, options.portal_id, None)
+if portal is None:
+  print 'Adding ERP5 site %s' % options.portal_id
+  app.manage_addProduct['ERP5'].manage_addERP5Site(
+              id=options.portal_id,
+              erp5_sql_connection_string=options.erp5_sql_connection_string,
+              cmf_activity_sql_connection_string=\
+                        options.cmf_activity_sql_connection_string,
+              erp5_catalog_storage='erp5_mysql_innodb_catalog')
+
+  transaction.commit()
+  portal = app._getOb(options.portal_id)
+
+# set preference for erp5_subversion
+from App.config import getConfiguration
+default_site_preference = portal.portal_preferences.default_site_preference
+instance_home = getConfiguration().instancehome
+default_site_preference.edit(
+  preferred_subversion_working_copy_list=['%s/bt5/' % instance_home])
+if default_site_preference.getPreferenceState() == 'disabled':
+  default_site_preference.enable()
+
+# install our business templates
+bt5_list = []
+for arg in args:
+  bt_path = os.path.join(options.bt5_path, arg)
+  installed_bt = portal.portal_templates.getInstalledBusinessTemplate(arg)
+  if installed_bt is not None:
+    # XXX this way works only for extracted business template, not for
+    # *.bt5 packed business template.
+    version = file('%s/bt/version' % bt_path).read().strip()
+    revision = file('%s/bt/revision' % bt_path).read().strip()
+    if version == installed_bt.getVersion() and \
+       revision == installed_bt.getRevision():
+      print 'Skipping bt %s' % bt_path
+      continue
+    else:
+      print 'Updating bt %s' % bt_path
+  else:
+    print 'Installing bt %s' % bt_path
+  bt = portal.portal_templates.download(bt_path)
+  bt.install(force=True)
+  transaction.commit()
+
+transaction.commit()
-- 
2.30.9