diff options
Diffstat (limited to 'modules/hashes.py')
-rw-r--r-- | modules/hashes.py | 29 |
1 files changed, 21 insertions, 8 deletions
diff --git a/modules/hashes.py b/modules/hashes.py index ebfbd90c..b85a7580 100644 --- a/modules/hashes.py +++ b/modules/hashes.py @@ -34,31 +34,44 @@ def cache(subsection): def calculate_sha256(filename):
hash_sha256 = hashlib.sha256()
+ blksize = 1024 * 1024
with open(filename, "rb") as f:
- for chunk in iter(lambda: f.read(4096), b""):
+ for chunk in iter(lambda: f.read(blksize), b""):
hash_sha256.update(chunk)
return hash_sha256.hexdigest()
-def sha256(filename, title):
+def sha256_from_cache(filename, title):
hashes = cache("hashes")
ondisk_mtime = os.path.getmtime(filename)
- if title in hashes:
- cached_sha256 = hashes[title].get("sha256", None)
- cached_mtime = hashes[title].get("mtime", 0)
+ if title not in hashes:
+ return None
+
+ cached_sha256 = hashes[title].get("sha256", None)
+ cached_mtime = hashes[title].get("mtime", 0)
+
+ if ondisk_mtime > cached_mtime or cached_sha256 is None:
+ return None
+
+ return cached_sha256
+
+
+def sha256(filename, title):
+ hashes = cache("hashes")
- if ondisk_mtime <= cached_mtime and cached_sha256 is not None:
- return cached_sha256
+ sha256_value = sha256_from_cache(filename, title)
+ if sha256_value is not None:
+ return sha256_value
print(f"Calculating sha256 for {filename}: ", end='')
sha256_value = calculate_sha256(filename)
print(f"{sha256_value}")
hashes[title] = {
- "mtime": ondisk_mtime,
+ "mtime": os.path.getmtime(filename),
"sha256": sha256_value,
}
|