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