diff options
| author | Siddhant Kumar <saytosid@gmail.com> | 2019-12-15 19:13:08 +0000 |
|---|---|---|
| committer | Bernat Gabor <bgabor8@bloomberg.net> | 2020-01-10 15:38:36 +0000 |
| commit | c72cd301583aebcd8f0565673ac1847ee9fe0faa (patch) | |
| tree | 487d4f9db9ea0ca990a834076664d5b43995efee /src/virtualenv/activation/python | |
| parent | 7928094d2df6910802a638e6919cf5e30977956d (diff) | |
| download | virtualenv-c72cd301583aebcd8f0565673ac1847ee9fe0faa.tar.gz | |
Activation scripts for next-gen virtualenv (#1454)
Diffstat (limited to 'src/virtualenv/activation/python')
| -rw-r--r-- | src/virtualenv/activation/python/__init__.py | 19 | ||||
| -rw-r--r-- | src/virtualenv/activation/python/activate_this.py | 45 |
2 files changed, 64 insertions, 0 deletions
diff --git a/src/virtualenv/activation/python/__init__.py b/src/virtualenv/activation/python/__init__.py new file mode 100644 index 0000000..1d73e99 --- /dev/null +++ b/src/virtualenv/activation/python/__init__.py @@ -0,0 +1,19 @@ +from __future__ import absolute_import, unicode_literals + +import json +import os + +from pathlib2 import Path + +from ..via_template import ViaTemplateActivator + + +class PythonActivator(ViaTemplateActivator): + def templates(self): + yield Path("activate_this.py") + + def replacements(self, creator, dest_folder): + replacements = super(PythonActivator, self).replacements(creator, dest_folder) + site_dump = json.dumps([os.path.relpath(str(i), str(dest_folder)) for i in creator.site_packages], indent=2) + replacements.update({"__SITE_PACKAGES__": site_dump}) + return replacements diff --git a/src/virtualenv/activation/python/activate_this.py b/src/virtualenv/activation/python/activate_this.py new file mode 100644 index 0000000..fc8d449 --- /dev/null +++ b/src/virtualenv/activation/python/activate_this.py @@ -0,0 +1,45 @@ +"""Activate virtualenv for current interpreter: + +Use exec(open(this_file).read(), {'__file__': this_file}). + +This can be used when you must use an existing Python interpreter, not the virtualenv bin/python. +""" +import json +import os +import site +import sys + +try: + __file__ +except NameError: + raise AssertionError("You must use exec(open(this_file).read(), {'__file__': this_file}))") + +# prepend bin to PATH (this file is inside the bin directory) +bin_dir = os.path.dirname(os.path.abspath(__file__)) +os.environ["PATH"] = os.pathsep.join([bin_dir] + os.environ.get("PATH", "").split(os.pathsep)) + +base = os.path.dirname(bin_dir) + +# virtual env is right above bin directory +os.environ["VIRTUAL_ENV"] = base + +# add the virtual environments site-packages to the host python import mechanism +prev = set(sys.path) + +# fmt: off +# turn formatter off as json dumps will contain " characters - so we really need here ' black +site_packages = r''' +__SITE_PACKAGES__ +''' + +for site_package in json.loads(site_packages): + path = os.path.realpath(os.path.join(os.path.dirname(__file__), site_package)) + site.addsitedir(path) +# fmt: on + +sys.real_prefix = sys.prefix +sys.prefix = base + +# Move the added items to the front of the path, in place +new = list(sys.path) +sys.path[:] = [i for i in new if i not in prev] + [i for i in new if i in prev] |
