Commit 99904281 authored by Thomas Leymonerie's avatar Thomas Leymonerie

component/macros/virtual-env: Add script chaining

Now, with the `chain` option, you can source a chain of activate script
created by the macro.

In addition, the `deactivate` function will return to the initial environment.
parent a088b69a
......@@ -2,6 +2,8 @@
extends =
../pygolang/buildout.cfg
parts =
[virtual-env-base]
recipe = slapos.recipe.build
name = ${:_buildout_section_name_}
......@@ -12,6 +14,7 @@ init =
name = options['name']
eggs = options.get('eggs')
self.message = options.get('message')
self.chain = options.get('chain')
environment = options.get('environment')
scripts = options.get('scripts')
......@@ -74,14 +77,20 @@ init =
install =
message = ""
if self.message:
message = "\n echo " + "\n echo ".join(
message = "echo " + "\n echo ".join(
"%r" % line for line in self.message.splitlines())
message += "\n echo \'\'"
chain = ""
if self.chain:
chain = "source " + "\n source ".join(
"%r" % line for line in self.chain.splitlines())
with open(location, "w") as f:
f.write(options['template'] % {
"env": " ".join("%s %s" % (k, v) for k, v in self.env.items()),
"message": message,
"chain": chain,
})
# Template virtual env for bash shell in posix
......@@ -89,24 +98,47 @@ install =
template =
if type deactivate > /dev/null 2>&1
then
echo "deactivate current environment first"
export _OLD_PARAM=( "$@")
set %(env)s
while [ "$1" ]; do
if ! ( echo $_LIST_OLD_VENV | grep $1 ) > /dev/null 2>&1
then
eval "export _OLD_VENV_$1=\$$1"
eval "export _LIST_OLD_VENV=\"$1 \$_LIST_OLD_VENV\""
fi
eval "export $1=\"$2\""
shift
shift
done
if [[ -n "$_OLD_PARAM" ]]; then
set "$${_OLD_PARAM[@]}"
fi
unset _OLD_PARAM
%(chain)s
%(message)s
else
deactivate () {
set %(env)s
set $_LIST_OLD_VENV
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
unset _LIST_OLD_VENV
}
export _OLD_PARAM=( "$@" )
set %(env)s
while [ "$1" ]; do
eval "_OLD_VENV_$1=\$$1"
eval "export $1=\"$2\""
eval "export _LIST_OLD_VENV=\"$1 \$_LIST_OLD_VENV\""
shift
shift
done
if [[ -n "$_OLD_PARAM" ]]; then
set "$${_OLD_PARAM[@]}"
fi
unset _OLD_PARAM
%(chain)s
%(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