~ellisonbg/ipython/bugfixes0411409

« back to all changes in this revision

Viewing changes to IPython/upgrade_dir.py

  • Committer: ville
  • Date: 2008-02-16 09:50:47 UTC
  • mto: (0.12.1 ipython_main)
  • mto: This revision was merged to the branch mainline in revision 990.
  • Revision ID: ville@ville-pc-20080216095047-500x6dluki1iz40o
initialization (no svn history)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
""" A script/util to upgrade all files in a directory
 
3
 
 
4
This is rather conservative in its approach, only copying/overwriting
 
5
new and unedited files.
 
6
 
 
7
To be used by "upgrade" feature.
 
8
"""
 
9
try:
 
10
    from IPython.external.path import path
 
11
except ImportError:
 
12
    from path import path
 
13
 
 
14
import md5,pickle
 
15
 
 
16
def showdiff(old,new):
 
17
    import difflib
 
18
    d = difflib.Differ()
 
19
    lines = d.compare(old.lines(),new.lines())
 
20
    realdiff = False
 
21
    for l in lines:
 
22
        print l,
 
23
        if not realdiff and not l[0].isspace():
 
24
            realdiff = True
 
25
    return realdiff
 
26
 
 
27
def upgrade_dir(srcdir, tgtdir):
 
28
    """ Copy over all files in srcdir to tgtdir w/ native line endings
 
29
 
 
30
    Creates .upgrade_report in tgtdir that stores md5sums of all files
 
31
    to notice changed files b/w upgrades.
 
32
    """
 
33
 
 
34
    def pr(s):
 
35
        print s
 
36
    junk = ['.svn','ipythonrc*','*.pyc', '*.pyo', '*~', '.hg']
 
37
    
 
38
    def ignorable(p):
 
39
        for pat in junk:
 
40
            if p.startswith(pat) or p.fnmatch(pat):
 
41
                return True
 
42
        return False
 
43
 
 
44
    modded = []
 
45
    files = [path(srcdir).relpathto(p) for p in path(srcdir).walkfiles()]
 
46
    #print files
 
47
    rep = tgtdir / '.upgrade_report'
 
48
    try:
 
49
        rpt = pickle.load(rep.open())
 
50
    except:
 
51
        rpt = {}
 
52
 
 
53
    for f in files:
 
54
        if ignorable(f):
 
55
            continue
 
56
        src = srcdir / f
 
57
        tgt = tgtdir / f
 
58
        if not tgt.isfile():
 
59
            pr("Creating %s" % str(tgt))
 
60
 
 
61
            tgt.write_text(src.text())
 
62
            rpt[str(tgt)] = md5.new(tgt.text()).hexdigest()
 
63
        else:
 
64
            cont = tgt.text()
 
65
            sum = rpt.get(str(tgt), None)
 
66
            #print sum
 
67
            if sum and md5.new(cont).hexdigest() == sum:
 
68
                pr("%s: Unedited, installing new version" % tgt)
 
69
                tgt.write_text(src.text())
 
70
                rpt[str(tgt)] = md5.new(tgt.text()).hexdigest()
 
71
            else:
 
72
                pr(' == Modified, skipping %s, diffs below == ' % tgt)
 
73
                #rpt[str(tgt)] = md5.new(tgt.bytes()).hexdigest()
 
74
                real = showdiff(tgt,src)
 
75
                pr('') # empty line
 
76
                if not real:
 
77
                    pr("(Ok, it was identical, only upgrading checksum)")
 
78
                    rpt[str(tgt)] = md5.new(tgt.text()).hexdigest()
 
79
                else:
 
80
                    modded.append(tgt)
 
81
 
 
82
        #print rpt
 
83
    pickle.dump(rpt, rep.open('w'))
 
84
    if modded:
 
85
        print "\n\nDelete the following files manually (and rerun %upgrade)\nif you need a full upgrade:"
 
86
        for m in modded:
 
87
            print m
 
88
 
 
89
 
 
90
import sys
 
91
if __name__ == "__main__":
 
92
    upgrade_dir(path(sys.argv[1]), path(sys.argv[2]))