Skip to content

Commit

Permalink
Ignore local hashes with --no-reuse-hashes (#1177)
Browse files Browse the repository at this point in the history
  • Loading branch information
graingert committed Jul 20, 2020
1 parent d7db32a commit 9eb10d7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
8 changes: 5 additions & 3 deletions piptools/repositories/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class LocalRequirementsRepository(BaseRepository):
PyPI. This keeps updates to the requirements.txt down to a minimum.
"""

def __init__(self, existing_pins, proxied_repository):
def __init__(self, existing_pins, proxied_repository, reuse_hashes=True):
self._reuse_hashes = reuse_hashes
self.repository = proxied_repository
self.existing_pins = existing_pins

Expand Down Expand Up @@ -74,8 +75,9 @@ def get_dependencies(self, ireq):
return self.repository.get_dependencies(ireq)

def get_hashes(self, ireq):
key = key_from_ireq(ireq)
existing_pin = self.existing_pins.get(key)
existing_pin = self._reuse_hashes and self.existing_pins.get(
key_from_ireq(ireq)
)
if existing_pin and ireq_satisfied_by_existing_pin(ireq, existing_pin):
if PIP_VERSION[:2] <= (20, 0):
hashes = existing_pin.options.get("hashes", {})
Expand Down
14 changes: 13 additions & 1 deletion piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@ def has_arg(self, arg_name):
default=False,
help="Generate pip 8 style hashes in the resulting requirements file.",
)
@click.option(
"--reuse-hashes/--no-reuse-hashes",
is_flag=True,
default=True,
help=(
"Improve the speed of --generate-hashes by reusing the hashes from an "
"existing output file."
),
)
@click.option(
"--max-rounds",
default=10,
Expand Down Expand Up @@ -241,6 +250,7 @@ def cli(
output_file,
allow_unsafe,
generate_hashes,
reuse_hashes,
src_files,
max_rounds,
build_isolation,
Expand Down Expand Up @@ -360,7 +370,9 @@ def cli(
existing_pins_to_upgrade.add(key)
else:
existing_pins[key] = ireq
repository = LocalRequirementsRepository(existing_pins, repository)
repository = LocalRequirementsRepository(
existing_pins, repository, reuse_hashes=reuse_hashes
)

###
# Parsing/collecting initial requirements
Expand Down
23 changes: 23 additions & 0 deletions tests/test_repository_local.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import copy

import pytest

from tests.conftest import FakeRepository

from piptools.repositories.local import LocalRequirementsRepository
Expand Down Expand Up @@ -29,6 +31,27 @@ def test_get_hashes_local_repository_cache_hit(from_line, repository):
assert hashes == EXPECTED


NONSENSE = {"sha256:NONSENSE"}


@pytest.mark.parametrize(
("reuse_hashes", "expected"), ((True, NONSENSE), (False, EXPECTED))
)
def test_toggle_reuse_hashes_local_repository(
pip_conf, from_line, pypi_repository, reuse_hashes, expected
):
# Create an install requirement with the hashes included in its options
options = {"hashes": {"sha256": [entry.split(":")[1] for entry in NONSENSE]}}
req = from_line("small-fake-a==0.1", options=options)
existing_pins = {name_from_req(req): req}

local_repository = LocalRequirementsRepository(
existing_pins, pypi_repository, reuse_hashes=reuse_hashes
)
with local_repository.allow_all_wheels():
assert local_repository.get_hashes(from_line("small-fake-a==0.1")) == expected


class FakeRepositoryChecksForCopy(FakeRepository):
def __init__(self):
super(FakeRepositoryChecksForCopy, self).__init__()
Expand Down

0 comments on commit 9eb10d7

Please sign in to comment.