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

Unravel pip shims #5204

Merged
merged 4 commits into from
Jul 30, 2022
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 news/5204.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove usages of ``pip_shims`` from the non vendored ``pipenv`` code, but retain initialization for ``requirementslib`` still has usages.
29 changes: 11 additions & 18 deletions pipenv/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@

from pipenv import environments, exceptions, pep508checker, progress
from pipenv._compat import decode_for_output, fix_utf8
from pipenv.patched.pip._internal.exceptions import PipError
from pipenv.patched.pip._internal.network.session import PipSession
from pipenv.patched.pip._internal.req.constructors import (
install_req_from_parsed_requirement,
)
from pipenv.patched.pip._internal.req.req_file import parse_requirements
from pipenv.utils.constants import MYPY_RUNNING
from pipenv.utils.dependencies import (
convert_deps_to_pip,
Expand All @@ -30,7 +36,6 @@
from pipenv.utils.indexes import get_source_list, parse_indexes, prepare_pip_source_args
from pipenv.utils.internet import download_file, get_host_and_port, is_valid_url
from pipenv.utils.processes import run_command
from pipenv.utils.resolver import venv_resolve_deps
from pipenv.utils.shell import (
cmd_list_to_shell,
find_python,
Expand Down Expand Up @@ -160,12 +165,6 @@ def cleanup_virtualenv(project, bare=True):


def import_requirements(project, r=None, dev=False):
from pipenv.patched.pip._internal.req.constructors import (
install_req_from_parsed_requirement,
)
from pipenv.patched.pip._vendor import requests
from pipenv.vendor.pip_shims.shims import parse_requirements

# Parse requirements.txt file with Pip's parser.
# Pip requires a `PipSession` which is a subclass of requests.Session.
# Since we're not making any network calls, it's initialized to nothing.
Expand All @@ -191,7 +190,7 @@ def import_requirements(project, r=None, dev=False):
trusted_hosts = sorted(set(trusted_hosts))
reqs = [
install_req_from_parsed_requirement(f)
for f in parse_requirements(r, session=requests)
for f in parse_requirements(r, session=PipSession())
]
for package in reqs:
if package.name not in BAD_PACKAGES:
Expand Down Expand Up @@ -1124,6 +1123,8 @@ def do_lock(
err=True,
)

from pipenv.utils.resolver import venv_resolve_deps

# Mutates the lockfile
venv_resolve_deps(
packages,
Expand Down Expand Up @@ -1372,8 +1373,6 @@ def get_pip_args(
selective_upgrade: bool = False,
src_dir: Optional[str] = None,
) -> List[str]:
from pipenv.patched.pip._vendor.packaging.version import parse as parse_version

arg_map = {
"pre": ["--pre"],
"verbose": ["--verbose"],
Expand All @@ -1388,10 +1387,8 @@ def get_pip_args(
],
"src_dir": src_dir,
}
if project.environment.pip_version >= parse_version("19.0"):
arg_map["no_use_pep517"].append("--no-use-pep517")
if project.environment.pip_version < parse_version("19.1"):
arg_map["no_use_pep517"].append("--no-build-isolation")
# TODO: Why do we use no pep517?
arg_map["no_use_pep517"].append("--no-use-pep517")
arg_set = []
for key in arg_map.keys():
if key in locals() and locals().get(key):
Expand All @@ -1407,11 +1404,9 @@ def get_requirement_line(
include_hashes: bool = True,
format_for_file: bool = False,
) -> Union[List[str], str]:
line = None
if requirement.vcs or requirement.is_file_or_url:
if src_dir and requirement.line_instance.wheel_kwargs:
requirement.line_instance._wheel_kwargs.update({"src_dir": src_dir})
requirement.line_instance.vcsrepo
line = requirement.line_instance.line
if requirement.line_instance.markers:
line = f"{line}; {requirement.line_instance.markers}"
Expand Down Expand Up @@ -1950,8 +1945,6 @@ def do_install(
selective_upgrade=False,
site_packages=None,
):
from .vendor.pip_shims.shims import PipError

requirements_directory = vistir.path.create_tracked_tempdir(
suffix="-requirements", prefix="pipenv-"
)
Expand Down
52 changes: 12 additions & 40 deletions pipenv/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import pkg_resources

import pipenv
from pipenv.patched.pip._internal.commands.install import InstallCommand
from pipenv.patched.pip._internal.index.package_finder import PackageFinder
from pipenv.patched.pip._vendor.packaging.utils import canonicalize_name
from pipenv.utils.constants import is_type_checking
from pipenv.utils.indexes import prepare_pip_source_args
Expand All @@ -26,10 +28,8 @@
from types import ModuleType
from typing import ContextManager, Dict, Generator, List, Optional, Set, Union

import pip_shims.shims
import tomlkit

from pipenv.patched.pip._vendor.packaging.version import Version
from pipenv.project import Project, TPipfile, TSource

BASE_WORKING_SET = pkg_resources.WorkingSet(sys.path)
Expand Down Expand Up @@ -539,21 +539,6 @@ def libdir(self) -> str:
return "purelib", purelib
return "platlib", self.paths["platlib"]

@property
def pip_version(self) -> Version:
"""
Get the pip version in the environment. Useful for knowing which args we can use
when installing.
"""
from pipenv.patched.pip._vendor.packaging.version import parse as parse_version

pip = next(
iter(pkg for pkg in self.get_installed_packages() if pkg.key == "pip"), None
)
if pip is not None:
return parse_version(pip.version)
return parse_version("20.2")

def expand_egg_links(self) -> None:
"""
Expand paths specified in egg-link files to prevent pip errors during
Expand Down Expand Up @@ -641,41 +626,28 @@ def get_installed_packages(self) -> List[pkg_resources.Distribution]:
return packages

@contextlib.contextmanager
def get_finder(
self, pre: bool = False
) -> ContextManager[pip_shims.shims.PackageFinder]:
from .vendor.pip_shims.shims import InstallCommand, get_package_finder
def get_finder(self, pre: bool = False) -> ContextManager[PackageFinder]:
from .utils.resolver import get_package_finder

pip_command = InstallCommand()
pip_command = InstallCommand(
name="InstallCommand", summary="pip Install command."
)
pip_args = prepare_pip_source_args(self.sources)
pip_options, _ = pip_command.parser.parse_args(pip_args)
pip_options.cache_dir = self.project.s.PIPENV_CACHE_DIR
pip_options.pre = self.pipfile.get("pre", pre)
with pip_command._build_session(pip_options) as session:
finder = get_package_finder(
install_cmd=pip_command, options=pip_options, session=session
)
yield finder
session = pip_command._build_session(pip_options)
finder = get_package_finder(
install_cmd=pip_command, options=pip_options, session=session
)
yield finder

def get_package_info(
self, pre: bool = False
) -> Generator[pkg_resources.Distribution, None, None]:
from .vendor.pip_shims.shims import parse_version, pip_version

dependency_links = []
packages = self.get_installed_packages()
# This code is borrowed from pip's current implementation
if parse_version(pip_version) < parse_version("19.0"):
for dist in packages:
if dist.has_metadata("dependency_links.txt"):
dependency_links.extend(
dist.get_metadata_lines("dependency_links.txt")
)

with self.get_finder() as finder:
if parse_version(pip_version) < parse_version("19.0"):
finder.add_dependency_links(dependency_links)

for dist in packages:
typ = "unknown"
all_candidates = finder.find_all_candidates(dist.key)
Expand Down
7 changes: 3 additions & 4 deletions pipenv/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@
from pipenv.core import system_which
from pipenv.environment import Environment
from pipenv.environments import Setting, is_in_virtualenv, normalize_pipfile_path
from pipenv.patched.pip._internal.commands.install import InstallCommand
from pipenv.utils.constants import is_type_checking
from pipenv.utils.dependencies import (
get_canonical_names,
is_editable,
is_star,
pep423_name,
python_version,
)
from pipenv.utils.internet import get_url_name, is_valid_url, proper_case
from pipenv.utils.resolver import pep423_name
from pipenv.utils.shell import (
find_requirements,
find_windows_executable,
Expand Down Expand Up @@ -641,10 +642,8 @@ def pipfile_is_empty(self):

def create_pipfile(self, python=None):
"""Creates the Pipfile, filled with juicy defaults."""
from .vendor.pip_shims.shims import InstallCommand

# Inherit the pip's index configuration of install command.
command = InstallCommand()
command = InstallCommand(name="InstallCommand", summary="pip Install command.")
indexes = command.cmd_opts.get_option("--extra-index-url").default
sources = [self.default_source]
for i, index in enumerate(indexes):
Expand Down
Loading