Skip to content

Commit

Permalink
Fix hash when Program.source_files contains a subdirectory (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
mzuenni authored Feb 16, 2024
1 parent 7b52afd commit d3a33ca
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
4 changes: 4 additions & 0 deletions bin/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,10 @@ def build(self, bar):
for f in self.source_files:
ensure_symlink(self.tmpdir / f.name, f)
self.input_files.append(self.tmpdir / f.name)
if not f.is_file():
self.ok = False
self.bar.error(f'{str(f)} is not a file')
return False
hashes.append(hash_file(f))
self.hash = combine_hashes(hashes)

Expand Down
46 changes: 30 additions & 16 deletions bin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import hashlib
import tempfile
import yaml as yamllib
import errno

from enum import Enum
from pathlib import Path
Expand Down Expand Up @@ -1000,15 +1001,36 @@ def inc_label(label):
return 'A' + label


def combine_hashes(values):
values.sort()
hasher = hashlib.sha256(usedforsecurity=False)
for item in values:
hasher.update(item.encode())
return hasher.hexdigest()


def combine_hashes_dict(d):
hasher = hashlib.sha256(usedforsecurity=False)
for key in d:
hasher.update(key.encode())
if d[key] is not None:
hasher.update(d[key].encode())
return hasher.hexdigest()


def hash_string(string):
sha = hashlib.sha256(usedforsecurity=False)
sha.update(string.encode())
return sha.hexdigest()


def hash_file(file, buffer_size=65536):
assert file.is_file(), f"File {file} does not exist"
if not file.is_file():
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), str(file))
sha = hashlib.sha256(usedforsecurity=False)
name = file.name.encode('utf-8')
sha.update(len(name).to_bytes(8))
sha.update(name)

with open(file, 'rb') as f:
while True:
Expand All @@ -1020,18 +1042,10 @@ def hash_file(file, buffer_size=65536):
return sha.hexdigest()


def combine_hashes(list):
list.sort()
hasher = hashlib.sha256(usedforsecurity=False)
for item in list:
hasher.update(item.encode())
return hasher.hexdigest()


def combine_hashes_dict(d):
hasher = hashlib.sha256(usedforsecurity=False)
for key in d:
hasher.update(key.encode())
if d[key] is not None:
hasher.update(d[key].encode())
return hasher.hexdigest()
def hash_file_or_dir(file_or_dir, buffer_size=65536):
if file_or_dir.is_dir():
return combine_hashes(
[hash_string(file_or_dir.name)] + [hash_file_or_dir(f) for f in file_or_dir.iterdir()]
)
else:
return hash_file(file_or_dir)

0 comments on commit d3a33ca

Please sign in to comment.