Permalink
Browse files

correctly listing out files that have changed

  • Loading branch information...
1 parent 28e083e commit 9539bb70ab8a9f22cef8daeb5b2720f5ddf6a4e5 @Swizec committed Apr 20, 2011
Showing with 70 additions and 13 deletions.
  1. +1 −0 .gitignore
  2. 0 __init__.py
  3. +60 −0 check_files.py
  4. +9 −13 get_list.py
View
1 .gitignore
@@ -0,0 +1 @@
+*.pyc
View
0 __init__.py
No changes.
View
60 check_files.py
@@ -15,3 +15,63 @@
# how to install everything? Mkae sure it works?
# needs git
# git is weird on windows ...
+
+import sys, hashlib, os
+import cPickle as pickle
+
+from get_list import files
+
+def checksum(path):
+ hash = hashlib.sha512()
+
+ with open(path, 'rb') as f:
+ for chunk in iter(lambda: f.read(128*hash.block_size), ''):
+ hash.update(chunk)
+ return hash.hexdigest()
+
+def load():
+ try:
+ return pickle.load(open('meta.p', 'rb'))
+ except IOError:
+ return {}
+
+def store(changed, old):
+ data = old
+
+ for f in changed:
+ data[f] = {'time': os.path.getmtime(f),
+ 'checksum': checksum(f)}
+
+ pickle.dump(data, open('meta.p', 'wb'))
+
+def changed(dir):
+ old_meta = load()
+
+ suspects = []
+
+ data = files(dir)
+ for path in data.keys():
+ try:
+ if data[path] != old_meta[path]['time']:
+ suspects.append(path)
+ except KeyError:
+ suspects.append(path)
+
+ def check(path):
+ try:
+ print path
+ return checksum(path) != old_meta[path]['checksum']
+ except KeyError:
+ return True
+
+ suspects = filter(check, suspects)
+
+ store(suspects, old_meta)
+
+ return suspects
+
+if __name__ == '__main__':
+ try:
+ print changed(sys.argv[1])
+ except IndexError:
+ print "You need to give a directory mate"
View
22 get_list.py
@@ -1,18 +1,14 @@
import os
-# kle namest tega chdira pac nastavis ta os. na zeljen direktorij,
-# in pol dobis v l-ju dictionary full pathov pa last modified cajtov
-os.chdir('test')
+def files(dir):
+ l = {}
-cwd = os.getcwd()
-l = {}
+ def izpis(bla,d,flst):
+ for f in flst:
+ fullf = os.path.join(d,f)
+ if os.path.isfile(fullf):
+ l[fullf]=os.path.getmtime(fullf)
-def izpis(bla,d,flst):
- for f in flst:
- fullf = os.path.join(d,f)
- if os.path.isfile(fullf):
- l[fullf]=os.path.getmtime(fullf)
+ os.path.walk(dir, izpis, None)
-os.path.walk(cwd,izpis,None)
-
-print l
+ return l

0 comments on commit 9539bb7

Please sign in to comment.