Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix generated scripts use host version info rather than target #1655

Merged
merged 1 commit into from
Feb 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog/1600.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix generated scripts use host version info rather than target - by :user:`gaborbernat`.
31 changes: 26 additions & 5 deletions src/virtualenv/seed/via_app_data/pip_install/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import os
import re
import shutil
import sys
import zipfile
from abc import ABCMeta, abstractmethod
from contextlib import contextmanager
from tempfile import mkdtemp
from threading import Lock

from six import PY3, add_metaclass

Expand All @@ -17,6 +20,8 @@

@add_metaclass(ABCMeta)
class PipInstall(object):
lock = Lock()

def __init__(self, wheel, creator, image_folder):
self._wheel = wheel
self._creator = creator
Expand All @@ -29,7 +34,7 @@ def __init__(self, wheel, creator, image_folder):
def _sync(self, src, dst):
raise NotImplementedError

def install(self):
def install(self, version_info):
self._extracted = True
# sync image
for filename in self._image_dir.iterdir():
Expand All @@ -44,7 +49,7 @@ def install(self):
consoles = set()
script_dir = self._creator.script_dir
for name, module in self._console_scripts.items():
consoles.update(self._create_console_entry_point(name, module, script_dir))
consoles.update(self._create_console_entry_point(name, module, script_dir, version_info))
logging.debug("generated console scripts %s", " ".join(i.name for i in consoles))

def build_image(self):
Expand Down Expand Up @@ -77,10 +82,11 @@ def _generate_new_files(self):
try:
to_folder = Path(folder)
rel = os.path.relpath(ensure_text(str(self._creator.script_dir)), ensure_text(str(self._creator.purelib)))
version_info = self._creator.interpreter.version_info
for name, module in self._console_scripts.items():
new_files.update(
Path(os.path.normpath(ensure_text(str(self._image_dir / rel / i.name))))
for i in self._create_console_entry_point(name, module, to_folder)
for i in self._create_console_entry_point(name, module, to_folder, version_info)
)
finally:
shutil.rmtree(folder, ignore_errors=True)
Expand Down Expand Up @@ -123,7 +129,7 @@ def _console_scripts(self):
self._console_entry_points[name] = value
return self._console_entry_points

def _create_console_entry_point(self, name, value, to_folder):
def _create_console_entry_point(self, name, value, to_folder, version_info):
result = []
from distlib.scripts import ScriptMaker

Expand All @@ -133,10 +139,25 @@ def _create_console_entry_point(self, name, value, to_folder):
maker.set_mode = True # ensure they are executable
maker.executable = str(self._creator.exe)
specification = "{} = {}".format(name, value)
new_files = maker.make(specification)
with self.switch_sys_version(version_info):
new_files = maker.make(specification)
result.extend(Path(i) for i in new_files)
return result

@contextmanager
def switch_sys_version(self, version_info):
"""
Patch until upstream distutils supports creating scripts with different python target
https://bitbucket.org/pypa/distlib/issues/134/allow-specifying-the-version-information
"""
previous = sys.version_info
with self.lock:
sys.version_info = version_info
try:
yield
finally:
sys.version_info = previous

def clear(self):
if self._image_dir.exists():
shutil.rmtree(ensure_text(str(self._image_dir)))
Expand Down
2 changes: 1 addition & 1 deletion src/virtualenv/seed/via_app_data/via_app_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def _install(name, wheel):
installer.clear()
if not installer.has_image():
installer.build_image()
installer.install()
installer.install(creator.interpreter.version_info)

threads = list(Thread(target=_install, args=(n, w)) for n, w in name_to_whl.items())
for thread in threads:
Expand Down
6 changes: 5 additions & 1 deletion tests/unit/create/test_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,17 @@ def test_cross_major(cross_python, coverage_env, tmp_path, current_fastest):
"-p",
ensure_text(cross_python.executable),
ensure_text(str(tmp_path)),
"--no-seed",
"--no-setuptools",
"--no-wheel",
"--activators",
"",
"--creator",
current_fastest,
]
result = cli_run(cmd)
pip_scripts = {i.name.replace(".exe", "") for i in result.creator.script_dir.iterdir() if i.name.startswith("pip")}
major, minor = cross_python.version_info[0:2]
assert pip_scripts == {"pip", "pip-{}.{}".format(major, minor), "pip{}".format(major)}
coverage_env()
env = PythonInfo.from_exe(str(result.creator.exe))
assert env.version_info.major != CURRENT.version_info.major
Expand Down