Commit 41ac5395 authored by zagy's avatar zagy

- fixed typo in environment

- allowed to interpolate variables with the environment

- make sure the environment is cleaned

git-svn-id: http://svn.zope.org/repos/main/zc.buildout/trunk@84019 62d5b8a3-27da-0310-9561-8e5933582275
parent 6b0a8ca0
......@@ -57,10 +57,14 @@ class Custom(Base):
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,11 +88,36 @@ class Custom(Base):
distribution = options.get('egg', options.get('eggs', self.name)
).strip()
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):
......
......@@ -84,7 +84,7 @@ python
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 @@ the egg:
... [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 @@ Let's define a script that uses out ext demo:
... 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 @@ version is imported:
>>> 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 @@ We can specify a specific version using the egg option:
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 @@ set with a value of 2.
>>> print system(buildout),
Develop: '/sample-buildout/demo'
Uninstalling demo.
Uninstalling extdemo.
Installing extdemo.
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