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