From e6d4f66c28b6419acf61b38fb8c63939038e9cf1 Mon Sep 17 00:00:00 2001 From: Magdalena Banwinkler Date: Sun, 21 Aug 2022 21:29:19 +0200 Subject: [PATCH] Fixes to the validation script --- first_validation.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/first_validation.py b/first_validation.py index cfbd543..f7d26e8 100644 --- a/first_validation.py +++ b/first_validation.py @@ -30,8 +30,9 @@ def hash_for_fname(fname): """ # Convert a string filename to a Path object. fpath = Path(fname) - # Your code here. - return 'not-really-the-hash' + contents = fpath.read_bytes() + hash_value = sha1(contents).hexdigest() + return hash_value # Fill in the function above to make the test below pass. @@ -40,7 +41,6 @@ def hash_for_fname(fname): exp_hash = '7fa09f0f0dc11836094b8d360dc63943704796a1' assert calc_hash == exp_hash, f'{calc_hash} does not match {exp_hash}' - def check_hashes(hash_fname): """ Check hashes and filenames in given in file `hash_fname` """ @@ -48,13 +48,19 @@ def check_hashes(hash_fname): # Directory containing hash filenames file. data_dir = hash_pth.parent # Read in text for hash filename + hash_text = hash_pth.read_text() # Split into lines. # For each line: + for line in hash_text.splitlines(): # Split each line into expected_hash and filename + fhash, fname = line.split() # Calculate actual hash for given filename. + pth = data_dir / fname # Check actual hash against expected hash # Return False if any of the hashes do not match. - return False + if not hash_for_fname(pth) == fhash: + return False + return True assert check_hashes(hashes_pth), 'Check hash list does not return True'