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