Created
June 29, 2019 23:30
-
-
Save daragh/579458cd966e415d0e325b43007ce439 to your computer and use it in GitHub Desktop.
Progressive sha1sum in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import os | |
import hashlib | |
from argparse import ArgumentParser | |
def main(args): | |
parser = ArgumentParser() | |
group = parser.add_mutually_exclusive_group(required=True) | |
group.add_argument('--create', metavar='PATH', help='Create manifest with paths from stdin') | |
group.add_argument('--update', metavar='PATH') | |
parsed = parser.parse_args() | |
stdin = getattr(sys.stdin, 'buffer', sys.stdin) | |
stdout = getattr(sys.stdout, 'buffer', sys.stdout) | |
blank = b' ' * 42 | |
if parsed.create: | |
assert not os.path.exists(parsed.create) | |
with open(parsed.create, 'wb') as f: | |
f.seek(0, 2) | |
for line in stdin: | |
f.write(blank) | |
f.write(line) | |
if parsed.update: | |
with open(parsed.update, 'r+b') as f: | |
while True: | |
line = f.readline() | |
if not line: | |
break | |
if line.startswith(blank): | |
path = line[len(blank):-len(b'\n')] | |
sha1 = sha1sum(path) | |
f.seek(-len(line), 1) | |
f.write(sha1.encode()) | |
f.seek(-len(sha1), 1) | |
stdout.write(f.readline()) | |
def sha1sum(path): | |
sha1 = hashlib.sha1() | |
with open(path, 'rb') as f: | |
for n in iter(lambda: f.read(1024 ** 2), b''): | |
sha1.update(n) | |
return sha1.hexdigest() | |
if __name__ == '__main__': main(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment