Skip to content

Commit

Permalink
Drop old pip versions
Browse files Browse the repository at this point in the history
  • Loading branch information
atugushev committed Feb 28, 2020
1 parent a58a1c5 commit 50ecc4e
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 298 deletions.
5 changes: 0 additions & 5 deletions piptools/_compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,8 @@
WheelCache,
cmdoptions,
get_installed_distributions,
get_requirement_tracker,
global_tempdir_manager,
install_req_from_editable,
install_req_from_line,
is_dir_url,
is_file_url,
is_vcs_url,
normalize_path,
parse_requirements,
path_to_url,
Expand Down
64 changes: 2 additions & 62 deletions piptools/_compat/pip_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,12 @@
from __future__ import absolute_import

import importlib
from contextlib import contextmanager

import pip
from pip._vendor.packaging.version import parse as parse_version

PIP_VERSION = tuple(map(int, parse_version(pip.__version__).base_version.split(".")))

try:
from pip._internal.req.req_tracker import RequirementTracker
except ImportError:

@contextmanager
def RequirementTracker():
yield


# Introduced in pip 20.0
try:
from pip._internal.utils.temp_dir import global_tempdir_manager
except ImportError:

@contextmanager
def global_tempdir_manager():
yield


def do_import(module_path, subimport=None, old_path=None):
old_path = old_path or module_path
Expand Down Expand Up @@ -72,47 +53,6 @@ def do_import(module_path, subimport=None, old_path=None):
Session = do_import("_vendor.requests.sessions", "Session")
Resolver = do_import("legacy_resolve", "Resolver", old_path="resolve")
WheelCache = do_import("cache", "WheelCache", old_path="wheel")
install_req_from_line = do_import("req.constructors", "install_req_from_line")
install_req_from_editable = do_import("req.constructors", "install_req_from_editable")
normalize_path = do_import("utils.misc", "normalize_path", old_path="utils")

# pip 18.1 has refactored InstallRequirement constructors use by pip-tools.
if PIP_VERSION < (18, 1):
install_req_from_line = InstallRequirement.from_line
install_req_from_editable = InstallRequirement.from_editable
else:
install_req_from_line = do_import("req.constructors", "install_req_from_line")
install_req_from_editable = do_import(
"req.constructors", "install_req_from_editable"
)


def is_vcs_url(link):
if PIP_VERSION < (19, 3):
_is_vcs_url = do_import("download", "is_vcs_url")
return _is_vcs_url(link)

return link.is_vcs


def is_file_url(link):
if PIP_VERSION < (19, 3):
_is_file_url = do_import("download", "is_file_url")
return _is_file_url(link)

return link.is_file


def is_dir_url(link):
if PIP_VERSION < (19, 3):
_is_dir_url = do_import("download", "is_dir_url")
return _is_dir_url(link)

return link.is_existing_dir()


def get_requirement_tracker():
if PIP_VERSION[:2] <= (19, 3):
return RequirementTracker()

from pip._internal.req import req_tracker

return req_tracker.get_requirement_tracker()
170 changes: 43 additions & 127 deletions piptools/repositories/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,21 @@
import hashlib
import os
from contextlib import contextmanager
from functools import partial
from shutil import rmtree

from pip._internal.commands import create_command
from pip._internal.req.req_tracker import get_requirement_tracker
from pip._internal.utils.temp_dir import TempDirectory, global_tempdir_manager

