Mercurial > codeOptimizer
comparison codeOptimizer.py @ 11:5b542d05e2b1
Make main function an actual function for easier testing.
| author | Tom Fredrik Blenning Klaussen <bfg@blenning.no> |
|---|---|
| date | Wed, 14 Nov 2012 22:04:53 +0100 |
| parents | 94b1959b0108 |
| children | 07e34df56b2a |
comparison
equal
deleted
inserted
replaced
| 10:09b39021c4af | 11:5b542d05e2b1 |
|---|---|
| 7 from Config import Config | 7 from Config import Config |
| 8 | 8 |
| 9 | 9 |
| 10 def usage(name): | 10 def usage(name): |
| 11 print "Usage is:\n\t%s <xml configuration file> [files for analysis]" % name | 11 print "Usage is:\n\t%s <xml configuration file> [files for analysis]" % name |
| 12 | |
| 13 def codeOptimizer(options, infiles): | |
| 14 | |
| 15 if not infiles: | |
| 16 infiles = options.getFiles() | |
| 17 | |
| 18 files = {} | |
| 19 unknown = [] | |
| 20 | |
| 21 for file in infiles: | |
| 22 if Compilable.acceptsFile(file): | |
| 23 c = Compilable(file) | |
| 24 c.setFlags(options.getCxxflags(file)) | |
| 25 files[file] = c | |
| 26 else: | |
| 27 unknown.append(file) | |
| 28 | |
| 29 if len(unknown) > 0: | |
| 30 delim = ", " | |
| 31 raise SystemExit(delim.join(unknown) + " are of unknown filetype") | |
| 32 | |
| 33 depgraph = DepGraph() | |
| 34 | |
| 35 for file in files: | |
| 36 depgraph.add(files[file]) | |
| 37 | |
| 38 for file in files: | |
| 39 depgraph.addDependency(files[file], | |
| 40 list(files[dep] | |
| 41 for dep in files[file].dependencies())) | |
| 42 | |
| 43 files = depgraph.directedGraph() | |
| 44 print files | |
| 45 | |
| 46 for file in files: | |
| 47 if not file.worksWithoutModifications(): | |
| 48 print file.path | |
| 49 raise SystemExit(file.path + " does not compile at all") | |
| 50 | |
| 51 for file in files: | |
| 52 removable = file.removeRemovableIncludes() | |
| 53 if removable: | |
| 54 print 'Removable lines in ' + file.path | |
| 55 for r in removable: | |
| 56 print str(r) + ' : ' + file.lines[r].rstrip() | |
| 57 | |
| 58 replacable = file.replaceIncludes(dict.fromkeys(removable, '\n')) | |
| 59 if replacable: | |
| 60 print 'Replacable lines in ' + file.path | |
| 61 for r in replacable: | |
| 62 print str(r) + ' : ' + file.lines[r].rstrip() | |
| 63 | |
| 12 | 64 |
| 13 if len(sys.argv) < 2: | 65 if len(sys.argv) < 2: |
| 14 usage(sys.argv[0]) | 66 usage(sys.argv[0]) |
| 15 sys.exit(1) | 67 sys.exit(1) |
| 16 | 68 |
| 20 print sys.argv[1] + ' is not a valid xml file' | 72 print sys.argv[1] + ' is not a valid xml file' |
| 21 sys.exit(1) | 73 sys.exit(1) |
| 22 | 74 |
| 23 infiles = sys.argv[2:] | 75 infiles = sys.argv[2:] |
| 24 | 76 |
| 25 if not infiles: | 77 codeOptimizer(options, infiles) |
| 26 infiles = options.getFiles() | |
| 27 | 78 |
| 28 files = {} | |
| 29 unknown = [] | |
| 30 | |
| 31 for file in infiles: | |
| 32 if Compilable.acceptsFile(file): | |
| 33 c = Compilable(file) | |
| 34 c.setFlags(options.getCxxflags(file)) | |
| 35 files[file] = c | |
| 36 else: | |
| 37 unknown.append(file) | |
| 38 | |
| 39 if len(unknown) > 0: | |
| 40 str = ", " | |
| 41 raise SystemExit(str.join(unknown) + " are of unknown filetype") | |
| 42 | |
| 43 depgraph = DepGraph() | |
| 44 | |
| 45 for file in files: | |
| 46 depgraph.add(files[file]) | |
| 47 | |
| 48 for file in files: | |
| 49 depgraph.addDependency(files[file], | |
| 50 list(files[dep] | |
| 51 for dep in files[file].dependencies())) | |
| 52 | |
| 53 files = depgraph.directedGraph() | |
| 54 print files | |
| 55 | |
| 56 for file in files: | |
| 57 if not file.worksWithoutModifications(): | |
| 58 print file.path | |
| 59 raise SystemExit(file.path + " does not compile at all") | |
| 60 | |
| 61 for file in files: | |
| 62 removable = file.removeRemovableIncludes() | |
| 63 if removable: | |
| 64 print 'Removable lines in ' + file.path | |
| 65 for r in removable: | |
| 66 print str(r) + ' : ' + file.lines[r].rstrip() | |
| 67 | |
| 68 replacable = file.replaceIncludes(dict.fromkeys(removable, '\n')) | |
| 69 if replacable: | |
| 70 print 'Replacable lines in ' + file.path | |
| 71 for r in replacable: | |
| 72 print str(r) + ' : ' + file.lines[r].rstrip() |
