Commit a088b69a authored by Thomas Leymonerie's avatar Thomas Leymonerie

component/macros/virtual-env: allows you to create a custom environment

The `message` option allows to write a message that will be displayed when sourcing the script.

The `environment` option allows you to write your own environment variables for the virtual environment.
To use it:
environment =
  <env-name> = <env-value>
parent bddc5491
......@@ -10,64 +10,103 @@ init =
from zc.buildout.easy_install import working_set
import os
name = options['name']
eggs = options['eggs']
try:
scripts = "scripts = " + options['scripts']
except KeyError:
scripts = ""
eggs = options.get('eggs')
self.message = options.get('message')
environment = options.get('environment')
scripts = options.get('scripts')
self.buildout.parse("""
eggs_template = """
[.%(name)s.install-eggs]
recipe = zc.recipe.egg
eggs = %(eggs)s
eggs =
%(eggs)s
%(scripts)s
[.%(name)s.install-interpreter]
<= python-interpreter
eggs += %(eggs)s
""" % locals())
eggs +=
%(eggs)s
"""
instance_template = """
[.%(name)s.instance]
recipe = slapos.recipe.template
output = ${buildout:directory}/instance.cfg
depends = $%(cookbook)s
inline =
[buildout]
parts = publish
eggs-directory = ${buildout:eggs-directory}
develop-eggs-directory = ${buildout:develop-eggs-directory}
[publish]
recipe = slapos.cookbook:publish
activate-script = %(location)s
"""
if eggs:
self.buildout.parse(eggs_template % {
"eggs": "\n ".join(e.strip() for e in eggs.splitlines()),
"name": name,
"scripts": "scripts = " + scripts if scripts else "",
})
if is_true(options.get('default-instance')):
cookbook = "{slapos-cookbook:recipe}"
self.buildout.parse("""
[.%(name)s.instance]
recipe = slapos.recipe.template
output = ${buildout:directory}/instance.cfg
depends = $%(cookbook)s
inline =
[buildout]
parts = publish
self.buildout.parse(instance_template % {
"cookbook": "{slapos-cookbook:recipe}",
"location": location,
"name": name,
})
env = {
"PATH": self.buildout['buildout']['bin-directory'] + ":\$PATH",
"PS1": "\"(" + self.name + ") \$PS1\"",
}
eggs-directory = ${buildout:eggs-directory}
develop-eggs-directory = ${buildout:develop-eggs-directory}
if environment:
for line in environment.splitlines():
key, value = line.split("=", 1)
env[key.strip()] = value.strip()
[publish]
recipe = slapos.cookbook:publish
activate-script = %(location)s
""" % locals())
self.env = env
install =
message = ""
if self.message:
message = "\n echo " + "\n echo ".join(
"%r" % line for line in self.message.splitlines())
message += "\n echo \'\'"
with open(location, "w") as f:
f.write(options['template'] % {
"path" : self.buildout['buildout']['bin-directory'],
"name" : self.name,
"env": " ".join("%s %s" % (k, v) for k, v in self.env.items()),
"message": message,
})
# Template virtual env for bash shell in posix
[virtual-env-base:posix]
template =
deactivate () {
set PATH PS1
if type deactivate > /dev/null 2>&1
then
echo "deactivate current environment first"
else
deactivate () {
set %(env)s
while [ "$1" ]; do
eval "if [ \"\$_OLD_VENV_$1\" ]; then $1=\$_OLD_VENV_$1; else unset $1; fi; unset \$_OLD_VENV_$1"
shift
shift
done
unset -f deactivate
}
set %(env)s
while [ "$1" ]; do
eval "if [ \"\$_OLD_VENV_$1\" ]; then $1=\$_OLD_VENV_$1; else unset $1; fi; unset \$_OLD_VENV_$1"
eval "_OLD_VENV_$1=\$$1"
eval "export $1=\"$2\""
shift
shift
done
unset -f deactivate
}
VENV_PATH=%(path)s
_OLD_VENV_PATH=$PATH
_OLD_VENV_PS1=$PS1
PATH=$VENV_PATH:$PATH
PS1='(%(name)s) '$PS1
%(message)s
fi
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