From e7813094d72dcf8932c9b38b9d0908500e3793c4 Mon Sep 17 00:00:00 2001 From: Eklavya Sharma Date: Wed, 20 Jul 2016 19:04:31 +0530 Subject: [PATCH] Patch activate script to set VIRTUAL_ENV correctly. Patch a virtualenv's activate script to not resolve symlinks when setting the environment variable VIRTUAL_ENV. Fixes #1190. --- scripts/lib/setup_venv.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/scripts/lib/setup_venv.py b/scripts/lib/setup_venv.py index 8a0eb16aa4..556ddc2d99 100644 --- a/scripts/lib/setup_venv.py +++ b/scripts/lib/setup_venv.py @@ -34,6 +34,21 @@ VENV_DEPENDENCIES = [ "libpq-dev", # Needed by psycopg2 ] +def patch_activate_script(venv_path): + # venv_path should be what we want to have in VIRTUAL_ENV after patching + script_path = os.path.join(venv_path, "bin", "activate") + + file_obj = open(script_path) + lines = file_obj.readlines() + for i, line in enumerate(lines): + if line.startswith('VIRTUAL_ENV='): + lines[i] = 'VIRTUAL_ENV="%s"\n' % (venv_path,) + file_obj.close() + + file_obj = open(script_path, 'w') + file_obj.write("".join(lines)) + file_obj.close() + def setup_virtualenv(target_venv_path, requirements_file, virtualenv_args=None): # type: (Optional[str], str, Optional[List[str]]) -> str @@ -53,6 +68,7 @@ def setup_virtualenv(target_venv_path, requirements_file, virtualenv_args=None): print("Using cached Python venv from %s" % (cached_venv_path,)) if target_venv_path is not None: run(["sudo", "ln", "-nsf", cached_venv_path, target_venv_path]) + patch_activate_script(target_venv_path) activate_this = os.path.join(cached_venv_path, "bin", "activate_this.py") exec(open(activate_this).read(), {}, dict(__file__=activate_this)) # type: ignore # https://github.com/python/mypy/issues/1577 return cached_venv_path