from .._compat import (
FAVORITE_HASH,
PIP_VERSION,
Link,
PyPI,
RequirementSet,
Resolver as PipResolver,
TemporaryDirectory,
Wheel,
WheelCache,
contextlib,
get_requirement_tracker,
global_tempdir_manager,
is_dir_url,
is_file_url,
is_vcs_url,
normalize_path,
path_to_url,
url_to_path,
Expand All @@ -32,7 +28,6 @@
from ..exceptions import NoCandidateFound
from ..logging import log
from ..utils import (
create_install_command,
fs_str,
is_pinned_requirement,
is_url_requirement,
Expand All @@ -56,16 +51,17 @@ class PyPIRepository(BaseRepository):
"""

def __init__(self, pip_args, cache_dir, build_isolation=False):
self.build_isolation = build_isolation

# Use pip's parser for pip.conf management and defaults.
# General options (find_links, index_url, extra_index_url, trusted_host,
# and pre) are deferred to pip.
self.command = create_install_command()
self.command = create_command("install")
self.options, _ = self.command.parse_args(pip_args)
if self.options.cache_dir:
self.options.cache_dir = normalize_path(self.options.cache_dir)

self.options.build_isolation = build_isolation
self.options.require_hashes = False

self.session = self.command._build_session(self.options)
self.finder = self.command._build_package_finder(
options=self.options, session=self.session
Expand Down Expand Up @@ -135,123 +131,51 @@ def find_best_match(self, ireq, prereleases=None):
if not matching_candidates:
raise NoCandidateFound(ireq, all_candidates, self.finder)

if PIP_VERSION < (19, 1):
best_candidate = max(
matching_candidates, key=self.finder._candidate_sort_key
)
elif PIP_VERSION < (19, 2):
evaluator = self.finder.candidate_evaluator
best_candidate = evaluator.get_best_candidate(matching_candidates)
elif PIP_VERSION < (19, 3):
evaluator = self.finder.make_candidate_evaluator(ireq.name)
best_candidate = evaluator.get_best_candidate(matching_candidates)
else:
evaluator = self.finder.make_candidate_evaluator(ireq.name)
best_candidate_result = evaluator.compute_best_candidate(
matching_candidates
)
best_candidate = best_candidate_result.best_candidate

if PIP_VERSION[:2] <= (19, 3):
best_candidate_name = best_candidate.project
else:
best_candidate_name = best_candidate.name
evaluator = self.finder.make_candidate_evaluator(ireq.name)
best_candidate_result = evaluator.compute_best_candidate(matching_candidates)
best_candidate = best_candidate_result.best_candidate

# Turn the candidate into a pinned InstallRequirement
return make_install_requirement(
best_candidate_name,
best_candidate.name,
best_candidate.version,
ireq.extras,
constraint=ireq.constraint,
)

def resolve_reqs(self, download_dir, ireq, wheel_cache):
results = None

if PIP_VERSION < (10,):
reqset = RequirementSet(
self.build_dir,
self.source_dir,
with get_requirement_tracker() as req_tracker, TempDirectory(
kind="req-tracker"
) as temp_dir:
preparer = self.command.make_requirement_preparer(
temp_build_dir=temp_dir,
options=self.options,
req_tracker=req_tracker,
session=self.session,
finder=self.finder,
use_user_site=False,
download_dir=download_dir,
wheel_download_dir=self._wheel_download_dir,
session=self.session,
wheel_cache=wheel_cache,
)
results = reqset._prepare_file(self.finder, ireq)
else:
from pip._internal.operations.prepare import RequirementPreparer

preparer_kwargs = {
"build_dir": self.build_dir,
"src_dir": self.source_dir,
"download_dir": download_dir,
"wheel_download_dir": self._wheel_download_dir,
"progress_bar": "off",
"build_isolation": self.build_isolation,
}
resolver_kwargs = {
"finder": self.finder,
"session": self.session,
"upgrade_strategy": "to-satisfy-only",
"force_reinstall": False,
"ignore_dependencies": False,
"ignore_requires_python": False,
"ignore_installed": True,
"use_user_site": False,
}
make_install_req_kwargs = {"isolated": False, "wheel_cache": wheel_cache}

if PIP_VERSION < (19, 3):
resolver_kwargs.update(**make_install_req_kwargs)
else:
from pip._internal.req.constructors import install_req_from_req_string

make_install_req = partial(
install_req_from_req_string, **make_install_req_kwargs
)
resolver_kwargs["make_install_req"] = make_install_req

if PIP_VERSION >= (20,):
del resolver_kwargs["session"]
del preparer_kwargs["progress_bar"]

resolver = None
preparer = None

if PIP_VERSION[:2] <= (19, 3):
tmp_dir_cm = contextlib.nullcontext()
else:
from pip._internal.utils.temp_dir import TempDirectory

tmp_dir_cm = TempDirectory(kind="req-tracker")

with get_requirement_tracker() as req_tracker, tmp_dir_cm as temp_build_dir:
# Pip 18 uses a requirement tracker to prevent fork bombs
if req_tracker:
preparer_kwargs["req_tracker"] = req_tracker
reqset = RequirementSet()
ireq.is_direct = True
reqset.add_requirement(ireq)

if PIP_VERSION[:2] <= (19, 3):
preparer = RequirementPreparer(**preparer_kwargs)
else:
preparer = self.command.make_requirement_preparer(
temp_build_dir=temp_build_dir,
options=self.options,
req_tracker=req_tracker,
session=self.session,
finder=self.finder,
use_user_site=self.options.use_user_site,
)

resolver_kwargs["preparer"] = preparer
reqset = RequirementSet()
ireq.is_direct = True
reqset.add_requirement(ireq)

resolver = PipResolver(**resolver_kwargs)
resolver.require_hashes = False
results = resolver._resolve_one(reqset, ireq)
resolver = self.command.make_resolver(
preparer=preparer,
finder=self.finder,
options=self.options,
wheel_cache=wheel_cache,
use_user_site=False,
ignore_installed=True,
ignore_requires_python=False,
force_reinstall=False,
upgrade_strategy="to-satisfy-only",
)
results = resolver._resolve_one(reqset, ireq)

reqset.cleanup_files()
reqset.cleanup_files()

return set(results)

Expand All @@ -276,7 +200,7 @@ def get_dependencies(self, ireq):
# If a download_dir is passed, pip will unnecessarely
# archive the entire source directory
download_dir = None
elif ireq.link and is_vcs_url(ireq.link):
elif ireq.link and ireq.link.is_vcs:
# No download_dir for VCS sources. This also works around pip
# using git-checkout-index, which gets rid of the .git dir.
download_dir = None
Expand All @@ -301,9 +225,7 @@ def get_dependencies(self, ireq):
else:
del os.environ["PIP_REQ_TRACKER"]

# WheelCache.cleanup() introduced in pip==10.0.0
if PIP_VERSION >= (10,):
wheel_cache.cleanup()
wheel_cache.cleanup()
return self._dependencies_cache[ireq]

def get_hashes(self, ireq):
Expand All @@ -316,7 +238,7 @@ def get_hashes(self, ireq):
if ireq.link:
link = ireq.link

if is_vcs_url(link) or (is_file_url(link) and is_dir_url(link)):
if link.is_vcs or (link.is_file and link.is_existing_dir()):
# Return empty set for unhashable requirements.
# Unhashable logic modeled on pip's
# RequirementPreparer.prepare_linked_requirement
Expand Down Expand Up @@ -348,14 +270,8 @@ def get_hashes(self, ireq):

log.debug(" {}".format(ireq.name))

def get_candidate_link(candidate):
if PIP_VERSION < (19, 2):
return candidate.location
return candidate.link

return {
self._get_file_hash(get_candidate_link(candidate))
for candidate in matching_candidates
self._get_file_hash(candidate.link) for candidate in matching_candidates
}

def _get_file_hash(self, link):
Expand Down Expand Up @@ -423,7 +339,7 @@ def open_local_or_remote_file(link, session):
"""
url = link.url_without_fragment

if is_file_url(link):
if link.is_file:
# Local URL
local_path = url_to_path(url)
if os.path.isdir(local_path):
Expand Down
7 changes: 3 additions & 4 deletions piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import tempfile

from click.utils import safecall
from pip._internal.commands import create_command

from .. import click
from .._compat import install_req_from_line, parse_requirements
Expand All @@ -17,9 +18,7 @@
from ..resolver import Resolver
from ..utils import (
UNSAFE_PACKAGES,
create_install_command,
dedup,
get_trusted_hosts,
is_pinned_requirement,
key_from_ireq,
)
Expand All @@ -29,7 +28,7 @@
DEFAULT_REQUIREMENTS_OUTPUT_FILE = "requirements.txt"

# Get default values of the pip's options (including options from pip.conf).
install_command = create_install_command()
install_command = create_command("install")
pip_defaults = install_command.parser.get_default_values()


Expand Down Expand Up @@ -415,7 +414,7 @@ def cli(
generate_hashes=generate_hashes,
default_index_url=repository.DEFAULT_INDEX_URL,
index_urls=repository.finder.index_urls,
trusted_hosts=get_trusted_hosts(repository.finder),
trusted_hosts=repository.finder.trusted_hosts,
format_control=repository.finder.format_control,
allow_unsafe=allow_unsafe,
find_links=repository.finder.find_links,
Expand Down
Loading

0 comments on commit 50ecc4e

Please sign in to comment.