Commit d2e46744 authored by Christian Zagrodnick's avatar Christian Zagrodnick

- fixed typo in environment

- allowed to interpolate variables with the environment

- make sure the environment is cleaned
parent 4b4ad2ac
...@@ -57,10 +57,14 @@ class Custom(Base): ...@@ -57,10 +57,14 @@ class Custom(Base):
options['index'] = index options['index'] = index
self.index = index self.index = index
environment_section = options.get('envirionment') environment_section = options.get('environment')
if environment_section: if environment_section:
for key, value in buildout[environment_section].items(): self.environment = buildout[environment_section]
os.environ[key] = value else:
self.environment = {}
environment_data = self.environment.items()
environment_data.sort()
options['_environment-data'] = repr(environment_data)
options['_e'] = buildout['buildout']['eggs-directory'] options['_e'] = buildout['buildout']['eggs-directory']
...@@ -84,11 +88,36 @@ class Custom(Base): ...@@ -84,11 +88,36 @@ class Custom(Base):
distribution = options.get('egg', options.get('eggs', self.name) distribution = options.get('egg', options.get('eggs', self.name)
).strip() ).strip()
self._set_environment()
try:
return zc.buildout.easy_install.build( return zc.buildout.easy_install.build(
distribution, options['_d'], self.build_ext, distribution, options['_d'], self.build_ext,
self.links, self.index, options['executable'], [options['_e']], self.links, self.index, options['executable'], [options['_e']],
newest=self.newest, 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): class Develop(Base):
......
...@@ -84,7 +84,7 @@ python ...@@ -84,7 +84,7 @@ python
environment environment
The name of a section with additional environment variables. The 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 To illustrate this, we'll define a buildout that builds an egg for a
package that has a simple extension module:: package that has a simple extension module::
...@@ -129,29 +129,21 @@ the egg: ...@@ -129,29 +129,21 @@ the egg:
... [buildout] ... [buildout]
... parts = extdemo ... parts = extdemo
... ...
... [extdemo-env]
... test-variable = foo
...
... [extdemo] ... [extdemo]
... recipe = zc.recipe.egg:custom ... recipe = zc.recipe.egg:custom
... find-links = %(server)s ... find-links = %(server)s
... index = %(server)s/index ... index = %(server)s/index
... include-dirs = include ... include-dirs = include
... envirionment = extdemo-env
... ...
... """ % dict(server=link_server)) ... """ % dict(server=link_server))
>>> print system(buildout), >>> print system(buildout),
Installing extdemo. Installing extdemo.
Have environment test-variable: foo
zip_safe flag not set; analyzing archive contents... zip_safe flag not set; analyzing archive contents...
We got the zip_safe warning because the source distribution we used We got the zip_safe warning because the source distribution we used
wasn't setuptools based and thus didn't set the option. 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 The egg is created in the develop-eggs directory *not* the eggs
directory because it depends on buildout-specific parameters and the directory because it depends on buildout-specific parameters and the
eggs directory can be shared across multiple buildouts. eggs directory can be shared across multiple buildouts.
...@@ -188,15 +180,11 @@ Let's define a script that uses out ext demo: ...@@ -188,15 +180,11 @@ Let's define a script that uses out ext demo:
... develop = demo ... develop = demo
... parts = extdemo demo ... parts = extdemo demo
... ...
... [extdemo-env]
... test-variable = foo
...
... [extdemo] ... [extdemo]
... recipe = zc.recipe.egg:custom ... recipe = zc.recipe.egg:custom
... find-links = %(server)s ... find-links = %(server)s
... index = %(server)s/index ... index = %(server)s/index
... include-dirs = include ... include-dirs = include
... envirionment = extdemo-env
... ...
... [demo] ... [demo]
... recipe = zc.recipe.egg ... recipe = zc.recipe.egg
...@@ -252,7 +240,6 @@ version is imported: ...@@ -252,7 +240,6 @@ version is imported:
>>> print system(buildout), >>> print system(buildout),
Develop: '/sample-buildout/demo' Develop: '/sample-buildout/demo'
Updating extdemo. Updating extdemo.
Have environment test-variable: foo
zip_safe flag not set; analyzing archive contents... zip_safe flag not set; analyzing archive contents...
Updating demo. Updating demo.
Generated script '/sample-buildout/bin/demo'. Generated script '/sample-buildout/bin/demo'.
...@@ -302,6 +289,161 @@ We can specify a specific version using the egg option: ...@@ -302,6 +289,161 @@ We can specify a specific version using the egg option:
d extdemo-1.4-py2.4-linux-i686.egg d extdemo-1.4-py2.4-linux-i686.egg
- zc.recipe.egg.egg-link - 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 Controlling develop-egg generation
================================== ==================================
...@@ -401,7 +543,6 @@ set with a value of 2. ...@@ -401,7 +543,6 @@ set with a value of 2.
>>> print system(buildout), >>> print system(buildout),
Develop: '/sample-buildout/demo' Develop: '/sample-buildout/demo'
Uninstalling demo.
Uninstalling extdemo. Uninstalling extdemo.
Installing extdemo. Installing extdemo.
Installing demo. Installing demo.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment