Skip to main content
Build status

Overview

This recipe allows to set and get environment variables during the execution of a buildout.

The recipe mirrors the current environment variables into its section, so that e.g. ${environment:USER} will give the current user.

To set an environment variable you just set it in the section.

The environment variables are set and get during the initialization of the Recipe instance, i.e. after buildout.cfg is read but before any recipe is installed or updated.

Example usage: Use an environment variable

We’ll start by creating a buildout that uses the recipe:

>>> write('buildout.cfg',
... """
... [buildout]
... parts = environment print
...
... [some-section]
... some-option = ${environment:SOME_VARIABLE}
...
... [environment]
... recipe = collective.recipe.environment
...
... [print]
... recipe = mr.scripty
... install =
...     ... print(self.buildout['some-section']['some-option'])
...     ... return []
... """)

The mr.scripty recipe is used to print out the value of the ${some-section:some-option} option.

Now we set the environment variable:

>>> import os
>>> os.environ['SOME_VARIABLE'] = 'some_value'

Running the buildout gives us:

>>> run_buildout()
START...
some_value
...END

Example usage: Set an environment variable

We’ll start by creating a buildout that uses the recipe:

>>> write('buildout.cfg',
... """
... [buildout]
... parts = environment print
...
... [some-section]
... some-option = value2
...
... [environment]
... recipe = collective.recipe.environment
... var1 = value1
... var2 = ${some-section:some-option}
...
... [print]
... recipe = mr.scripty
... install =
...     ... import os
...     ... for var in ('var1', 'var2'):
...     ...     print('%s = %s' % (var, os.environ[var]))
...     ... return []
... """)

The mr.scripty recipe is used to print out the values of the environment variables.

Running the buildout gives us:

>>> run_buildout()
START...
var1 = value1
var2 = value2
...END

Similar recipes

The functionality to mirror the environment variables into the recipe’s section is largely copied from gocept.recipe.env.

Regression test: Values containing variable substitution syntax breaks things

Problem: if an environment variable value contains something looking like variable substitution in Buildout syntax then things break. We fix this by escaping the variable substitutions using two dollar signs. Eg.: ${foo} becomes $${foo}.

Let’s see if it works.

Set environment variables:

>>> os.environ['PROBLEM_VAR_1'] = '${foo}'
>>> os.environ['PROBLEM_VAR_2'] = '${foo:bar}'
>>> os.environ['PROBLEM_VAR_3'] = 'Contains ${foo} and also ${foo:bar}.'
>>> os.environ['LEGAL_VAR_1'] = '$foo'
>>> os.environ['LEGAL_VAR_2'] = '{foo}'

Write a buildout using those variables:

>>> write('buildout.cfg',
... """
... [buildout]
... parts = environment print
...
... [some-section]
... option-1 = ${environment:PROBLEM_VAR_1}
... option-2 = ${environment:PROBLEM_VAR_2}
... option-3 = ${environment:PROBLEM_VAR_3}
... option-4 = ${environment:LEGAL_VAR_1}
... option-5 = ${environment:LEGAL_VAR_2}
...
... [environment]
... recipe = collective.recipe.environment
...
... [print]
... recipe = mr.scripty
... install =
...     ... section = self.buildout['some-section']
...     ... for (k, v) in sorted(section.items()):
...     ...     print('{} = {}'.format(k, v))
...     ... return []
...
... """)

Running the buildout gives us:

>>> run_buildout()
START...
option-1 = $${foo}
option-2 = $${foo:bar}
option-3 = Contains $${foo} and also $${foo:bar}.
option-4 = $foo
option-5 = {foo}
...END

Changelog

1.1.0 (2017-07-27)

New features:

  • Fix code for Python 3 compatiblity. [cas–]

1.0.0 (2017-07-20)

Breaking changes:

  • Fix bug: Values containing variable substitution syntax breaks things. This is a breaking change because problematic values are escaped, eg. “${foo}” becomes “$${foo}”.

0.2.0 (2012-08-21)

  • Now it’s possible to read environment variables.

0.1b1 (2011-08-18)

  • First release.

Release files for collective.recipe.environment 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for collective.recipe.environment 1.1.0
File Size Uploaded
collective.recipe.environment-1.1.0.tar.gz 5.4 kB Details

Release files / collective.recipe.environment-1.1.0.tar.gz

Download URL collective.recipe.environment-1.1.0.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
0776a68ebf4e10d879733a514b998a8540fa70083283913b02e6f6641027313f
BLAKE2b-256 checksum
How to use checksums
e401c2b6bfe67636193c39c3decb8720aab526e5fb67c12a72e5d38a4eaae01b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.1.0 This release

1 release file

1.0.0

1 release file

0.2.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page