[Checkins] SVN: zc.buildout/branches/help-api/zc.recipe.egg_/src/zc/recipe/egg/custom. - fixed typo in environment

Godefroid Chapelle gotcha at bubblenet.be
Sun Mar 29 17:17:09 EDT 2009


Log message for revision 98468:
  - fixed typo in environment
  
  - allowed to interpolate variables with the environment
  
  - make sure the environment is cleaned

Changed:
  U   zc.buildout/branches/help-api/zc.recipe.egg_/src/zc/recipe/egg/custom.py
  U   zc.buildout/branches/help-api/zc.recipe.egg_/src/zc/recipe/egg/custom.txt

-=-
Modified: zc.buildout/branches/help-api/zc.recipe.egg_/src/zc/recipe/egg/custom.py
===================================================================
--- zc.buildout/branches/help-api/zc.recipe.egg_/src/zc/recipe/egg/custom.py	2009-03-29 21:17:02 UTC (rev 98467)
+++ zc.buildout/branches/help-api/zc.recipe.egg_/src/zc/recipe/egg/custom.py	2009-03-29 21:17:09 UTC (rev 98468)
@@ -57,10 +57,14 @@
             options['index'] = index
         self.index = index
 
-        environment_section = options.get('envirionment')
+        environment_section = options.get('environment')
         if environment_section:
-            for key, value in buildout[environment_section].items():
-                os.environ[key] = value
+            self.environment = buildout[environment_section]
+        else:
+            self.environment = {}
+        environment_data = self.environment.items()
+        environment_data.sort()
+        options['_environment-data'] = repr(environment_data)
 
         options['_e'] = buildout['buildout']['eggs-directory']
 
@@ -84,13 +88,38 @@
 
         distribution = options.get('egg', options.get('eggs', self.name)
                                    ).strip()
-        return zc.buildout.easy_install.build(
-            distribution, options['_d'], self.build_ext,
-            self.links, self.index, options['executable'], [options['_e']],
-            newest=self.newest,
-            )
+        self._set_environment()
+        try:
+            return zc.buildout.easy_install.build(
+                distribution, options['_d'], self.build_ext,
+                self.links, self.index, options['executable'], [options['_e']],
+                newest=self.newest,
+                )
+        finally:
+            self._restore_environment()
 
 
+    def _set_environment(self):
+        self._saved_environment = {}
+        for key, value in self.environment.items():
+            if key in os.environ:
+                self._saved_environment[key] = os.environ[key]
+            # Interpolate value with variables from environment. Maybe there
+            # should be a general way of doing this in buildout with something
+            # like ${environ:foo}:
+            os.environ[key] = value % os.environ
+
+    def _restore_environment(self):
+        for key in self.environment:
+            if key in self._saved_environment:
+                os.environ[key] = self._saved_environment[key]
+            else:
+                try:
+                    del os.environ[key]
+                except KeyError:
+                    pass
+
+
 class Develop(Base):
 
     def __init__(self, buildout, name, options):

Modified: zc.buildout/branches/help-api/zc.recipe.egg_/src/zc/recipe/egg/custom.txt
===================================================================
--- zc.buildout/branches/help-api/zc.recipe.egg_/src/zc/recipe/egg/custom.txt	2009-03-29 21:17:02 UTC (rev 98467)
+++ zc.buildout/branches/help-api/zc.recipe.egg_/src/zc/recipe/egg/custom.txt	2009-03-29 21:17:09 UTC (rev 98468)
@@ -84,7 +84,7 @@
 
 environment
    The name of a section with additional environment variables. The
-   envirionment variables are set before the egg is built.
+   environment variables are set before the egg is built.
 
 To illustrate this, we'll define a buildout that builds an egg for a
 package that has a simple extension module::
@@ -129,29 +129,21 @@
     ... [buildout]
     ... parts = extdemo
     ...
-    ... [extdemo-env]
-    ... test-variable = foo
-    ...
     ... [extdemo]
     ... recipe = zc.recipe.egg:custom
     ... find-links = %(server)s
     ... index = %(server)s/index
     ... include-dirs = include
-    ... envirionment = extdemo-env
     ...
     ... """ % dict(server=link_server))
 
     >>> print system(buildout),
     Installing extdemo.
-    Have environment test-variable: foo
     zip_safe flag not set; analyzing archive contents...
 
 We got the zip_safe warning because the source distribution we used
 wasn't setuptools based and thus didn't set the option.
 
-The setup.py also printed out that we have set the environment `test-variable`
-to foo.
-
 The egg is created in the develop-eggs directory *not* the eggs
 directory because it depends on buildout-specific parameters and the
 eggs directory can be shared across multiple buildouts.
@@ -188,15 +180,11 @@
     ... develop = demo
     ... parts = extdemo demo
     ...
-    ... [extdemo-env]
-    ... test-variable = foo
-    ...
     ... [extdemo]
     ... recipe = zc.recipe.egg:custom
     ... find-links = %(server)s
     ... index = %(server)s/index
     ... include-dirs = include
-    ... envirionment = extdemo-env
     ...
     ... [demo]
     ... recipe = zc.recipe.egg
@@ -252,7 +240,6 @@
     >>> print system(buildout),
     Develop: '/sample-buildout/demo'
     Updating extdemo.
-    Have environment test-variable: foo
     zip_safe flag not set; analyzing archive contents...
     Updating demo.
     Generated script '/sample-buildout/bin/demo'.
@@ -302,6 +289,161 @@
     d  extdemo-1.4-py2.4-linux-i686.egg
     -  zc.recipe.egg.egg-link
 
+
+Controlling environment variables
++++++++++++++++++++++++++++++++++
+
+To set additional environment variables, the `environment` option is used.
+
+Let's create a recipe which prints out environment variables. We need this to
+make sure the set envirionment variables are removed after the egg:custom
+recipe was run.
+
+    >>> mkdir(sample_buildout, 'recipes')
+    >>> write(sample_buildout, 'recipes', 'environ.py',
+    ... """
+    ... import logging, os, zc.buildout
+    ...
+    ... class Environ:
+    ...
+    ...     def __init__(self, buildout, name, options):
+    ...         self.name = name
+    ...
+    ...     def install(self):
+    ...         logging.getLogger(self.name).info(
+    ...             'test-variable left over: %s' % (
+    ...                 'test-variable' in os.environ))
+    ...         return []
+    ...
+    ...     def update(self):
+    ...         self.install()
+    ... """)
+    >>> write(sample_buildout, 'recipes', 'setup.py',
+    ... """
+    ... from setuptools import setup
+    ...
+    ... setup(
+    ...     name = "recipes",
+    ...     entry_points = {'zc.buildout': ['environ = environ:Environ']},
+    ...     )
+    ... """)
+
+
+Create our buildout:
+
+    >>> write(sample_buildout, 'buildout.cfg',
+    ... """
+    ... [buildout]
+    ... develop = recipes
+    ... parts = extdemo checkenv
+    ...
+    ... [extdemo-env]
+    ... test-variable = foo
+    ...
+    ... [extdemo]
+    ... recipe = zc.recipe.egg:custom
+    ... find-links = %(server)s
+    ... index = %(server)s/index
+    ... include-dirs = include
+    ... environment = extdemo-env
+    ...
+    ... [checkenv]
+    ... recipe = recipes:environ
+    ...
+    ... """ % dict(server=link_server))
+    >>> print system(buildout),
+    Develop: '/sample-buildout/recipes'
+    Uninstalling demo.
+    Uninstalling extdemo.
+    Installing extdemo.
+    Have environment test-variable: foo
+    zip_safe flag not set; analyzing archive contents...
+    Installing checkenv.
+    checkenv: test-variable left over: False
+
+
+The setup.py also printed out that we have set the environment `test-variable`
+to foo. After the buildout the variable is reset to its original value (i.e.
+removed).
+
+When an environment variable has a value before zc.recipe.egg:custom is run,
+the original value will be restored:
+
+    >>> import os
+    >>> os.environ['test-variable'] = 'bar'
+    >>> print system(buildout),
+    Develop: '/sample-buildout/recipes'
+    Updating extdemo.
+    Updating checkenv.
+    checkenv: test-variable left over: True
+
+    >>> os.environ['test-variable']
+    'bar'
+
+
+Sometimes it is required to prepend or append to an existing environment
+variable, for instance for adding something to the PATH. Therefor all variables
+are interpolated with os.environ before the're set:
+
+    >>> write(sample_buildout, 'buildout.cfg',
+    ... """
+    ... [buildout]
+    ... develop = recipes
+    ... parts = extdemo checkenv
+    ...
+    ... [extdemo-env]
+    ... test-variable = foo:%%(test-variable)s
+    ...
+    ... [extdemo]
+    ... recipe = zc.recipe.egg:custom
+    ... find-links = %(server)s
+    ... index = %(server)s/index
+    ... include-dirs = include
+    ... environment = extdemo-env
+    ...
+    ... [checkenv]
+    ... recipe = recipes:environ
+    ...
+    ... """ % dict(server=link_server))
+    >>> print system(buildout),
+    Develop: '/sample-buildout/recipes'
+    Uninstalling extdemo.
+    Installing extdemo.
+    Have environment test-variable: foo:bar
+    zip_safe flag not set; analyzing archive contents...
+    Updating checkenv.
+    checkenv: test-variable left over: True
+
+    >>> os.environ['test-variable']
+    'bar'
+    >>> del os.environ['test-variable']
+
+
+Create a clean buildout.cfg w/o the checkenv recipe, and delete the recipe:
+
+    >>> write(sample_buildout, 'buildout.cfg',
+    ... """
+    ... [buildout]
+    ... develop = recipes
+    ... parts = extdemo
+    ...
+    ... [extdemo]
+    ... recipe = zc.recipe.egg:custom
+    ... find-links = %(server)s
+    ... index = %(server)s/index
+    ... include-dirs = include
+    ...
+    ... """ % dict(server=link_server))
+    >>> print system(buildout),
+    Develop: '/sample-buildout/recipes'
+    Uninstalling checkenv.
+    Uninstalling extdemo.
+    Installing extdemo.
+    zip_safe flag not set; analyzing archive contents...
+    
+    >>> rmdir(sample_buildout, 'recipes')
+
+
 Controlling develop-egg generation
 ==================================
 
@@ -401,7 +543,6 @@
 
     >>> print system(buildout),
     Develop: '/sample-buildout/demo'
-    Uninstalling demo.
     Uninstalling extdemo.
     Installing extdemo.
     Installing demo.



More information about the Checkins mailing list