Last active
December 20, 2015 09:48
-
-
Save rochacon/6110123 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
# /usr/bin/time -f '%E - %P%% cpu - %M kbytes ram' python tmp/map.py ~/Desktop >tmp/map.json | |
import hashlib | |
import json | |
import os | |
import sys | |
def get_file_hash(path): | |
h = hashlib.sha256() | |
with open(path, 'rb') as fp: | |
for chunk in iter(lambda: fp.read(131072), ''): | |
h.update(chunk) | |
return h.hexdigest() | |
everything = [] | |
for volume in sorted(sys.argv[1:]): | |
for root, dirs, files in os.walk(os.path.expanduser(volume)): | |
for path in sorted(dirs + files): | |
fullpath = os.path.join(root, path) | |
if os.path.isdir(fullpath): | |
everything.append({ | |
"path":fullpath, | |
"perms":oct(os.stat(fullpath).st_mode), | |
"type":"tree" | |
}) | |
else: | |
everything.append({ | |
"path":fullpath, | |
"perms":oct(os.stat(fullpath).st_mode), | |
"size":os.path.getsize(fullpath), | |
"sha256":get_file_hash(fullpath), | |
"type":"blob" | |
}) | |
print json.dumps(everything, sort_keys=True, separators=(',', ':')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment