Skip to content

Commit

Permalink
Keep pip.Wheel monkey patch only in the get_hashes context.
Browse files Browse the repository at this point in the history
  • Loading branch information
vphilippon committed Sep 28, 2017
1 parent aecb213 commit a984fdf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 1.10.2 (UNRELEASED)

Bug Fixes:
- Fixed bug causing dependencies from invalid wheels for the current platform to be included ([#571](https://github.com/jazzband/pip-tools/pull/571)).

# 1.10.1 (2017-09-27)

Bug Fixes:
Expand Down
62 changes: 38 additions & 24 deletions piptools/repositories/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,6 @@
from .._compat import TemporaryDirectory


# Monkey patch pip's Wheel class to support all platform tags. This allows
# pip-tools to generate hashes for all available distributions, not only the
# one for the current platform.

def _wheel_supported(self, tags=None):
# Ignore current platform. Support everything.
return True


def _wheel_support_index_min(self, tags=None):
# All wheels are equal priority for sorting.
return 0


Wheel.supported = _wheel_supported
Wheel.support_index_min = _wheel_support_index_min


class PyPIRepository(BaseRepository):
DEFAULT_INDEX_URL = 'https://pypi.python.org/simple'

Expand Down Expand Up @@ -182,12 +164,13 @@ def get_hashes(self, ireq):

# We need to get all of the candidates that match our current version
# pin, these will represent all of the files that could possibly
# satisify this constraint.
all_candidates = self.find_all_candidates(ireq.name)
candidates_by_version = lookup_table(all_candidates, key=lambda c: c.version)
matching_versions = list(
ireq.specifier.filter((candidate.version for candidate in all_candidates)))
matching_candidates = candidates_by_version[matching_versions[0]]
# satisfy this constraint.
with self.allow_all_wheels():
all_candidates = self.find_all_candidates(ireq.name)
candidates_by_version = lookup_table(all_candidates, key=lambda c: c.version)
matching_versions = list(
ireq.specifier.filter((candidate.version for candidate in all_candidates)))
matching_candidates = candidates_by_version[matching_versions[0]]

return {
self._get_file_hash(candidate.location)
Expand All @@ -201,6 +184,37 @@ def _get_file_hash(self, location):
h.update(chunk)
return ":".join([FAVORITE_HASH, h.hexdigest()])

@contextmanager
def allow_all_wheels(self):
"""
Monkey patches pip.Wheel to allow wheels from all platforms and Python versions.
This also saves the candidate cache and set a new one, or else the results from the
previous non-patched calls will interfere.
"""
def _wheel_supported(self, tags=None):
# Ignore current platform. Support everything.
return True

def _wheel_support_index_min(self, tags=None):
# All wheels are equal priority for sorting.
return 0

original_wheel_supported = Wheel.supported
original_support_index_min = Wheel.support_index_min
original_cache = self._available_candidates_cache

Wheel.supported = _wheel_supported
Wheel.support_index_min = _wheel_support_index_min
self._available_candidates_cache = {}

try:
yield
finally:
Wheel.supported = original_wheel_supported
Wheel.support_index_min = original_support_index_min
self._available_candidates_cache = original_cache


@contextmanager
def open_local_or_remote_file(link, session):
Expand Down

0 comments on commit a984fdf

Please sign in to comment.