def matchFile(phase, dirname, basename, rule): result = dict() filename = os.path.join(dirname, basename) # # Check applicability of rule to phase # if ("predicate" in rule and phase != "predicates")\ or ("fragment" in rule and phase != "fragments")\ or (not "predicate" in rule and phase == "predicates")\ or (not "fragment" in rule and phase == "fragments"): return None # ignore fpredicates if rule.has_key('fpredicate'): return None # # Check dirname constraint # if not checkFilename(rule, "dirname", dirname): return None # # Check basename constraint # if not checkFilename(rule, "basename", basename): return None # # Check filename constraint # if not checkFilename(rule, "filename", filename): return None # # Check suffix constraint # if "suffix" in rule: suffixes = rule["suffix"] if not isinstance(suffixes, list): suffixes = [ suffixes ] either = False for suffix in suffixes: if basename.endswith(suffix): either = True break if not either: return None # # Check content, if required. # if "content" in rule: global noContentConstraints noContentConstraints += 1 pattern = rule["content"] if pattern[0]=="#" and pattern[len(pattern)-1]=="#": pattern = pattern[1:len(pattern)-2] content = open(os.path.join(const101.sRoot, filename), 'r').read() searchResult = re.search(pattern, content) if searchResult is None: return None else: global noContentConstraintsOk noContentConstraintsOk += 1 # # Apply predicate, if present. # if "predicate" in rule: predicate = rule["predicate"] global predicates predicates.add(predicate) global noPredicateConstraints noPredicateConstraints += 1 global noPredicateConstraintsOk if "args" in rule: args = rule["args"] if not isinstance(args, list): args = [ args ] else: args = [] predicatePath = os.path.join(const101.sRoot, predicate) # this branch is only needed as long as the shell script import matchers (javaImport.sh, dotNETImport.sh) aren't replaced yet if predicate.endswith(".sh"): cmd = predicatePath for arg in args: cmd += " \"" + arg + "\"" cmd += " \"" + os.path.join(const101.sRoot, filename) + "\"" (status, output) = commands.getstatusoutput(cmd) if status == 0: noPredicateConstraintsOk += 1 else: return None else: # this branch is will replace the upper branch as soon as all shell script import matchers (javaImport.sh, dotNETImport.sh) are replaced moduleName, file_ext = os.path.splitext(os.path.split(predicatePath)[-1]) if file_ext.lower() == '.py': predicateModule = imp.load_source(moduleName, predicatePath) elif file_ext.lower() == '.pyc': predicateModule = imp.load_compiled(moduleName, predicatePath) try: status = predicateModule.run(args=args,filePath=os.path.join(const101.sRoot, filename)) except: return None if status==True: noPredicateConstraintsOk += 1 else: return None # # Locate fragment, if present. # if "fragment" in rule: fragment = rule["fragment"] global noFragments noFragments += 1 if "args" in rule: args = rule["args"] if not isinstance(args, list): args = [ args ] else: args = [] locator = None if filename in basics: locator = tools101.valueByKey(basics[filename]["units"], "locator") if locator is None: failure = dict() failure["error"] = "locator not found" failure["rule"] = rule failures.append(failure) return None global locators locators.add(locator) cmd = os.path.join(const101.sRoot, locator) tmpIn = os.path.join(const101.tRoot, filename + ".tmpIn") tmpOut = os.path.join(const101.tRoot, filename + ".tmpOut") for arg in args: cmd += " \"" + arg + "\"" cmd += " \"" + os.path.join(const101.sRoot, filename) + "\"" cmd += " \"" + tmpIn + "\"" cmd += " \"" + tmpOut + "\"" tmpInFile = open(tmpIn, 'w') if isinstance(fragment, basestring): tmpInContent = fragment else: tmpInContent = json.dumps(fragment) tmpInFile.write(tmpInContent) tmpInFile.close() (status, output) = commands.getstatusoutput(cmd) if status == 0: try: os.remove(tmpIn) except: pass try: tmpOutFile = open(tmpOut, 'r') result["lines"] = json.load(open(tmpOut, 'r')) except: failure = dict() failure["locator"] = locator failure["command"] = cmd failure["output"] = "result of fragment location not found" failure["rule"] = rule failures.append(failure) return None try: os.remove(tmpOut) except: pass if status != 0: failure = dict() failure["locator"] = locator failure["command"] = cmd failure["status"] = status failure["output"] = output failure["rule"] = rule failures.append(failure) return None # # No constraint left to check # return result
def matchFile(phase, dirname, basename, rule): global predicate_time global fragment_time result = dict() filename = os.path.join(dirname, basename) # # Check applicability of rule to phase # if ("predicate" in rule and phase != "predicates")\ or ("fragment" in rule and phase != "fragments")\ or (not "predicate" in rule and phase == "predicates")\ or (not "fragment" in rule and phase == "fragments"): return None # # Check dirname constraint # if not checkFilename(rule, "dirname", dirname): return None # # Check basename constraint # if not checkFilename(rule, "basename", basename): return None # # Check filename constraint # if not checkFilename(rule, "filename", filename): return None # # Check suffix constraint # if "suffix" in rule: suffixes = rule["suffix"] if not isinstance(suffixes, list): suffixes = [ suffixes ] either = False for suffix in suffixes: if basename.endswith(suffix): either = True break if not either: return None # # Check content, if required. # if "content" in rule: global noContentConstraints noContentConstraints += 1 pattern = rule["content"] if pattern[0]=="#" and pattern[len(pattern)-1]=="#": pattern = pattern[1:len(pattern)-2] content = open(os.path.join(const101.sRoot, filename), 'r').read() searchResult = re.search(pattern, content) if searchResult is None: return None else: global noContentConstraintsOk noContentConstraintsOk += 1 # # Apply predicate, if present. # if "predicate" in rule: _t = time.time() predicate = rule["predicate"] global predicates predicates.add(predicate) global noPredicateConstraints noPredicateConstraints += 1 if "args" in rule: args = rule["args"] if not isinstance(args, list): args = [ args ] else: args = [] cmd = os.path.join(const101.sRoot, predicate) for arg in args: cmd += " \"" + arg + "\"" cmd += " \"" + os.path.join(const101.sRoot, filename) + "\"" (status, output) = commands.getstatusoutput(cmd) _t = time.time() - _t predicate_time += _t times.append({ 'filename': filename, 'cmd': cmd, 'time': _t, 'predicate': predicate }) if status == 0: global noPredicateConstraintsOk noPredicateConstraintsOk += 1 else: return None # # Locate fragment, if present. # if "fragment" in rule: _t = time.time() fragment = rule["fragment"] global noFragments noFragments += 1 if "args" in rule: args = rule["args"] if not isinstance(args, list): args = [ args ] else: args = [] locator = None if filename in basics: locator = tools101.valueByKey(basics[filename]["units"], "locator") if locator is None: failure = dict() failure["error"] = "locator not found" failure["rule"] = rule failures.append(failure) return None global locators locators.add(locator) cmd = os.path.join(const101.sRoot, locator) tmpIn = os.path.join(const101.tRoot, filename + ".tmpIn") tmpOut = os.path.join(const101.tRoot, filename + ".tmpOut") for arg in args: cmd += " \"" + arg + "\"" cmd += " \"" + os.path.join(const101.sRoot, filename) + "\"" cmd += " \"" + tmpIn + "\"" cmd += " \"" + tmpOut + "\"" tmpInFile = open(tmpIn, 'w') if isinstance(fragment, basestring): tmpInContent = fragment else: tmpInContent = json.dumps(fragment) tmpInFile.write(tmpInContent) tmpInFile.close() (status, output) = commands.getstatusoutput(cmd) if status == 0: try: os.remove(tmpIn) except: pass try: tmpOutFile = open(tmpOut, 'r') result["lines"] = json.load(open(tmpOut, 'r')) except: failure = dict() failure["locator"] = locator failure["command"] = cmd failure["output"] = "result of fragment location not found" failure["rule"] = rule failures.append(failure) return None try: os.remove(tmpOut) except: pass fragment_time += time.time() - _t if status != 0: #failure = dict() #failure["locator"] = locator #failure["command"] = cmd #failure["status"] = status #failure["output"] = output #failure["rule"] = rule failure = { 'locator': locator, 'command': cmd, 'status': status, 'output': output, 'rule': rule } failures.append(failure) return None # # No constraint left to check # return result