Skip to content

Instantly share code, notes, and snippets.

@1328
Created June 5, 2014 16:53
Show Gist options
  • Select an option

  • Save 1328/e10bbc2eb8b8a66f6335 to your computer and use it in GitHub Desktop.

Select an option

Save 1328/e10bbc2eb8b8a66f6335 to your computer and use it in GitHub Desktop.
Comments 6-5
import os.path
from hashlib import md5
from pprint import pprint
# spacing is a bit wacked, should be 4 spaces per tab
def get_files():
# let's make these generators
for f in os.listdir('.'):
if os.path.isfile(f) and f != "manifest.txt":
yield f
def get_files_recursive():
# this too
for (path, dirs, files) in os.walk('.'):
for f in files:
if f != "manifest.txt":
yield (os.path.join(path,f))
def calc_hash(fname):
m = md5()
data = "tmp"
with open(fname,'rb') as f:
while len(data) > 0:
data = f.read(512000)
m.update(data)
return m.hexdigest()
def output(data):
x=0
with open('manifest.txt','w') as f:
# don't use hash -- its reserved
for fn in sorted(data.keys()):
f.write("%s %s\n" % (fn,data[fn]))
x+=1 # could use enumerate here if you wanted
def read_manifest():
if not os.path.isfile('manifest.txt'):
# let's make this an exception
raise Exception("manifest.txt does not exist.")
flist = {}
with open('manifest.txt','r') as f:
for line in f:
# let's use rpartition
# that way names can have spaces
fn, _, h = line.rpartition(' ')
flist[fn.strip()] = h.strip()
return flist
def check_manifest():
# a little strange since this only looks at the files that then existed
# and not any adds to the directory,
# but I guess that is what you wanted?
changed = {}
# changed to dict
flist = read_manifest()
for fn in flist.keys():
oldh = flist[fn]
newh = calc_hash(fn)
if newh != oldh:
changed[fn] = newh
# i'd return changed and let the calling code figure it out
return changed
def build_manifest(recursive = False):
# let's use a dict
# that way we don't have to worry about it being matchy matchy
hlist = {}
# hacked up since I am not running with args,
# but I would do the parsing in main anyway
if recursive:
# see what I do here
flist = get_files_recursive
else:
flist = get_files
for fn in flist():
hlist[fn] = calc_hash(fn)
output(hlist)
def main():
# put parsing in here maybe
pprint(check_manifest())
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment