Colin Cross | 8742735 | 2024-09-25 15:41:19 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (C) 2018 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 | |
| 18 | """This file checks baselines passed to Android Lint for checks that must not be baselined.""" |
| 19 | |
| 20 | import argparse |
| 21 | import sys |
| 22 | from xml.dom import minidom |
| 23 | |
| 24 | from ninja_rsp import NinjaRspFileReader |
| 25 | |
| 26 | |
| 27 | def parse_args(): |
| 28 | """Parse commandline arguments.""" |
| 29 | |
| 30 | def convert_arg_line_to_args(arg_line): |
| 31 | for arg in arg_line.split(): |
| 32 | if arg.startswith('#'): |
| 33 | return |
| 34 | if not arg.strip(): |
| 35 | continue |
| 36 | yield arg |
| 37 | |
| 38 | parser = argparse.ArgumentParser(fromfile_prefix_chars='@') |
| 39 | parser.convert_arg_line_to_args = convert_arg_line_to_args |
| 40 | parser.add_argument('--name', dest='name', |
| 41 | help='name of the module.') |
| 42 | parser.add_argument('--baselines', dest='baselines', action='append', default=[], |
| 43 | help='file containing whitespace separated list of baseline files.') |
| 44 | parser.add_argument('--disallowed_issues', dest='disallowed_issues', default=[], |
| 45 | help='lint issues disallowed in the baseline file') |
| 46 | return parser.parse_args() |
| 47 | |
| 48 | |
| 49 | def check_baseline_for_disallowed_issues(baseline, forced_checks): |
| 50 | issues_element = baseline.documentElement |
| 51 | if issues_element.tagName != 'issues': |
| 52 | raise RuntimeError('expected issues tag at root') |
| 53 | issues = issues_element.getElementsByTagName('issue') |
| 54 | disallowed = set() |
| 55 | for issue in issues: |
| 56 | id = issue.getAttribute('id') |
| 57 | if id in forced_checks: |
| 58 | disallowed.add(id) |
| 59 | return disallowed |
| 60 | |
| 61 | |
| 62 | def main(): |
| 63 | """Program entry point.""" |
| 64 | args = parse_args() |
| 65 | |
| 66 | error = False |
| 67 | for baseline_rsp_file in args.baselines: |
| 68 | for baseline_path in NinjaRspFileReader(baseline_rsp_file): |
| 69 | baseline = minidom.parse(baseline_path) |
| 70 | disallowed_issues = check_baseline_for_disallowed_issues(baseline, args.disallowed_issues) |
| 71 | if disallowed_issues: |
| 72 | print('disallowed issues %s found in lint baseline file %s for module %s' |
| 73 | % (disallowed_issues, baseline_path, args.name)) |
| 74 | error = True |
| 75 | |
| 76 | if error: |
| 77 | sys.exit(1) |
| 78 | |
| 79 | |
| 80 | if __name__ == '__main__': |
| 81 | main() |