Skip to content

Commit

Permalink
allow python activator to be relocate-able
Browse files Browse the repository at this point in the history
Signed-off-by: Bernat Gabor <[email protected]>
  • Loading branch information
gaborbernat committed Nov 29, 2019
1 parent 272056b commit eb64b9f
Show file tree
Hide file tree
Showing 11 changed files with 294 additions and 134 deletions.
8 changes: 5 additions & 3 deletions src/virtualenv/activation/python/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import, unicode_literals

import json
import os

from pathlib2 import Path

Expand All @@ -11,7 +12,8 @@ class PythonActivator(ViaTemplateActivator):
def templates(self):
yield Path("activate_this.py")

def replacements(self, creator):
replacements = super(PythonActivator, self).replacements(creator)
replacements.update({"__SITE_PACKAGES__": json.dumps(list(str(i) for i in creator.site_packages), indent=2)})
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
4 changes: 3 additions & 1 deletion src/virtualenv/activation/python/activate_this.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
site_packages = r'''
__SITE_PACKAGES__
'''

for site_package in json.loads(site_packages):
site.addsitedir(site_package)
path = os.path.realpath(os.path.join(os.path.dirname(__file__), site_package))
site.addsitedir(path)
# fmt: on

sys.real_prefix = sys.prefix
Expand Down
7 changes: 4 additions & 3 deletions src/virtualenv/activation/via_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ def templates(self):
raise NotImplementedError

def generate(self, creator):
self._generate(self.replacements(creator), self.templates(), creator.bin_dir)
dest_folder = creator.bin_dir
self._generate(self.replacements(creator, dest_folder), self.templates(), dest_folder)
if self.flag_prompt is not None:
creator.pyenv_cfg["prompt"] = self.flag_prompt

def replacements(self, creator):
def replacements(self, creator, dest_folder):
return {
"__VIRTUAL_PROMPT__": "" if self.flag_prompt is None else self.flag_prompt,
"__VIRTUAL_ENV__": str(creator.env_dir),
"__VIRTUAL_ENV__": str(creator.dest_dir),
"__VIRTUAL_NAME__": str(creator.env_name),
"__BIN_NAME__": str(creator.bin_name),
"__PATH_SEP__": os.pathsep,
Expand Down
4 changes: 2 additions & 2 deletions src/virtualenv/interpreters/create/cpython/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def create(self):
self.system_site_package = true_system_site

def ensure_directories(self):
dirs = [self.env_dir, self.bin_dir]
dirs = [self.dest_dir, self.bin_dir]
dirs.extend(self.site_packages)
return dirs

Expand All @@ -53,7 +53,7 @@ def lib_base(self):

@property
def lib_dir(self):
return self.env_dir / self.lib_base
return self.dest_dir / self.lib_base

@property
def system_stdlib(self):
Expand Down
12 changes: 4 additions & 8 deletions src/virtualenv/interpreters/create/creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,17 @@ def set_pyenv_cfg(self):
"virtualenv": __version__,
}

@property
def env_dir(self):
return Path(self.dest_dir)

@property
def env_name(self):
return self.env_dir.parts[-1]
return self.dest_dir.parts[-1]

@property
def bin_name(self):
raise NotImplementedError

@property
def bin_dir(self):
return self.env_dir / self.bin_name
return self.dest_dir / self.bin_name

@property
def lib_dir(self):
Expand All @@ -127,13 +123,13 @@ def site_packages(self):
return [self.lib_dir / "site-packages"]

@property
def env_exe(self):
def exe(self):
return self.bin_dir / "python{}".format(".exe" if IS_WIN else "")

@property
def debug(self):
if self._debug is None:
self._debug = get_env_debug_info(self.env_exe, self.debug_script())
self._debug = get_env_debug_info(self.exe, self.debug_script())
return self._debug

# noinspection PyMethodMayBeStatic
Expand Down
4 changes: 2 additions & 2 deletions src/virtualenv/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def _collect_seeders():

def _get_activation(interpreter, parser, options):
activator_parser = parser.add_argument_group("activation script generator")
compatible = _collect_activators(interpreter)
compatible = collect_activators(interpreter)
default = ",".join(compatible.keys())

def _extract_activators(entered_str):
Expand Down Expand Up @@ -172,7 +172,7 @@ def _extract_activators(entered_str):
yield activator_instances


def _collect_activators(interpreter):
def collect_activators(interpreter):
all_activators = {e.name: e.load() for e in get_group_named("virtualenv.activate").values()}
activators = {k: v for k, v in all_activators.items() if v.supports(interpreter)}
return activators
2 changes: 1 addition & 1 deletion src/virtualenv/seed/embed/link_app_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def run(self, creator):


def pip_install(wheels, creator, cache):
site_package, bin_dir, env_exe = creator.site_packages[0], creator.bin_dir, creator.env_exe
site_package, bin_dir, env_exe = creator.site_packages[0], creator.bin_dir, creator.exe
folder_link_method, folder_linked = link_folder()
for name, wheel in wheels.items():
logging.debug("install %s from wheel %s", name, wheel)
Expand Down
22 changes: 0 additions & 22 deletions tests/unit/activation/conftest.py
Original file line number Diff line number Diff line change
@@ -1,22 +0,0 @@
from __future__ import absolute_import, unicode_literals

import os

import pytest
from pathlib2 import Path

from virtualenv.run import run_via_cli


@pytest.fixture(scope="session")
def activation_python(tmp_path_factory):
path = tmp_path_factory.mktemp("activation-test-env")
prev_cwd = os.getcwd()
try:
os.chdir(str(path))
session = run_via_cli([str(path), "--seeder", "none"])
pydoc_test = Path(session.creator.site_packages[0]) / "pydoc_test.py"
pydoc_test.write_text('"""This is pydoc_test.py"""')
yield session, pydoc_test
finally:
os.chdir(str(prev_cwd))
91 changes: 0 additions & 91 deletions tests/unit/activation/test_activate_this.py

This file was deleted.

Loading

0 comments on commit eb64b9f

Please sign in to comment.