[Checkins] SVN: zc.recipe.deployment/trunk/ Added a configuration recipe for generating configuration files.

Jim Fulton jim at zope.com
Wed Feb 14 16:32:52 EST 2007


Log message for revision 72585:
  Added a configuration recipe for generating configuration files.
  

Changed:
  U   zc.recipe.deployment/trunk/README.txt
  U   zc.recipe.deployment/trunk/setup.py
  U   zc.recipe.deployment/trunk/src/zc/recipe/deployment/README.txt
  U   zc.recipe.deployment/trunk/src/zc/recipe/deployment/__init__.py

-=-
Modified: zc.recipe.deployment/trunk/README.txt
===================================================================
--- zc.recipe.deployment/trunk/README.txt	2007-02-14 21:32:27 UTC (rev 72584)
+++ zc.recipe.deployment/trunk/README.txt	2007-02-14 21:32:51 UTC (rev 72585)
@@ -40,6 +40,14 @@
 Changes
 *******
 
+0.3 (Feb 14, 2007)
+==================
+
+Features Added
+--------------
+
+- Added a configuration recipe for creating configuration files.
+
 0.2.1 (Feb 13, 2007)
 ====================
 

Modified: zc.recipe.deployment/trunk/setup.py
===================================================================
--- zc.recipe.deployment/trunk/setup.py	2007-02-14 21:32:27 UTC (rev 72584)
+++ zc.recipe.deployment/trunk/setup.py	2007-02-14 21:32:51 UTC (rev 72585)
@@ -1,15 +1,16 @@
 import os
-
 from setuptools import setup, find_packages
 
 name = 'zc.recipe.deployment'
 
 entry_points = '''
 [zc.buildout]
-default=%(name)s:Install
+default = %(name)s:Install
+deployment = %(name)s:Install
+configuration = %(name)s:Configuration
 
 [zc.buildout.uninstall]
-default=%(name)s:uninstall
+default = %(name)s:uninstall
 
 ''' % globals()
 
