##// END OF EJS Templates
treat .pyo as junk in %upgrade
vivainio -
Show More
@@ -1,95 +1,95 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 junk = ['.svn','ipythonrc*','*.pyc', '*~', '.hg']
39 junk = ['.svn','ipythonrc*','*.pyc', '*.pyo', '*~', '.hg']
40
40
41 def ignorable(p):
41 def ignorable(p):
42 for pat in junk:
42 for pat in junk:
43 if p.startswith(pat) or p.fnmatch(pat):
43 if p.startswith(pat) or p.fnmatch(pat):
44 return True
44 return True
45 return False
45 return False
46
46
47 modded = []
47 modded = []
48 files = [path(srcdir).relpathto(p) for p in path(srcdir).walkfiles()]
48 files = [path(srcdir).relpathto(p) for p in path(srcdir).walkfiles()]
49 #print files
49 #print files
50 rep = tgtdir / '.upgrade_report'
50 rep = tgtdir / '.upgrade_report'
51 try:
51 try:
52 rpt = pickle.load(rep.open())
52 rpt = pickle.load(rep.open())
53 except:
53 except:
54 rpt = {}
54 rpt = {}
55
55
56 for f in files:
56 for f in files:
57 if ignorable(f):
57 if ignorable(f):
58 continue
58 continue
59 src = srcdir / f
59 src = srcdir / f
60 tgt = tgtdir / f
60 tgt = tgtdir / f
61 if not tgt.isfile():
61 if not tgt.isfile():
62 pr("Creating %s" % str(tgt))
62 pr("Creating %s" % str(tgt))
63
63
64 tgt.write_text(src.text())
64 tgt.write_text(src.text())
65 rpt[str(tgt)] = md5.new(tgt.text()).hexdigest()
65 rpt[str(tgt)] = md5.new(tgt.text()).hexdigest()
66 else:
66 else:
67 cont = tgt.text()
67 cont = tgt.text()
68 sum = rpt.get(str(tgt), None)
68 sum = rpt.get(str(tgt), None)
69 #print sum
69 #print sum
70 if sum and md5.new(cont).hexdigest() == sum:
70 if sum and md5.new(cont).hexdigest() == sum:
71 pr("%s: Unedited, installing new version" % tgt)
71 pr("%s: Unedited, installing new version" % tgt)
72 tgt.write_text(src.text())
72 tgt.write_text(src.text())
73 rpt[str(tgt)] = md5.new(tgt.text()).hexdigest()
73 rpt[str(tgt)] = md5.new(tgt.text()).hexdigest()
74 else:
74 else:
75 pr(' == Modified, skipping %s, diffs below == ' % tgt)
75 pr(' == Modified, skipping %s, diffs below == ' % tgt)
76 #rpt[str(tgt)] = md5.new(tgt.bytes()).hexdigest()
76 #rpt[str(tgt)] = md5.new(tgt.bytes()).hexdigest()
77 real = showdiff(tgt,src)
77 real = showdiff(tgt,src)
78 pr('') # empty line
78 pr('') # empty line
79 if not real:
79 if not real:
80 pr("(Ok, it was identical, only upgrading checksum)")
80 pr("(Ok, it was identical, only upgrading checksum)")
81 rpt[str(tgt)] = md5.new(tgt.text()).hexdigest()
81 rpt[str(tgt)] = md5.new(tgt.text()).hexdigest()
82 else:
82 else:
83 modded.append(tgt)
83 modded.append(tgt)
84
84
85 #print rpt
85 #print rpt
86 pickle.dump(rpt, rep.open('w'))
86 pickle.dump(rpt, rep.open('w'))
87 if modded:
87 if modded:
88 print "\n\nDelete the following files manually (and rerun %upgrade)\nif you need a full upgrade:"
88 print "\n\nDelete the following files manually (and rerun %upgrade)\nif you need a full upgrade:"
89 for m in modded:
89 for m in modded:
90 print m
90 print m
91
91
92
92
93 import sys
93 import sys
94 if __name__ == "__main__":
94 if __name__ == "__main__":
95 upgrade_dir(path(sys.argv[1]), path(sys.argv[2]))
95 upgrade_dir(path(sys.argv[1]), path(sys.argv[2]))
General Comments 0
You need to be logged in to leave comments. Login now