Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def relocateable_ve(ve_dir):
log.debug('Making virtualenv relocatable')
virtualenv.make_environment_relocatable(ve_dir)
# Make activate relocatable, using approach taken in
# https://github.com/pypa/virtualenv/pull/236
activate = []
with open(os.path.join(ve_dir, 'bin', 'activate')) as f:
for line in f:
line = line.strip()
if line == 'deactivate () {':
activate += ['ACTIVATE_PATH_FALLBACK="$_"', '']
if line.startswith('VIRTUAL_ENV='):
activate += [
'# attempt to determine VIRTUAL_ENV in relocatable way',
'if [ ! -z "${BASH_SOURCE:-}" ]; then',
' # bash',
' ACTIVATE_PATH="${BASH_SOURCE}"',
'elif [ ! -z "${DASH_SOURCE:-}" ]; then',
' # dash',
' ACTIVATE_PATH="${DASH_SOURCE}"',
'elif [ ! -z "${ZSH_VERSION:-}" ]; then',
' # zsh',
' ACTIVATE_PATH="$0"',
'elif [ ! -z "${KSH_VERSION:-}" ] '
'|| [ ! -z "${.sh.version:}" ]; then',
' # ksh - we have to use history, '
'and unescape spaces before quoting',
' ACTIVATE_PATH="$(history -r -l -n | head -1 | sed -e '
'\'s/^[\\t ]*\\(\\.\\|source\\) *//;s/\\\\ / /g\')"',
'elif [ "$(basename "$ACTIVATE_PATH_FALLBACK")" == '
'"activate.sh" ]; then',
' ACTIVATE_PATH="${ACTIVATE_PATH_FALLBACK}"',
'else',
' ACTIVATE_PATH=""',
'fi',
'',
'# default to non-relocatable path',
]
if line == 'export VIRTUAL_ENV':
activate += [
'if [ ! -z "${ACTIVATE_PATH:-}" ]; then',
' VIRTUAL_ENV="$(cd '
'"$(dirname "${ACTIVATE_PATH}")/.."; pwd)"',
'fi',
'unset ACTIVATE_PATH',
'unset ACTIVATE_PATH_FALLBACK',
]
activate.append(line)
with open(os.path.join(ve_dir, 'bin', 'activate'), 'w') as f:
f.write('\n'.join(activate))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.