summaryrefslogtreecommitdiffstats
path: root/2to3c
blob: ae3233a85dfd6249d6a93318e0b1644fccfc6d71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python

def get_fixers():
    from fixes.typeobject import FixTypeobjectInitializers
    from fixes import CocciFix
    from fixes.initmodule import FixInitModule
    fixes = [FixTypeobjectInitializers()]
    for filename in ['RO.cocci', 'int-to-long.cocci', 'ob_type.cocci', 
                     'repr.cocci']:
        fixes.append(CocciFix(filename))
    fixes.append(FixInitModule())
    return fixes

def fixup_content(content):
    # Apply the various fixers, in turn:
    for fixer in get_fixers():
        content = fixer.transform(content)
    return content


def fixup_file(filename, options):
    from difflib import unified_diff

    content = open(filename, 'r').read()    
    fixed_content = fixup_content(content)
    if content != fixed_content:
        for line in unified_diff(content.splitlines(),
                                 fixed_content.splitlines(), 
                                 fromfile = filename+'.orig',
                                 tofile = filename,
                                 lineterm=''):
            print line

        if options.write:
            open(filename, 'w').write(fixed_content)

def main():
    from optparse import OptionParser
    usage = "usage: %prog [options] filenames..."
    parser = OptionParser(usage=usage)
    parser.add_option('-w', '--write',
                      action="store_true", dest="write", default=False,
                      help="Write back modified files")
    (options, args) = parser.parse_args()
    # print (options, args)
    for filename in args:
        fixup_file(filename, options)

if __name__ == '__main__':
    main()