示例#1
0
def handleFile(phase, dirname, basename, suffix):
    filename = os.path.join(dirname, basename)
    #if the target file is older than the file this rule should be applied to, do nothing
    #Also check the rules dump - if this one is newer, then we have to execute this stuff, otherwise do nothing
    tFilename = os.path.join(const101.tRoot, dirname, basename + suffix)
    if not tools101.build(filename, tFilename) or not tools101.build(const101.rulesDump, tFilename):
        entry = {'units' : json.load(open(tFilename, 'r')), 'filename': filename}
        if not entry['units'] == []:
            matches.append(entry)
        return
    units = list() # metadata units for the file at hand
    id = 0 # current rule number
    for r in rules:
        rule = r["rule"]
        result = matchFile(phase, dirname, basename, rule)
        if not result is None:
            if "metadata" in rule:
                metadata = rule["metadata"]
                if isinstance(metadata, list):
                    for m in metadata:
                        buildUnit(units, id, m, result)
                else:
                    buildUnit(units, id, metadata, result)
        id += 1

    # Contract units to pay attention to dominators
    keys = list()
    removals = list()
    for unit in units:
        metadata = unit["metadata"]
        if "dominator" in metadata:
            keys.append(metadata["dominator"])
    for key in keys:
        for unit in units:
            metadata = unit["metadata"]
            if key in metadata \
            and (not "dominator" in metadata
                 or not metadata["dominator"] == key):
                removals.append(unit)
    survivals = list()
    for unit in units:
        if not unit in removals:
            survivals.append(unit)
    units = survivals

    # Add entry to matches if any matches for file at hand
    tools101.makedirs(os.path.join(const101.tRoot, dirname))

    matchesFile = open(tFilename, 'w')
    matchesFile.write(json.dumps(units))
    if len(units) > 0:
        global noUnits
        global noFilesAffected
        noUnits += len(units)
        noFilesAffected += 1
        entry = dict()
        entry["filename"] = filename
        entry["units"] = units
        matches.append(entry)
        tools101.tick()
示例#2
0
def testFile(sFilename, tFilename):
    return tools101.build(sFilename, tFilename)