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