Skip to content

add seed packages via pip invoke #1462

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

Merged
merged 1 commit into from
Dec 21, 2019
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 .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ exclude_lines =
omit =
# site.py is ran before the coverage can be enabled, no way to measure coverage on this
src/virtualenv/interpreters/create/impl/cpython/site.py
src/virtualenv/seed/embed/wheels/pip-*.whl/*

[coverage:paths]
source =
Expand Down
32 changes: 31 additions & 1 deletion src/virtualenv/seed/embed/pip_invoke.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from __future__ import absolute_import, unicode_literals

from .base_embed import BaseEmbed
import os
import subprocess

from virtualenv.seed.embed.base_embed import BaseEmbed
from virtualenv.seed.embed.wheels.acquire import get_bundled_wheel


class PipInvoke(BaseEmbed):
Expand All @@ -10,3 +14,29 @@ def __init__(self, options):
def run(self, creator):
if not self.enabled:
return

version = creator.interpreter.version_release_str

cmd = [str(creator.exe), "-m", "pip", "install", "--only-binary", ":all:"]
for folder in {get_bundled_wheel(p, version).parent for p in ("pip", "setuptools")}:
cmd.extend(["--find-links", str(folder)])
if not self.download:
cmd.append("--no-index")
for key, version in {"pip": self.pip_version, "setuptools": self.setuptools_version}.items():
cmd.append("{}{}".format(key, "=={}".format(version) if version is not None else ""))

env = os.environ.copy()
env.update(
{
str(k): str(v) # python 2 requires these to be string only (non-unicode)
for k, v in {
# put the bundled wheel onto the path, and use it to do the bootstrap operation
"PYTHONPATH": get_bundled_wheel("pip", version),
"PIP_USE_WHEEL": "1",
"PIP_USER": "0",
"PIP_NO_INPUT": "1",
}.items()
}
)

subprocess.call(cmd, env=env)
16 changes: 10 additions & 6 deletions src/virtualenv/seed/embed/wheels/acquire.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Bootstrap"""
from __future__ import absolute_import, unicode_literals

import subprocess
from collections import defaultdict
from shutil import copy2

Expand Down Expand Up @@ -39,9 +40,8 @@ def download_wheel(version_str, must_download, wheel_download):
str(wheel_download),
]
cmd.extend(must_download)
from pip._internal import main

main(cmd)
# pip has no interface in python - must be a new sub-process
subprocess.call(cmd)


def check_if_must_download(packages, wheel_download):
Expand Down Expand Up @@ -87,8 +87,12 @@ def _get_wheels_for_package(inside_folder, package):

def ensure_bundle_cached(packages, version_release, wheel_download):
for package in packages:
bundle = (BUNDLE_SUPPORT.get(version_release, {}) or BUNDLE_SUPPORT[MAX]).get(package)
bundle = get_bundled_wheel(package, version_release)
if bundle is not None:
bundled_wheel_file = wheel_download / bundle
bundled_wheel_file = wheel_download / bundle.name
if not bundled_wheel_file.exists():
copy2(str(BUNDLE_FOLDER / bundle), str(bundled_wheel_file))
copy2(str(bundle), str(bundled_wheel_file))


def get_bundled_wheel(package, version_release):
return BUNDLE_FOLDER / (BUNDLE_SUPPORT.get(version_release, {}) or BUNDLE_SUPPORT[MAX]).get(package)
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def test_base_bootstrap_link_via_app_data(tmp_path, coverage_env):
bundle_ver = BUNDLE_SUPPORT[CURRENT.version_release_str]
create_cmd = [
str(tmp_path / "env"),
"--seeder",
"link-app-data",
"--download",
"--pip",
bundle_ver["pip"].split("-")[1],
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/interpreters/boostrap/test_pip_invoke.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from __future__ import absolute_import, unicode_literals

from virtualenv.interpreters.discovery.py_info import CURRENT
from virtualenv.run import run_via_cli
from virtualenv.seed.embed.wheels import BUNDLE_SUPPORT


def test_base_bootstrap_via_pip_invoke(tmp_path, coverage_env):
bundle_ver = BUNDLE_SUPPORT[CURRENT.version_release_str]
create_cmd = [
"--seeder",
"pip",
str(tmp_path / "env"),
"--download",
"--pip",
bundle_ver["pip"].split("-")[1],
"--setuptools",
bundle_ver["setuptools"].split("-")[1],
]
result = run_via_cli(create_cmd)
coverage_env()
assert result

# uninstalling pip/setuptools now should leave us with a clean env
site_package = result.creator.site_packages[0]
pip = site_package / "pip"
setuptools = site_package / "setuptools"

files_post_first_create = list(site_package.iterdir())
assert pip in files_post_first_create
assert setuptools in files_post_first_create