Skip to content

Instantly share code, notes, and snippets.

@NicolasGeraud
Last active January 9, 2018 21:37
Show Gist options
  • Save NicolasGeraud/3db76823e84df883bd08b663d8c68071 to your computer and use it in GitHub Desktop.
Save NicolasGeraud/3db76823e84df883bd08b663d8c68071 to your computer and use it in GitHub Desktop.
generate md5 and sha1 for all zip in a directory
#!python3
# -*- coding:utf-8 -*-
import getopt
import sys
import glob
import os.path
import hashlib
workdir = None
def parse_args():
global workdir
try:
opts, args = getopt.getopt(sys.argv[1:], "", ["workdir="])
except getopt.GetoptError:
sys.exit(1)
for opt, arg in opts:
if opt in ('--workdir'):
workdir = arg
if workdir is None:
print("must define workdir:")
print(" --workdir=/path/to/root/directory")
sys.exit(1)
def generate_hash(algorithm, zipfile, hashfile):
print("generate %s" % hashfile)
with open(zipfile, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
algorithm.update(chunk)
f = open(hashfile, "w")
f.write(algorithm.hexdigest())
f.close()
if __name__ == "__main__" :
parse_args()
files = glob.glob("%s/**/*.zip" % workdir, recursive=True)
for file in files:
md5file = "%s.md5" % file
sha1file = "%s.sha1" % file
if not os.path.isfile(md5file):
generate_hash(hashlib.md5(), file, md5file)
if not os.path.isfile(sha1file):
generate_hash(hashlib.sha1(), file, sha1file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment