changeset 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 09b39021c4af
children 07e34df56b2a
files codeOptimizer.py
diffstat 1 files changed, 53 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/codeOptimizer.py	Wed Nov 14 21:44:53 2012 +0100
+++ b/codeOptimizer.py	Wed Nov 14 22:04:53 2012 +0100
@@ -10,6 +10,58 @@
 def usage(name):
     print "Usage is:\n\t%s <xml configuration file> [files for analysis]" % name
 
+def codeOptimizer(options, infiles):
+
+    if not infiles:
+        infiles = options.getFiles()
+
+    files = {}
+    unknown = []
+
+    for file in infiles:
+        if Compilable.acceptsFile(file):
+            c = Compilable(file)
+            c.setFlags(options.getCxxflags(file))
+            files[file] = c
+        else:
+            unknown.append(file)
+
+    if len(unknown) > 0:
+        delim = ", "
+        raise SystemExit(delim.join(unknown)  + " are of unknown filetype")
+
+    depgraph = DepGraph()
+
+    for file in files:
+        depgraph.add(files[file])
+
+    for file in files:
+        depgraph.addDependency(files[file],
+                               list(files[dep]
+                                    for dep in files[file].dependencies()))
+
+    files = depgraph.directedGraph()
+    print files
+
+    for file in files:
+        if not file.worksWithoutModifications():
+            print file.path
+            raise SystemExit(file.path  + " does not compile at all")
+
+    for file in files:
+        removable = file.removeRemovableIncludes()
+        if removable:
+            print 'Removable lines in ' + file.path
+            for r in removable:
+                print str(r) + ' : ' + file.lines[r].rstrip()
+
+        replacable = file.replaceIncludes(dict.fromkeys(removable, '\n'))
+        if replacable:
+            print 'Replacable lines in ' + file.path
+            for r in replacable:
+                print str(r) + ' : ' + file.lines[r].rstrip()
+
+
 if len(sys.argv) < 2:
     usage(sys.argv[0])
     sys.exit(1)
@@ -22,51 +74,5 @@
 
 infiles = sys.argv[2:]
 
-if not infiles:
-    infiles = options.getFiles()
-
-files = {}
-unknown = []
-
-for file in infiles:
-    if Compilable.acceptsFile(file):
-        c = Compilable(file)
-        c.setFlags(options.getCxxflags(file))
-        files[file] = c
-    else:
-        unknown.append(file)
-
-if len(unknown) > 0:
-    str = ", "
-    raise SystemExit(str.join(unknown)  + " are of unknown filetype")
-
-depgraph = DepGraph()
-
-for file in files:
-    depgraph.add(files[file])
+codeOptimizer(options, infiles)
 
-for file in files:
-    depgraph.addDependency(files[file],
-                           list(files[dep]
-                                for dep in files[file].dependencies()))
-
-files = depgraph.directedGraph()
-print files
-
-for file in files:
-    if not file.worksWithoutModifications():
-        print file.path
-        raise SystemExit(file.path  + " does not compile at all")
-
-for file in files:
-    removable = file.removeRemovableIncludes()
-    if removable:
-        print 'Removable lines in ' + file.path
-        for r in removable:
-            print str(r) + ' : ' + file.lines[r].rstrip()
-
-    replacable = file.replaceIncludes(dict.fromkeys(removable, '\n'))
-    if replacable:
-        print 'Replacable lines in ' + file.path
-        for r in replacable:
-            print str(r) + ' : ' + file.lines[r].rstrip()