##// END OF EJS Templates
First cut at safe appending in the Qt console.
First cut at safe appending in the Qt console.

File last commit:

r3116:be0324d2
r4056:c438abc4
Show More
upgradedir.py
92 lines | 2.7 KiB | text/x-python | PythonLexer
vivainio
merge all from 0.7.3 branch to trunk
r503 #!/usr/bin/env python
""" 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.
"""
try:
vivainio
move path to external
r964 from IPython.external.path import path
vivainio
merge all from 0.7.3 branch to trunk
r503 except ImportError:
vivainio
move path to external
r964 from path import path
vivainio
merge all from 0.7.3 branch to trunk
r503
Thomas Kluyver
Update md5 calls.
r3116 import hashlib, pickle
vivainio
merge all from 0.7.3 branch to trunk
r503
def showdiff(old,new):
import difflib
d = difflib.Differ()
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
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
vivainio
treat .pyo as junk in %upgrade
r637 junk = ['.svn','ipythonrc*','*.pyc', '*.pyo', '*~', '.hg']
vivainio
upgrade_dir.py: skip junk files like *.pyc
r605
vivainio
merge all from 0.7.3 branch to trunk
r503 def ignorable(p):
vivainio
upgrade_dir.py: skip junk files like *.pyc
r605 for pat in junk:
if p.startswith(pat) or p.fnmatch(pat):
return True
vivainio
merge all from 0.7.3 branch to trunk
r503 return False
modded = []
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())
Thomas Kluyver
Update md5 calls.
r3116 rpt[str(tgt)] = hashlib.md5(tgt.text()).hexdigest()
vivainio
merge all from 0.7.3 branch to trunk
r503 else:
cont = tgt.text()
sum = rpt.get(str(tgt), None)
#print sum
Thomas Kluyver
Update md5 calls.
r3116 if sum and hashlib.md5(cont).hexdigest() == sum:
vivainio
upgrade_dir.py: skip junk files like *.pyc
r605 pr("%s: Unedited, installing new version" % tgt)
vivainio
merge all from 0.7.3 branch to trunk
r503 tgt.write_text(src.text())
Thomas Kluyver
Update md5 calls.
r3116 rpt[str(tgt)] = hashlib.md5(tgt.text()).hexdigest()
vivainio
merge all from 0.7.3 branch to trunk
r503 else:
pr(' == Modified, skipping %s, diffs below == ' % tgt)
Thomas Kluyver
Update md5 calls.
r3116 #rpt[str(tgt)] = hashlib.md5(tgt.bytes()).hexdigest()
vivainio
merge all from 0.7.3 branch to trunk
r503 real = showdiff(tgt,src)
pr('') # empty line
if not real:
vivainio
upgrade_dir.py: skip junk files like *.pyc
r605 pr("(Ok, it was identical, only upgrading checksum)")
Thomas Kluyver
Update md5 calls.
r3116 rpt[str(tgt)] = hashlib.md5(tgt.text()).hexdigest()
vivainio
merge all from 0.7.3 branch to trunk
r503 else:
modded.append(tgt)
#print rpt
pickle.dump(rpt, rep.open('w'))
if modded:
print "\n\nDelete the following files manually (and rerun %upgrade)\nif you need a full upgrade:"
for m in modded:
print m
import sys
if __name__ == "__main__":
upgrade_dir(path(sys.argv[1]), path(sys.argv[2]))