David Brazdil | 2c27f2c | 2015-05-12 18:06:38 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
| 2 | # |
| 3 | # Copyright (C) 2014 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | import argparse |
| 18 | import os |
| 19 | |
| 20 | from common.logger import Logger |
| 21 | from file_format.c1visualizer.parser import ParseC1visualizerStream |
| 22 | from file_format.checker.parser import ParseCheckerStream |
| 23 | from match.file import MatchFiles |
| 24 | |
| 25 | def ParseArguments(): |
| 26 | parser = argparse.ArgumentParser() |
| 27 | parser.add_argument("tested_file", |
| 28 | help="text file the checks should be verified against") |
| 29 | parser.add_argument("source_path", nargs="?", |
| 30 | help="path to file/folder with checking annotations") |
| 31 | parser.add_argument("--check-prefix", dest="check_prefix", default="CHECK", metavar="PREFIX", |
| 32 | help="prefix of checks in the test files (default: CHECK)") |
| 33 | parser.add_argument("--list-passes", dest="list_passes", action="store_true", |
| 34 | help="print a list of all passes found in the tested file") |
| 35 | parser.add_argument("--dump-pass", dest="dump_pass", metavar="PASS", |
| 36 | help="print a compiler pass dump") |
| 37 | parser.add_argument("-q", "--quiet", action="store_true", |
| 38 | help="print only errors") |
| 39 | return parser.parse_args() |
| 40 | |
| 41 | |
| 42 | def ListPasses(outputFilename): |
| 43 | c1File = ParseC1visualizerStream(os.path.basename(outputFilename), open(outputFilename, "r")) |
| 44 | for compiler_pass in c1File.passes: |
| 45 | Logger.log(compiler_pass.name) |
| 46 | |
| 47 | |
| 48 | def DumpPass(outputFilename, passName): |
| 49 | c1File = ParseC1visualizerStream(os.path.basename(outputFilename), open(outputFilename, "r")) |
| 50 | compiler_pass = c1File.findPass(passName) |
| 51 | if compiler_pass: |
| 52 | maxLineNo = compiler_pass.startLineNo + len(compiler_pass.body) |
| 53 | lenLineNo = len(str(maxLineNo)) + 2 |
| 54 | curLineNo = compiler_pass.startLineNo |
| 55 | for line in compiler_pass.body: |
| 56 | Logger.log((str(curLineNo) + ":").ljust(lenLineNo) + line) |
| 57 | curLineNo += 1 |
| 58 | else: |
| 59 | Logger.fail("Pass \"" + passName + "\" not found in the output") |
| 60 | |
| 61 | |
| 62 | def FindCheckerFiles(path): |
| 63 | """ Returns a list of files to scan for check annotations in the given path. |
| 64 | Path to a file is returned as a single-element list, directories are |
| 65 | recursively traversed and all '.java' files returned. |
| 66 | """ |
| 67 | if not path: |
| 68 | Logger.fail("No source path provided") |
| 69 | elif os.path.isfile(path): |
| 70 | return [ path ] |
| 71 | elif os.path.isdir(path): |
| 72 | foundFiles = [] |
| 73 | for root, dirs, files in os.walk(path): |
| 74 | for file in files: |
| 75 | extension = os.path.splitext(file)[1] |
| 76 | if extension in [".java", ".smali"]: |
| 77 | foundFiles.append(os.path.join(root, file)) |
| 78 | return foundFiles |
| 79 | else: |
| 80 | Logger.fail("Source path \"" + path + "\" not found") |
| 81 | |
| 82 | |
| 83 | def RunTests(checkPrefix, checkPath, outputFilename): |
| 84 | c1File = ParseC1visualizerStream(os.path.basename(outputFilename), open(outputFilename, "r")) |
| 85 | for checkFilename in FindCheckerFiles(checkPath): |
| 86 | checkerFile = ParseCheckerStream(os.path.basename(checkFilename), |
| 87 | checkPrefix, |
| 88 | open(checkFilename, "r")) |
| 89 | MatchFiles(checkerFile, c1File) |
| 90 | |
| 91 | |
| 92 | if __name__ == "__main__": |
| 93 | args = ParseArguments() |
| 94 | |
| 95 | if args.quiet: |
| 96 | Logger.Verbosity = Logger.Level.Error |
| 97 | |
| 98 | if args.list_passes: |
| 99 | ListPasses(args.tested_file) |
| 100 | elif args.dump_pass: |
| 101 | DumpPass(args.tested_file, args.dump_pass) |
| 102 | else: |
| 103 | RunTests(args.check_prefix, args.source_path, args.tested_file) |