@@ -18,7 +19,7 @@
 
 setup(
     name = name,
-    version = '0.2.1',
+    version = '0.3',
     author = 'Jim Fulton',
     author_email = 'jim at zope.com',
     description = 'ZC Buildout recipe for Unix deployments',

Modified: zc.recipe.deployment/trunk/src/zc/recipe/deployment/README.txt
===================================================================
--- zc.recipe.deployment/trunk/src/zc/recipe/deployment/README.txt	2007-02-14 21:32:27 UTC (rev 72584)
+++ zc.recipe.deployment/trunk/src/zc/recipe/deployment/README.txt	2007-02-14 21:32:51 UTC (rev 72585)
@@ -140,3 +140,133 @@
     False
     >>> os.path.exists('/var/run/foo')
     False
+
+Configuration files
+===================
+
+Normally, configuration files are created by specialized recipes.
+Sometimes, it's useful to specifu configuration files in a buildout
+cnfiguration file.  The zc.recipe.deployment:configuration recipe can be
+used to do that.
+
+Let's add a configuration file to our buildout:
+
+    >>> write('buildout.cfg',
+    ... '''
+    ... [buildout]
+    ... parts = foo x.cfg
+    ...
+    ... [foo]
+    ... recipe = zc.recipe.deployment
+    ... user = jim
+    ...
+    ... [x.cfg]
+    ... recipe = zc.recipe.deployment:configuration
+    ... text = xxx
+    ...        yyy
+    ...        zzz
+    ... ''')
+
+    >>> print system(join('bin', 'buildout')),
+    buildout: Installing foo
+    zc.recipe.deployment: 
+        Creating '/etc/foo',
+        mode 755, user 'root', group 'root'
+    zc.recipe.deployment: 
+        Creating '/var/log/foo',
+        mode 755, user 'jim', group 'jim'
+    zc.recipe.deployment: 
+        Creating '/var/run/foo',
+        mode 750, user 'jim', group 'jim'
+    buildout: Installing x.cfg
+
+By default, the configuration is installed as a part:
+
+    >>> cat('parts', 'x.cfg')
+    xxx
+    yyy
+    zzz
+
+If a deployment is specified, then the file is placed in the
+deployment etc directory:
+
+    >>> write('buildout.cfg',
+    ... '''
+    ... [buildout]
+    ... parts = foo x.cfg
+    ...
+    ... [foo]
+    ... recipe = zc.recipe.deployment
+    ... user = jim
+    ...
+    ... [x.cfg]
+    ... recipe = zc.recipe.deployment:configuration
+    ... text = xxx
+    ...        yyy
+    ...        zzz
+    ... deployment = foo
+    ... ''')
+
+    >>> print system(join('bin', 'buildout')),
+    buildout: Uninstalling x.cfg
+    buildout: Updating foo
+    buildout: Installing x.cfg
+
+    >>> os.path.exists(join('parts', 'x.cfg'))
+    False
+
+    >>> cat('/etc/foo/x.cfg')
+    xxx
+    yyy
+    zzz
+
+We can read data from a fiile rather than specifying in the
+configuration:
+
+    >>> write('x.in', '1\n2\n3\n')
+
+    >>> write('buildout.cfg',
+    ... '''
+    ... [buildout]
+    ... parts = foo x.cfg
+    ...
+    ... [foo]
+    ... recipe = zc.recipe.deployment
+    ... user = jim
+    ...
+    ... [x.cfg]
+    ... recipe = zc.recipe.deployment:configuration
+    ... file = x.in
+    ... deployment = foo
+    ... ''')
+
+    >>> print system(join('bin', 'buildout')),
+    buildout: Uninstalling x.cfg
+    buildout: Updating foo
+    buildout: Installing x.cfg
+
+    >>> cat('/etc/foo/x.cfg')
+    1
+    2
+    3
+
+The recipe sets a location option that can be used by other recipes:
+
+    >>> cat('.installed.cfg') # doctest: +ELLIPSIS
+    [buildout]
+    ...
+    [x.cfg]
+    ...
+    location = /etc/foo/x.cfg
+    ...
+
+
+.. cleanup
+
+    >>> print system(join('bin', 'buildout')+' buildout:parts='),
+    buildout: Uninstalling x.cfg
+    buildout: Uninstalling foo
+    buildout: Running uninstall recipe
+    zc.recipe.deployment: Removing '/etc/foo'
+    zc.recipe.deployment: Removing '/var/log/foo'.
+    zc.recipe.deployment: Removing '/var/run/foo'.

Modified: zc.recipe.deployment/trunk/src/zc/recipe/deployment/__init__.py
===================================================================
--- zc.recipe.deployment/trunk/src/zc/recipe/deployment/__init__.py	2007-02-14 21:32:27 UTC (rev 72584)
+++ zc.recipe.deployment/trunk/src/zc/recipe/deployment/__init__.py	2007-02-14 21:32:51 UTC (rev 72585)
@@ -17,6 +17,7 @@
 """
 
 import grp, logging, os, pwd, shutil
+import zc.buildout
 
 logger = logging.getLogger('zc.recipe.deployment')
 
@@ -45,14 +46,14 @@
             make_dir(options['etc-directory'],   0,   0, 0755, created)
             make_dir(options['log-directory'], uid, gid, 0755, created)
             make_dir(options['run-directory'], uid, gid, 0750, created)
-        except Exception, e:
+        except Exception:
             for d in created:
                 try:
                     shutil.rmtree(d)
                 except OSError:
                     # parent directory may have already been removed
                     pass
-            raise e
+            raise
 
         return ()
 
@@ -86,3 +87,33 @@
                     name, mode, uname, gname)
 
     os.chown(name, uid, gid)
+
+class Configuration:
+
+    def __init__(self, buildout, name, options):
+        self.name, self.options = name, options
+
+        deployment = options.get('deployment')
+        if deployment:
+            options['location'] = os.path.join(
+                buildout[deployment]['etc-directory'],
+                name)
+        else:
+            options['location'] = os.path.join(
+                buildout['buildout']['parts-directory'],
+                name)
+
+    def install(self):
+        options = self.options
+        mode = options.get('mode', '')
+        if 'file' in options:
+            if 'text' in options:
+                raise zc.buildout.UserError(
+                    "Cannot specify both file and text options")
+            text = open(options['file'], 'r'+mode).read()
+        else:
+            text = options['text']
+        open(options['location'], 'w'+mode).write(text)
+        return options['location']
+
+    update = install



More information about the Checkins mailing list