#!/usr/bin/env python def get_fixers(): from lib2to3c.typeobject import FixTypeobjectInitializers from lib2to3c import CocciFix from lib2to3c.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()