changeset 1:a1224150b8f6

Cleanup and making code easier to read.
author Tom Fredrik Blenning Klaussen <bfg@blenning.no>
date Sun, 16 Sep 2012 10:35:39 +0200
parents 28b636105ed6
children 10f1d7de9bc3
files Compilable.py DepGraph.py codeOptimizer.py
diffstat 3 files changed, 19 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/Compilable.py	Sat Sep 15 20:34:39 2012 +0200
+++ b/Compilable.py	Sun Sep 16 10:35:39 2012 +0200
@@ -34,31 +34,24 @@
     def worksWithoutModifications(self):
         return tryCompileFile(self.path, 'g++', self.flags)
 
-    def activeBuffer(self):
+    def activeBuffer(self, replace):
         self.loadFile()
         str = ''
         for i in range(len(self.lines)):
-            if not i in self.replace:
+            if not i in replace:
                 str += self.lines[i]
             else:
-                str += self.replace[i]
+                str += replace[i]
         return str
 
-    def works(self):
-        return tryCompileBuffer(self.activeBuffer(), 'g++', self.flags)
-
-    def deactivateLine(self, i):
-        self.replace[i] = "\n"
-
-    def activateLine(self, i):
-        del self.replace[i]
+    def works(self, replace):
+        return tryCompileBuffer(self.activeBuffer(replace), 'g++', self.flags)
 
     def loadFile(self):
         if self.lines == None:
             f = open(self.path, 'r')
             self.lines = f.readlines()
             f.close()
-            self.replace = {}
 
     def includeLines(self):
         self.loadFile()
@@ -73,13 +66,14 @@
     def removeRemovableIncludes(self):
         l = self.includeLines()
         retVal = []
+        replace = {}
         for n in l:
-            self.deactivateLine(n)
+            replace[n] = '\n'
             print 'Trying to compile ' + self.path + ' without ' + self.getPathname(self.lines[n])
-            if self.works():
+            if self.works(replace):
                 retVal.append(n)
             else:
-                self.activateLine(n)
+                del replace[n]
 
         return retVal
             
@@ -108,17 +102,17 @@
 
         return basename
 
-    def replaceIncludes(self):
+    def replaceIncludes(self, replace):
         l = self.includeLines()
         retVal = []
         for n in l:
-            if n not in self.replace:
+            if n not in replace:
                 rstring = 'class ' + self.getClassname(self.lines[n]) + ';\n'
-                self.replace[n] = rstring
-                if self.works():
+                replace[n] = rstring
+                if self.works(replace):
                     retVal.append(n)
                 else:
-                    self.activateLine(n)
+                    del replace[n]
         return retVal
 
     def dependencies(self):
--- a/DepGraph.py	Sat Sep 15 20:34:39 2012 +0200
+++ b/DepGraph.py	Sun Sep 16 10:35:39 2012 +0200
@@ -1,5 +1,3 @@
-import numpy
-
 from types import *
 
 class DepGraph:
--- a/codeOptimizer.py	Sat Sep 15 20:34:39 2012 +0200
+++ b/codeOptimizer.py	Sun Sep 16 10:35:39 2012 +0200
@@ -1,6 +1,6 @@
 #!/usr/bin/env python -O
 
-import sys, string, os, subprocess
+import sys
 
 from Compilable import Compilable
 from DepGraph import DepGraph
@@ -22,7 +22,7 @@
 
 infiles = sys.argv[1:]
 
-cppfiles = {}
+files = {}
 unknown = []
 
 flags = '-DHAS_BOOST -DQT_NO_DEBUG -DQT_OPENGL_LIB -DQT_XML_LIB -DQT_SQL_LIB -O3 -Wall -I/opt/local/include -I/Users/bfg/QtSDK/Desktop/Qt/4.8.1/gcc/include -F/Users/bfg/QtSDK/Desktop/Qt/4.8.1/gcc/lib -I/Users/bfg/QtSDK/Desktop/Qt/4.8.1/gcc/include/QtOpenGL -I/Users/bfg/QtSDK/Desktop/Qt/4.8.1/gcc/include/QtXml -I/Users/bfg/QtSDK/Desktop/Qt/4.8.1/gcc/include/QtSql -I/Users/bfg/projects/dedupe'
@@ -31,7 +31,7 @@
     if isHppfile(file) or isCppfile(file):
         c = Compilable(file)
         c.setFlags(flags)
-        cppfiles[file] = c
+        files[file] = c
     else:
         unknown.append(file)
 
@@ -39,8 +39,6 @@
     str = ", "
     raise SystemExit(str.join(unknown)  + " are of unknown filetype")
 
-files = cppfiles
-
 depgraph = DepGraph()
 
 for file in files:
@@ -62,8 +60,8 @@
         print 'Removable lines in ' + file.path
         for r in removable:
             print str(r) + ' : ' + file.lines[r].rstrip()
-            
-    replacable = file.replaceIncludes()
+
+    replacable = file.replaceIncludes(dict.fromkeys(removable, '\n'))
     if replacable:
         print 'Replacable lines in ' + file.path
         for r in replacable: