##// END OF EJS Templates
Update md5 calls.
Thomas Kluyver -
Show More
@@ -1,92 +1,92 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 """ A script/util to upgrade all files in a directory
2 """ A script/util to upgrade all files in a directory
3
3
4 This is rather conservative in its approach, only copying/overwriting
4 This is rather conservative in its approach, only copying/overwriting
5 new and unedited files.
5 new and unedited files.
6
6
7 To be used by "upgrade" feature.
7 To be used by "upgrade" feature.
8 """
8 """
9 try:
9 try:
10 from IPython.external.path import path
10 from IPython.external.path import path
11 except ImportError:
11 except ImportError:
12 from path import path
12 from path import path
13
13
14 import md5, pickle
14 import hashlib, pickle
15
15
16 def showdiff(old,new):
16 def showdiff(old,new):
17 import difflib
17 import difflib
18 d = difflib.Differ()
18 d = difflib.Differ()
19 lines = d.compare(old.lines(),new.lines())
19 lines = d.compare(old.lines(),new.lines())
20 realdiff = False
20 realdiff = False
21 for l in lines:
21 for l in lines:
22 print l,
22 print l,
23 if not realdiff and not l[0].isspace():
23 if not realdiff and not l[0].isspace():
24 realdiff = True
24 realdiff = True
25 return realdiff
25 return realdiff
26
26
27 def upgrade_dir(srcdir, tgtdir):
27 def upgrade_dir(srcdir, tgtdir):
28 """ Copy over all files in srcdir to tgtdir w/ native line endings
28 """ Copy over all files in srcdir to tgtdir w/ native line endings
29
29
30 Creates .upgrade_report in tgtdir that stores md5sums of all files
30 Creates .upgrade_report in tgtdir that stores md5sums of all files
31 to notice changed files b/w upgrades.
31 to notice changed files b/w upgrades.
32 """
32 """
33
33
34 def pr(s):
34 def pr(s):
35 print s
35 print s
36 junk = ['.svn','ipythonrc*','*.pyc', '*.pyo', '*~', '.hg']
36 junk = ['.svn','ipythonrc*','*.pyc', '*.pyo', '*~', '.hg']
37
37
38 def ignorable(p):
38 def ignorable(p):
39 for pat in junk:
39 for pat in junk:
40 if p.startswith(pat) or p.fnmatch(pat):
40 if p.startswith(pat) or p.fnmatch(pat):
41 return True
41 return True
42 return False
42 return False
43
43
44 modded = []
44 modded = []
45 files = [path(srcdir).relpathto(p) for p in path(srcdir).walkfiles()]
45 files = [path(srcdir).relpathto(p) for p in path(srcdir).walkfiles()]
46 #print files
46 #print files
47 rep = tgtdir / '.upgrade_report'
47 rep = tgtdir / '.upgrade_report'
48 try:
48 try:
49 rpt = pickle.load(rep.open())
49 rpt = pickle.load(rep.open())
50 except:
50 except:
51 rpt = {}
51 rpt = {}
52
52
53 for f in files:
53 for f in files:
54 if ignorable(f):
54 if ignorable(f):
55 continue
55 continue
56 src = srcdir / f
56 src = srcdir / f
57 tgt = tgtdir / f
57 tgt = tgtdir / f
58 if not tgt.isfile():
58 if not tgt.isfile():
59 pr("Creating %s" % str(tgt))
59 pr("Creating %s" % str(tgt))
60
60
61 tgt.write_text(src.text())
61 tgt.write_text(src.text())
62 rpt[str(tgt)] = md5.new(tgt.text()).hexdigest()
62 rpt[str(tgt)] = hashlib.md5(tgt.text()).hexdigest()
63 else:
63 else:
64 cont = tgt.text()
64 cont = tgt.text()
65 sum = rpt.get(str(tgt), None)
65 sum = rpt.get(str(tgt), None)
66 #print sum
66 #print sum
67 if sum and md5.new(cont).hexdigest() == sum:
67 if sum and hashlib.md5(cont).hexdigest() == sum:
68 pr("%s: Unedited, installing new version" % tgt)
68 pr("%s: Unedited, installing new version" % tgt)
69 tgt.write_text(src.text())
69 tgt.write_text(src.text())
70 rpt[str(tgt)] = md5.new(tgt.text()).hexdigest()
70 rpt[str(tgt)] = hashlib.md5(tgt.text()).hexdigest()
71 else:
71 else:
72 pr(' == Modified, skipping %s, diffs below == ' % tgt)
72 pr(' == Modified, skipping %s, diffs below == ' % tgt)
73 #rpt[str(tgt)] = md5.new(tgt.bytes()).hexdigest()
73 #rpt[str(tgt)] = hashlib.md5(tgt.bytes()).hexdigest()
74 real = showdiff(tgt,src)
74 real = showdiff(tgt,src)
75 pr('') # empty line
75 pr('') # empty line
76 if not real:
76 if not real:
77 pr("(Ok, it was identical, only upgrading checksum)")
77 pr("(Ok, it was identical, only upgrading checksum)")
78 rpt[str(tgt)] = md5.new(tgt.text()).hexdigest()
78 rpt[str(tgt)] = hashlib.md5(tgt.text()).hexdigest()
79 else:
79 else:
80 modded.append(tgt)
80 modded.append(tgt)
81
81
82 #print rpt
82 #print rpt
83 pickle.dump(rpt, rep.open('w'))
83 pickle.dump(rpt, rep.open('w'))
84 if modded:
84 if modded:
85 print "\n\nDelete the following files manually (and rerun %upgrade)\nif you need a full upgrade:"
85 print "\n\nDelete the following files manually (and rerun %upgrade)\nif you need a full upgrade:"
86 for m in modded:
86 for m in modded:
87 print m
87 print m
88
88
89
89
90 import sys
90 import sys
91 if __name__ == "__main__":
91 if __name__ == "__main__":
92 upgrade_dir(path(sys.argv[1]), path(sys.argv[2]))
92 upgrade_dir(path(sys.argv[1]), path(sys.argv[2]))
General Comments 0
You need to be logged in to leave comments. Login now