Skip to content

Commit

Permalink
Postpone the import of pip_shims
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming committed Nov 4, 2021
1 parent c04faf2 commit 76a80fb
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions pipenv/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from click import echo as click_echo

from pipenv import environments
from pipenv._compat import DEFAULT_ENCODING
from pipenv.exceptions import (
PipenvCmdError, PipenvUsageError, RequirementError, ResolutionFailure
)
Expand All @@ -37,7 +36,6 @@
)
from pipenv.vendor.vistir.misc import fs_str, run
from pipenv.vendor.vistir.contextmanagers import open_file
from pipenv.vendor.pip_shims import shims


if environments.MYPY_RUNNING:
Expand Down Expand Up @@ -372,7 +370,7 @@ def get_pipenv_sitedir():
return None


class HashCache(shims.SafeFileCache):
class HashCacheMixin:

"""Caches hashes of PyPI artifacts so we do not need to re-download them.
Expand All @@ -384,7 +382,7 @@ def __init__(self, directory, session):
self.session = session
if not os.path.isdir(directory):
os.makedirs(directory, exist_ok=True)
super(HashCache, self).__init__(directory=directory)
super().__init__(directory=directory)

def get_hash(self, link):
# If there is no link hash (i.e., md5, sha256, etc.), we don't want
Expand All @@ -396,6 +394,8 @@ def get_hash(self, link):
return hash_value.decode("utf8")

def _get_file_hash(self, link):
from pipenv.vendor.pip_shims import shims

h = hashlib.new(shims.FAVORITE_HASH)
with open_file(link.url, self.session) as fp:
for chunk in iter(lambda: fp.read(8096), b""):
Expand Down Expand Up @@ -432,7 +432,7 @@ def __init__(
self._pip_options = None
self._pip_command = None
self._retry_attempts = 0
self._hash_cache = HashCache(os.path.join(project.s.PIPENV_CACHE_DIR, "hashes"), self.session)
self._hash_cache = None

def __repr__(self):
return (
Expand All @@ -443,8 +443,20 @@ def __repr__(self):
@staticmethod
@lru_cache()
def _get_pip_command():
from pipenv.vendor.pip_shims import shims

return shims.InstallCommand()

@property
def hash_cache(self):
from pipenv.vendor.pip_shims import shims

if not self._hash_cache:
self._hash_cache = type("HashCache", (HashCacheMixin, shims.SafeFileHash), {})(
os.path.join(self.project.s.PIPENV_CACHE_DIR, "hashes"), self.session
)
return self._hash_cache

@classmethod
def get_metadata(
cls,
Expand Down Expand Up @@ -788,7 +800,7 @@ def session(self):

@property
def finder(self):

from pipenv.vendor.pip_shims import shims
if self._finder is None:
self._finder = shims.get_package_finder(
install_cmd=self.pip_command,
Expand All @@ -799,6 +811,8 @@ def finder(self):

@property
def parsed_constraints(self):
from pipenv.vendor.pip_shims import shims

if self._parsed_constraints is None:
self._parsed_constraints = shims.parse_requirements(
self.constraint_file, finder=self.finder, session=self.session,
Expand Down Expand Up @@ -896,6 +910,7 @@ def prepend_hash_types(cls, checksums, hash_type):
return cleaned_checksums

def _get_hashes_from_pypi(self, ireq):
from pipenv.vendor.pip_shims import shims

pkg_url = f"https://pypi.org/pypi/{ireq.name}/json"
session = _get_requests_session(self.project.s.PIPENV_MAX_RETRIES)
Expand Down Expand Up @@ -959,11 +974,12 @@ def resolve_hashes(self):
return self.hashes

def _get_hash_from_link(self, link):
from pipenv.vendor.pip_shims import shims

if link.hash and link.hash_name == shims.FAVORITE_HASH:
return f"{link.hash_name}:{link.hash}"

return self._hash_cache.get(link)
return self.hash_cache.get(link)

def _clean_skipped_result(self, req, value):
ref = None
Expand Down Expand Up @@ -1544,6 +1560,8 @@ def is_file(package):
def pep440_version(version):
"""Normalize version to PEP 440 standards"""
# Use pip built-in version parser.
from pipenv.vendor.pip_shims import shims

return str(shims.parse_version(version))


Expand Down

0 comments on commit 76a80fb

Please sign in to comment.