[Erp5-report] r30286 - /experimental/erp5.buildout/recipes/run_unit_test.py
nobody at svn.erp5.org
nobody at svn.erp5.org
Wed Nov 4 13:44:35 CET 2009
Author: leonardo
Date: Wed Nov 4 13:44:33 2009
New Revision: 30286
URL: http://svn.erp5.org?rev=30286&view=rev
Log:
Make run_unit_test recipe compatible with Zope 2.12, make it more flexible regarding location if instance_home and software_home, and delegate more of the functionality to zc.recipe.egg
Modified:
experimental/erp5.buildout/recipes/run_unit_test.py
Modified: experimental/erp5.buildout/recipes/run_unit_test.py
URL: http://svn.erp5.org/experimental/erp5.buildout/recipes/run_unit_test.py?rev=30286&r1=30285&r2=30286&view=diff
==============================================================================
--- experimental/erp5.buildout/recipes/run_unit_test.py [utf8] (original)
+++ experimental/erp5.buildout/recipes/run_unit_test.py [utf8] Wed Nov 4 13:44:33 2009
@@ -23,80 +23,133 @@
name = 'runUnitTest'
def __init__(self, buildout, name, options):
- self.egg = zc.recipe.egg.Egg(buildout, 'recipes', options)
self.buildout, self.options, self.name = buildout, options, name
-
- options['location'] = os.path.join(
+
+ self.location = options['location'] = os.path.join(
buildout['buildout']['parts-directory'],
self.name,
)
- options['bin-directory'] = buildout['buildout']['bin-directory']
- options['scripts'] = '' # suppress script generation. ??? XXX
+ try:
+ self.instance_home = (options.get('instance-home') or
+ # backward compatibility...
+ self.buildout['erp5_instance']['location'])
+ except KeyError:
+ raise RuntimeError('please specify location of ERP5 instance '
+ '(e.g.: "instance-home = '
+ '${erp5_instance:location}")')
+ # force the generation of a single script...
+ scriptName = options.get('control-script', name)
+ options['scripts'] = scriptName + '=runUnitTest'
+ # ...manually defined by our entry point
+ options['entry-points'] = '''
+ runUnitTest=Products.ERP5Type.tests.runUnitTest:main
+ '''
+ # initialize the recipe that will actually generate the script
+ self.egg = zc.recipe.egg.Egg(buildout, 'recipes', options)
def install(self):
options = self.options
- location = options['location']
+ location = self.location
- extra_paths = []
- zope2_location = options['zope2-location']
- software_home = os.path.join(zope2_location, 'lib', 'python')
- extra_paths.append(software_home)
+ extra_paths = self.egg.extra_paths
+ # insert software-home into extra_paths, calculating from
+ # zope2-location if necessary
+ software_home = options.get('software-home')
+ zope2_location = options.get('zope2-location')
+ if zope2_location and not software_home:
+ software_home = os.path.join(zope2_location, 'lib', 'python')
+ if software_home:
+ extra_paths.insert(0, software_home)
+ zope_scripts = None
+ else:
+ # if no software_home can be found, assume software_home is already
+ # reachable by eggs or extra-paths (the generated script will check
+ # if that is the case), but then the skeletons on
+ # ERP5Type/tests/skel2.12 will need to find the Zope startup
+ # scripts (zopectl, runzope), which will likely be in this buildout
+ # bin directory.
+ zope_scripts = options.get('zope2-scripts',
+ options['bin-directory'])
- extra_paths.extend(options.get('extra-paths', []))
+ init = init_template % dict(name=self.name,
+ zope_scripts=zope_scripts)
- init = init_template % dict(software_home=software_home)
- for product in options.get('products', '').splitlines():
+ products = options.get('products', '')
+ test_products = options.get('test-products', products)
+ # Add Products to sys.path under the Products namespace
+ for product in products.splitlines():
if product:
init += add_product_path_template % dict(product_path=product)
- for product_test_path in glob.glob(
- os.path.join(product, '*', 'tests')):
- init += add_sys_path_template % dict(
- sys_path=product_test_path)
-
+ # add test directories of selected products (defaulting to all
+ # products) to sys.path since that's where runUnitTest actually looks
+ # for them
+ for product in test_products.splitlines():
+ if product:
+ extra_paths.extend(
+ product_test_path for product_test_path in
+ glob.glob(os.path.join(product, '*', 'tests'))
+ )
+ #for product_test_path in glob.glob(
+ # os.path.join(product, '*', 'tests')):
+ # init += add_sys_path_template % dict(
+ # sys_path=product_test_path)
+
for bt5_path in options.get('bt5_path', '').splitlines():
+ bt5_path = bt5_path.strip()
if bt5_path:
init += add_bt5_path_template % dict(bt5_path=bt5_path)
- # add parts/erp5_instance/tests to path because runUnitTest will
+ # add parts/<instance>/tests to path because runUnitTest will
# guess it wrong
- instance_path = self.buildout['erp5_instance']['location']
- local_tests_path = os.path.join(instance_path, 'tests')
+ local_tests_path = os.path.join(self.instance_home, 'tests')
init += add_sys_path_template % dict(sys_path=local_tests_path)
- parts_directory = self.buildout['buildout']['parts-directory']
+ # runUnitTest works in our parts directory, which will contain
+ # custom_zodb.py
+ if not os.path.exists(location):
+ os.mkdir(location)
+ init += set_env_and_chdir_template % dict(location=location)
+
# framework.py requires a Products directory, so create it here
- products_dir = os.path.join(parts_directory, 'Products')
+ # This is temporary, as we're getting rid of "framework.py"
+ products_dir = os.path.join(location, 'Products')
if not os.path.exists(products_dir):
os.mkdir(products_dir)
- # runUnitTest works in unit_test directory, which will contain
- # custom_zodb.py
- working_dir = os.path.join(parts_directory, 'unit_test')
- if not os.path.exists(working_dir):
- os.mkdir(working_dir)
- init += set_env_and_chdir_template % dict(
- parts_unit_test=working_dir)
+ self.options['initialization'] = init
+ # reassign just to be on the safe side...
+ self.egg.extra_paths = extra_paths
- requirements, ws = self.egg.working_set(('setuptools',))
+ # generate the script according to our configuration
+ self.egg.install()
- zc.buildout.easy_install.scripts(
- [(self.options.get('control-script', self.name),
- 'Products.ERP5Type.tests.runUnitTest', 'main')],
- ws, options['executable'], options['bin-directory'],
- extra_paths = extra_paths,
- initialization = init,
- )
-
- return [working_dir]
+ return [location]
update = install
init_template = '''
import os
-os.environ['SOFTWARE_HOME'] = '%(software_home)s'
+from os.path import dirname, abspath
+
+# derive SOFTWARE_HOME from import location of Zope2
+try:
+ import Zope2
+except ImportError:
+ print >> sys.stderr, ("Zope2 could not be found. In the %(name)r part of the "
+ "buildout configuration, please specify either "
+ "software-home or zope2-location, or add Zope2 to the "
+ "list of eggs.")
+ raise
+else:
+ os.environ['SOFTWARE_HOME'] = abspath(dirname(dirname(Zope2.__file__)))
+
+# if under Zope 2.12, we need the location of the Zope2 startup scripts for the
+# substitutions in the skel2.12 directory
+ZOPE_SCRIPTS = %(zope_scripts)r
+if ZOPE_SCRIPTS is not None:
+ os.environ['ZOPE_SCRIPTS'] = ZOPE_SCRIPTS
import Products
-import sys
# Testing needs to be imported, otherwise framework.py will guess it from
# sys.path, which can be a zipped egg
@@ -104,19 +157,19 @@
'''
add_product_path_template = '''
-Products.__path__.append('%(product_path)s')'''
+Products.__path__.append(%(product_path)r)'''
add_sys_path_template = '''
-sys.path.append('%(sys_path)s')'''
+sys.path.append(%(sys_path)r)'''
add_bt5_path_template = '''
-sys.argv.insert(1, '%(bt5_path)s')
+sys.argv.insert(1, %(bt5_path)r)
sys.argv.insert(1, '--bt5_path')'''
set_env_and_chdir_template = '''
# execute framework.py, like in zope testcase
-os.environ['INSTANCE_HOME'] = '%(parts_unit_test)s'
-os.chdir("%(parts_unit_test)s")
+os.environ['INSTANCE_HOME'] = %(location)r
+os.chdir(%(location)r)
'''
More information about the Erp5-report
mailing list