view codeOptimizer.py @ 12:07e34df56b2a

Separate target for running from only conffilename.
author Tom Fredrik Blenning Klaussen <bfg@blenning.no>
date Wed, 14 Nov 2012 22:08:58 +0100
parents 5b542d05e2b1
children 2213edf59af4
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

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


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

    codeOptimizer(options, infiles)



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

infiles = sys.argv[2:]

codeOptimizerFromFilename(sys.argv[1], infiles)