upgrade_dir.py
95 lines
| 2.7 KiB
| text/x-python
|
PythonLexer
/ IPython / upgrade_dir.py
vivainio
|
r222 | #!/usr/bin/env python | ||
vivainio
|
r197 | """ A script/util to upgrade all files in a directory | ||
This is rather conservative in its approach, only copying/overwriting | ||||
new and unedited files. | ||||
To be used by "upgrade" feature. | ||||
""" | ||||
vivainio
|
r321 | try: | ||
from IPython.Extensions.path import path | ||||
except ImportError: | ||||
try: | ||||
from Extensions.path import path | ||||
except ImportError: | ||||
from path import path | ||||
vivainio
|
r197 | import md5,pickle | ||
def showdiff(old,new): | ||||
import difflib | ||||
d = difflib.Differ() | ||||
vivainio
|
r201 | lines = d.compare(old.lines(),new.lines()) | ||
realdiff = False | ||||
for l in lines: | ||||
print l, | ||||
if not realdiff and not l[0].isspace(): | ||||
realdiff = True | ||||
return realdiff | ||||
vivainio
|
r197 | |||
def upgrade_dir(srcdir, tgtdir): | ||||
""" Copy over all files in srcdir to tgtdir w/ native line endings | ||||
Creates .upgrade_report in tgtdir that stores md5sums of all files | ||||
to notice changed files b/w upgrades. | ||||
""" | ||||
def pr(s): | ||||
print s | ||||
def ignorable(p): | ||||
vivainio
|
r198 | if p.lower().startswith('.svn') or p.startswith('ipythonrc'): | ||
vivainio
|
r197 | return True | ||
return False | ||||
vivainio
|
r201 | modded = [] | ||
vivainio
|
r197 | files = [path(srcdir).relpathto(p) for p in path(srcdir).walkfiles()] | ||
#print files | ||||
rep = tgtdir / '.upgrade_report' | ||||
try: | ||||
rpt = pickle.load(rep.open()) | ||||
except: | ||||
rpt = {} | ||||
for f in files: | ||||
if ignorable(f): | ||||
continue | ||||
src = srcdir / f | ||||
tgt = tgtdir / f | ||||
if not tgt.isfile(): | ||||
pr("Creating %s" % str(tgt)) | ||||
tgt.write_text(src.text()) | ||||
vivainio
|
r201 | rpt[str(tgt)] = md5.new(tgt.text()).hexdigest() | ||
vivainio
|
r197 | else: | ||
vivainio
|
r201 | cont = tgt.text() | ||
vivainio
|
r197 | sum = rpt.get(str(tgt), None) | ||
#print sum | ||||
if sum and md5.new(cont).hexdigest() == sum: | ||||
pr("Unedited, installing new %s" % tgt) | ||||
vivainio
|
r201 | rpt[str(tgt)] = md5.new(tgt.text()).hexdigest() | ||
vivainio
|
r197 | else: | ||
vivainio
|
r201 | pr(' == Modified, skipping %s, diffs below == ' % tgt) | ||
vivainio
|
r197 | #rpt[str(tgt)] = md5.new(tgt.bytes()).hexdigest() | ||
vivainio
|
r201 | real = showdiff(tgt,src) | ||
vivainio
|
r206 | pr('') # empty line | ||
vivainio
|
r201 | if not real: | ||
vivainio
|
r206 | pr("(Ok, it wasn't that different at all, upgrading checksum)") | ||
vivainio
|
r201 | rpt[str(tgt)] = md5.new(tgt.text()).hexdigest() | ||
else: | ||||
modded.append(tgt) | ||||
vivainio
|
r197 | #print rpt | ||
pickle.dump(rpt, rep.open('w')) | ||||
vivainio
|
r201 | if modded: | ||
vivainio
|
r308 | print "\n\nDelete the following files manually (and rerun %upgrade)\nif you need a full upgrade:" | ||
vivainio
|
r201 | for m in modded: | ||
print m | ||||
vivainio
|
r197 | |||
import sys | ||||
vivainio
|
r201 | if __name__ == "__main__": | ||
upgrade_dir(path(sys.argv[1]), path(sys.argv[2])) | ||||
vivainio
|
r222 | |||