Skip to content

Commit

Permalink
Change usage to reuse the cache for the hash computation.
Browse files Browse the repository at this point in the history
  • Loading branch information
vphilippon committed Sep 28, 2017
1 parent a984fdf commit 81cf817
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 8 deletions.
8 changes: 8 additions & 0 deletions piptools/repositories/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
unicode_literals)

from abc import ABCMeta, abstractmethod
from contextlib import contextmanager

from six import add_metaclass

Expand Down Expand Up @@ -38,3 +39,10 @@ def get_hashes(self, ireq):
all of the files for a given requirement. It is not acceptable for an
editable or unpinned requirement to be passed to this function.
"""

@abstractmethod
@contextmanager
def allow_all_wheels(self):
"""
Monkey patches pip.Wheel to allow wheels from all platforms and Python versions.
"""
7 changes: 7 additions & 0 deletions piptools/repositories/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from __future__ import (absolute_import, division, print_function,
unicode_literals)

from contextlib import contextmanager

from piptools.utils import as_tuple, key_from_req, make_install_requirement
from .base import BaseRepository

Expand Down Expand Up @@ -63,3 +65,8 @@ def get_dependencies(self, ireq):

def get_hashes(self, ireq):
return self.repository.get_hashes(ireq)

@contextmanager
def allow_all_wheels(self):
with self.repository.allow_all_wheels():
yield
11 changes: 5 additions & 6 deletions piptools/repositories/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,11 @@ 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
# 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]]
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 Down
3 changes: 2 additions & 1 deletion piptools/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ def resolve_hashes(self, ireqs):
"""
Finds acceptable hashes for all of the given InstallRequirements.
"""
return {ireq: self.repository.get_hashes(ireq) for ireq in ireqs}
with self.repository.allow_all_wheels():
return {ireq: self.repository.get_hashes(ireq) for ireq in ireqs}

def resolve(self, max_rounds=10):
"""
Expand Down
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from contextlib import contextmanager
from functools import partial

from pip._vendor.packaging.version import Version
Expand Down Expand Up @@ -49,6 +50,11 @@ def get_dependencies(self, ireq):
dependencies = [dep for extra in extras for dep in self.index[name][version][extra]]
return [InstallRequirement.from_line(dep, constraint=ireq.constraint) for dep in dependencies]

@contextmanager
def allow_all_wheels(self):
# No need to do an actual pip.Wheel mock here.
yield


class FakeInstalledDistribution(object):
def __init__(self, line, deps=None):
Expand Down
3 changes: 2 additions & 1 deletion tests/test_repository_pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ def test_generate_hashes_all_platforms(from_line):
session = pip_command._build_session(pip_options)
repository = PyPIRepository(pip_options, session)
ireq = from_line('cffi==1.9.1')
assert repository.get_hashes(ireq) == expected
with repository.allow_all_wheels():
assert repository.get_hashes(ireq) == expected


def test_generate_hashes_without_interfering_with_each_other(from_line):
Expand Down

0 comments on commit 81cf817

Please sign in to comment.