2023-01-13 23:56:59 -07:00
|
|
|
import hashlib
|
|
|
|
import json
|
|
|
|
import os.path
|
|
|
|
|
|
|
|
import filelock
|
|
|
|
|
|
|
|
|
|
|
|
cache_filename = "cache.json"
|
|
|
|
cache_data = None
|
|
|
|
|
|
|
|
|
|
|
|
def dump_cache():
|
|
|
|
with filelock.FileLock(cache_filename+".lock"):
|
|
|
|
with open(cache_filename, "w", encoding="utf8") as file:
|
|
|
|
json.dump(cache_data, file, indent=4)
|
|
|
|
|
|
|
|
|
|
|
|
def cache(subsection):
|
|
|
|
global cache_data
|
|
|
|
|
|
|
|
if cache_data is None:
|
|
|
|
with filelock.FileLock(cache_filename+".lock"):
|
|
|
|
if not os.path.isfile(cache_filename):
|
|
|
|
cache_data = {}
|
|
|
|
else:
|
|
|
|
with open(cache_filename, "r", encoding="utf8") as file:
|
|
|
|
cache_data = json.load(file)
|
|
|
|
|
|
|
|
s = cache_data.get(subsection, {})
|
|
|
|
cache_data[subsection] = s
|
|
|
|
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
|
|
def calculate_sha256(filename):
|
|
|
|
hash_sha256 = hashlib.sha256()
|
|
|
|
|
|
|
|
with open(filename, "rb") as f:
|
|
|
|
for chunk in iter(lambda: f.read(4096), b""):
|
|
|
|
hash_sha256.update(chunk)
|
|
|
|
|
|
|
|
return hash_sha256.hexdigest()
|
|
|
|
|
|
|
|
|
2023-01-14 05:55:40 -07:00
|
|
|
def sha256_from_cache(filename, title):
|
2023-01-13 23:56:59 -07:00
|
|
|
hashes = cache("hashes")
|
|
|
|
ondisk_mtime = os.path.getmtime(filename)
|
|
|
|
|
2023-01-14 05:55:40 -07:00
|
|
|
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")
|
2023-01-13 23:56:59 -07:00
|
|
|
|
2023-01-14 05:55:40 -07:00
|
|
|
sha256_value = sha256_from_cache(filename, title)
|
|
|
|
if sha256_value is not None:
|
|
|
|
return sha256_value
|
2023-01-13 23:56:59 -07:00
|
|
|
|
|
|
|
print(f"Calculating sha256 for {filename}: ", end='')
|
|
|
|
sha256_value = calculate_sha256(filename)
|
|
|
|
print(f"{sha256_value}")
|
|
|
|
|
|
|
|
hashes[title] = {
|
2023-01-14 05:55:40 -07:00
|
|
|
"mtime": os.path.getmtime(filename),
|
2023-01-13 23:56:59 -07:00
|
|
|
"sha256": sha256_value,
|
|
|
|
}
|
|
|
|
|
|
|
|
dump_cache()
|
|
|
|
|
|
|
|
return sha256_value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|