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