Skip to content

Instantly share code, notes, and snippets.

@bryanjhv
Last active December 9, 2020 02:13
Show Gist options
  • Save bryanjhv/d0bb06f205bb74a19a1c0481d845f211 to your computer and use it in GitHub Desktop.
Save bryanjhv/d0bb06f205bb74a19a1c0481d845f211 to your computer and use it in GitHub Desktop.
import os
import sys
import os.path
import hashlib
base = sys.argv[1]
if not os.path.exists(base):
sys.exit(1)
dirs = []
files = []
for root, _dirs, _files in os.walk(base):
for dir in _dirs:
dirs.append(os.path.join(root, dir)[len(base) + 1 :])
for file in _files:
files.append(os.path.join(root, file)[len(base) + 1 :])
dirs.sort()
files.sort()
sep = b"\0"
hash = hashlib.sha1()
for dir in dirs:
hash.update(dir.encode())
hash.update(sep)
for file in files:
with open(os.path.join(base, file), "rb") as f:
hash.update(file.encode())
hash.update(f.read())
hash.update(sep)
print(hash.hexdigest())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment