Skip to content

Commit

Permalink
The resolve module now computes a sha256 sum if the link doesn't alre…
Browse files Browse the repository at this point in the history
…ady have one

Resolves #22
  • Loading branch information
apt-itude committed Jun 12, 2019
1 parent a0b2122 commit 6b0e8a2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/piprules/resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,12 @@ def _create_resolved_requirement(self, requirement):

link = requirement.link
source = ResolvedRequirementSource(link.url_without_fragment)
if link.hash:
# TODO this assumes the hash is sha256
source.sha256 = link.hash

source.sha256 = (
link.hash
if link.hash and link.hash_name == "sha256"
else self._compute_sha256_sum(requirement)
)

return ResolvedRequirement(
pipcompat.canonicalize_name(requirement.name),
Expand Down Expand Up @@ -199,6 +202,11 @@ def _set_link_to_local_wheel(self, requirement):
session=self._session,
)

def _compute_sha256_sum(self, requirement):
LOG.debug(f"Computing sha256 sum for {requirement.name}")
temp_wheel_path = _find_wheel(self._work_dirs.wheel, requirement.name)
return util.compute_file_hash(temp_wheel_path)


def _find_wheel(directory, name):
canon_name = pipcompat.canonicalize_name(name)
Expand Down
16 changes: 15 additions & 1 deletion src/piprules/util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import errno
import contextlib
import errno
import hashlib
import itertools
import os

Expand Down Expand Up @@ -42,3 +43,16 @@ def prepend_to_pythonpath(paths):
def full_groupby(iterable, key=None):
"""Like itertools.groupby(), but sorts the input on the group key first."""
return itertools.groupby(sorted(iterable, key=key), key=key)


def compute_file_hash(path, algorithm="sha256"):
hasher = hashlib.new(algorithm)
block_size = 4096

with open(path, mode='rb') as file_:
buf = file_.read(block_size)
while buf:
hasher.update(buf)
buf = file_.read(block_size)

return hasher.hexdigest()

0 comments on commit 6b0e8a2

Please sign in to comment.