Permalink
Browse files

Make 'zcml=' work, part I

  • Loading branch information...
1 parent 27ef6cd commit 41b79332d1cf4dd11f0fd6ff1856d30f3fd78c70 @aclark4life aclark4life committed Aug 18, 2011
Showing with 101 additions and 1 deletion.
  1. +101 −1 mr/migrator/recipe/__init__.py
@@ -2,7 +2,11 @@
"""Recipe mr.migrator"""
from zc.recipe.egg.egg import Scripts
+
import logging
+import os
+import re
+import shutil
logging.basicConfig(level=logging.DEBUG)
@@ -33,7 +37,22 @@ def __init__(self, buildout, name, options):
self.options['arguments'] = str(args) + ',"' + pipeline + '"'
else:
self.options['arguments'] = str(args)
- return Scripts.__init__(self, buildout, name, options)
+
+ # Process zcml
+ partsdir = self.buildout['buildout']['parts-directory']
+ newdir = partsdir + '/migrate'
+ if not os.path.exists(newdir):
+ os.mkdir(newdir)
+
+ newdir = partsdir + '/migrate/etc'
+ if not os.path.exists(newdir):
+ os.mkdir(newdir)
+
+ self.options['location'] = self.buildout['buildout']['parts-directory'] + '/migrate'
+ if 'zcml' in options:
+ self.build_package_includes()
+
+ return Scripts.__init__(self, buildout, name, options)
def install(self):
"""Installer"""
@@ -45,3 +64,84 @@ def install(self):
def update(self):
"""Updater"""
return Scripts.update(self)
+
+ # Borrowed from p.r.zope2instance
+ def build_package_includes(self):
+ """Create ZCML slugs in etc/package-includes
+ """
+ location = self.options['location']
+ sitezcml_path = os.path.join(location, 'etc', 'site.zcml')
+ zcml = self.options.get('zcml')
+ site_zcml = self.options.get('site-zcml')
+ additional_zcml = self.options.get("zcml-additional")
+ resources = self.options.get("resources")
+
+ if site_zcml:
+ open(sitezcml_path, 'w').write(site_zcml)
+ return
+
+ if zcml:
+ zcml = zcml.split()
+
+ if additional_zcml or resources or zcml:
+ includes_path = os.path.join(location, 'etc', 'package-includes')
+
+ if not os.path.exists(includes_path):
+ # Zope 2.9 does not have a package-includes so we
+ # create one.
+ os.mkdir(includes_path)
+ else:
+ if zcml and '*' in zcml:
+ zcml.remove('*')
+ else:
+ shutil.rmtree(includes_path)
+ os.mkdir(includes_path)
+
+ if additional_zcml:
+ path=os.path.join(includes_path, "999-additional-overrides.zcml")
+ open(path, "w").write(additional_zcml.strip())
+
+ if resources:
+ resources_path = resources.strip()
+ path=os.path.join(includes_path, "998-resources-configure.zcml")
+ open(path, "w").write(
+ resources_zcml % dict(directory=resources_path)
+ )
+
+ if not os.path.exists(resources_path):
+ os.mkdir(resources_path)
+
+ if zcml:
+ n = 0
+ package_match = re.compile('\w+([.]\w+)*$').match
+ for package in zcml:
+ n += 1
+ orig = package
+ if ':' in package:
+ package, filename = package.split(':')
+ else:
+ filename = None
+
+ if '-' in package:
+ package, suff = package.split('-')
+ file_suff = suff
+ if suff not in ('configure', 'meta', 'overrides'):
+ file_suff = '%s-configure' % suff
+ else:
+ suff = file_suff = 'configure'
+
+ if filename is None:
+ filename = suff + '.zcml'
+
+ if not package_match(package):
+ raise ValueError('Invalid zcml', orig)
+
+ path = os.path.join(
+ includes_path,
+ "%3.3d-%s-%s.zcml" % (n, package, file_suff),
+ )
+ open(path, 'w').write(
+ '<include package="%s" file="%s" />\n'
+ % (package, filename)
+ )
+

0 comments on commit 41b7933

Please sign in to comment.