Created
January 9, 2018 21:38
-
-
Save NicolasGeraud/b8ec856df0f2c9924a543962e28a954c to your computer and use it in GitHub Desktop.
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
#!python2 | |
# -*- coding:utf-8 -*- | |
import getopt | |
import sys | |
import glob | |
import os | |
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() | |
def recursive_search(rootdir): | |
files = glob.glob("%s/**/*.zip" % rootdir) | |
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) | |
dirs = [f for f in os.listdir(rootdir) if os.path.isdir("%s/%s"% (rootdir, f))] | |
for d in dirs: | |
recursive_search("%s/%s" % (rootdir, d)) | |
if __name__ == "__main__" : | |
parse_args() | |
recursive_search(workdir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment