view codeOptimizer.py @ 4:3a56cd936c59

Cleanup some code. Add code for breaking loops in the dependency tree.
author Tom Fredrik Blenning Klaussen <bfg@blenning.no>
date Mon, 24 Sep 2012 01:16:43 +0200
parents f65c2d63ab66
children 94b1959b0108
line wrap: on
line source

#!/usr/bin/env python -O

import sys

from Compilable import Compilable
from DepGraph import DepGraph
from Config import Config


def usage(name):
    print "Usage is:\n\t%s <xml configuration file> [files for analysis]" % name

if len(sys.argv) < 2:
    usage(sys.argv[0])
    sys.exit(1)

try:
    options = Config(sys.argv[1])
except:
    print sys.argv[1] + ' is not a valid xml file'
    sys.exit(1)

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])

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 files[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()