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 = ...@@ -10,64 +10,103 @@ 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
"""
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')): if is_true(options.get('default-instance')):
cookbook = "{slapos-cookbook:recipe}" self.buildout.parse(instance_template % {
self.buildout.parse(""" "cookbook": "{slapos-cookbook:recipe}",
[.%(name)s.instance] "location": location,
recipe = slapos.recipe.template "name": name,
output = ${buildout:directory}/instance.cfg })
depends = $%(cookbook)s
inline = env = {
[buildout] "PATH": self.buildout['buildout']['bin-directory'] + ":\$PATH",
parts = publish "PS1": "\"(" + self.name + ") \$PS1\"",
}
eggs-directory = ${buildout:eggs-directory} if environment:
develop-eggs-directory = ${buildout:develop-eggs-directory} for line in environment.splitlines():
key, value = line.split("=", 1)
env[key.strip()] = value.strip()
[publish] self.env = env
recipe = slapos.cookbook:publish
activate-script = %(location)s
""" % locals())
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 =
deactivate () { if type deactivate > /dev/null 2>&1
set PATH PS1 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 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 shift
done done
unset -f deactivate %(message)s
} fi
VENV_PATH=%(path)s
_OLD_VENV_PATH=$PATH
_OLD_VENV_PS1=$PS1
PATH=$VENV_PATH:$PATH
PS1='(%(name)s) '$PS1
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