diff options
Diffstat (limited to 'tools/checker.py')
-rwxr-xr-x | tools/checker.py | 111 |
1 files changed, 53 insertions, 58 deletions
diff --git a/tools/checker.py b/tools/checker.py index 406a311638..24784cec6d 100755 --- a/tools/checker.py +++ b/tools/checker.py @@ -77,10 +77,11 @@ import re import shutil import sys import tempfile -from subprocess import check_call class Logger(object): - SilentMode = False + + class Level(object): + NoOutput, Error, Info = range(3) class Color(object): Default, Blue, Gray, Purple, Red = range(5) @@ -100,13 +101,15 @@ class Logger(object): else: return '\033[0m' + Verbosity = Level.Info + @staticmethod - def log(text, color=Color.Default, newLine=True, out=sys.stdout): - if not Logger.SilentMode: + def log(text, level=Level.Info, color=Color.Default, newLine=True, out=sys.stdout): + if level <= Logger.Verbosity: text = Logger.Color.terminalCode(color, out) + text + \ Logger.Color.terminalCode(Logger.Color.Default, out) if newLine: - print(text, file=out) + print(text, flush=True, file=out) else: print(text, end="", flush=True, file=out) @@ -120,9 +123,9 @@ class Logger(object): if location: location += " " - Logger.log(location, color=Logger.Color.Gray, newLine=False, out=sys.stderr) - Logger.log("error: ", color=Logger.Color.Red, newLine=False, out=sys.stderr) - Logger.log(msg, out=sys.stderr) + Logger.log(location, Logger.Level.Error, color=Logger.Color.Gray, newLine=False, out=sys.stderr) + Logger.log("error: ", Logger.Level.Error, color=Logger.Color.Red, newLine=False, out=sys.stderr) + Logger.log(msg, Logger.Level.Error, out=sys.stderr) sys.exit(1) @staticmethod @@ -685,52 +688,23 @@ class OutputFile(FileSplitMixin): def ParseArguments(): parser = argparse.ArgumentParser() - parser.add_argument("test_file", help="the source of the test with checking annotations") + parser.add_argument("tested_file", + help="text file the checks should be verified against") + parser.add_argument("source_path", nargs="?", + help="path to file/folder with checking annotations") parser.add_argument("--check-prefix", dest="check_prefix", default="CHECK", metavar="PREFIX", - help="prefix of checks in the test file (default: CHECK)") + help="prefix of checks in the test files (default: CHECK)") parser.add_argument("--list-groups", dest="list_groups", action="store_true", - help="print a list of all groups found in the test output") + help="print a list of all groups found in the tested file") parser.add_argument("--dump-group", dest="dump_group", metavar="GROUP", help="print the contents of an output group") + parser.add_argument("-q", "--quiet", action="store_true", + help="print only errors") + parser.add_argument("--no-clean", dest="no_clean", action="store_true", + help="don't clean up generated files") return parser.parse_args() -class cd: - """Helper class which temporarily changes the working directory.""" - - def __init__(self, newPath): - self.newPath = newPath - - def __enter__(self): - self.savedPath = os.getcwd() - os.chdir(self.newPath) - - def __exit__(self, etype, value, traceback): - os.chdir(self.savedPath) - - -def CompileTest(inputFile, tempFolder): - classFolder = tempFolder + "/classes" - dexFile = tempFolder + "/test.dex" - oatFile = tempFolder + "/test.oat" - outputFile = tempFolder + "/art.cfg" - os.makedirs(classFolder) - - # Build a DEX from the source file. We pass "--no-optimize" to dx to avoid - # interference with its optimizations. - check_call(["javac", "-d", classFolder, inputFile]) - check_call(["dx", "--dex", "--no-optimize", "--output=" + dexFile, classFolder]) - - # Run dex2oat and export the HGraph. The output is stored into ${PWD}/art.cfg. - with cd(tempFolder): - check_call(["dex2oat", "-j1", "--dump-passes", "--compiler-backend=Optimizing", - "--android-root=" + os.environ["ANDROID_HOST_OUT"], - "--boot-image=" + os.environ["ANDROID_HOST_OUT"] + "/framework/core-optimizing.art", - "--runtime-arg", "-Xnorelocate", "--dex-file=" + dexFile, "--oat-file=" + oatFile]) - - return outputFile - - def ListGroups(outputFilename): outputFile = OutputFile(open(outputFilename, "r")) for group in outputFile.groups: @@ -751,26 +725,47 @@ def DumpGroup(outputFilename, groupName): Logger.fail("Group \"" + groupName + "\" not found in the output") -def RunChecks(checkPrefix, checkFilename, outputFilename): - checkBaseName = os.path.basename(checkFilename) - outputBaseName = os.path.splitext(checkBaseName)[0] + ".cfg" +def FindCheckFiles(path): + if not path: + Logger.fail("No source path provided") + elif os.path.isfile(path): + return [ path ] + elif os.path.isdir(path): + foundFiles = [] + for root, dirs, files in os.walk(path): + for file in files: + if os.path.splitext(file)[1] == ".java": + foundFiles.append(os.path.join(root, file)) + return foundFiles + else: + Logger.fail("Source path \"" + path + "\" not found") + - checkFile = CheckFile(checkPrefix, open(checkFilename, "r"), checkBaseName) +def RunChecks(checkPrefix, checkPath, outputFilename): + outputBaseName = os.path.basename(outputFilename) outputFile = OutputFile(open(outputFilename, "r"), outputBaseName) - checkFile.match(outputFile) + + for checkFilename in FindCheckFiles(checkPath): + checkBaseName = os.path.basename(checkFilename) + checkFile = CheckFile(checkPrefix, open(checkFilename, "r"), checkBaseName) + checkFile.match(outputFile) if __name__ == "__main__": args = ParseArguments() - tempFolder = tempfile.mkdtemp() + if args.quiet: + Logger.Verbosity = Logger.Level.Error + tempFolder = tempfile.mkdtemp() try: - outputFile = CompileTest(args.test_file, tempFolder) if args.list_groups: - ListGroups(outputFile) + ListGroups(args.tested_file) elif args.dump_group: - DumpGroup(outputFile, args.dump_group) + DumpGroup(args.tested_file, args.dump_group) else: - RunChecks(args.check_prefix, args.test_file, outputFile) + RunChecks(args.check_prefix, args.source_path, args.tested_file) finally: - shutil.rmtree(tempFolder) + if args.no_clean: + print("Files left in %s" % tempFolder) + else: + shutil.rmtree(tempFolder) |