Colin Cross | 014489c | 2020-06-02 20:09:13 -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 generates project.xml and lint.xml files used to drive the Android Lint CLI tool.""" |
| 19 | |
| 20 | import argparse |
Cole Faust | 9809860 | 2024-01-17 17:11:51 -0800 | [diff] [blame] | 21 | import sys |
Jaewoong Jung | a110439 | 2021-04-19 17:38:17 -0700 | [diff] [blame] | 22 | from xml.dom import minidom |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 23 | |
Colin Cross | 9b1aa0c | 2020-06-25 17:12:28 -0700 | [diff] [blame] | 24 | from ninja_rsp import NinjaRspFileReader |
| 25 | |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 26 | |
| 27 | def check_action(check_type): |
| 28 | """ |
| 29 | Returns an action that appends a tuple of check_type and the argument to the dest. |
| 30 | """ |
| 31 | class CheckAction(argparse.Action): |
| 32 | def __init__(self, option_strings, dest, nargs=None, **kwargs): |
| 33 | if nargs is not None: |
| 34 | raise ValueError("nargs must be None, was %s" % nargs) |
| 35 | super(CheckAction, self).__init__(option_strings, dest, **kwargs) |
| 36 | def __call__(self, parser, namespace, values, option_string=None): |
| 37 | checks = getattr(namespace, self.dest, []) |
| 38 | checks.append((check_type, values)) |
| 39 | setattr(namespace, self.dest, checks) |
| 40 | return CheckAction |
| 41 | |
| 42 | |
| 43 | def parse_args(): |
| 44 | """Parse commandline arguments.""" |
| 45 | |
| 46 | def convert_arg_line_to_args(arg_line): |
| 47 | for arg in arg_line.split(): |
| 48 | if arg.startswith('#'): |
| 49 | return |
| 50 | if not arg.strip(): |
| 51 | continue |
| 52 | yield arg |
| 53 | |
| 54 | parser = argparse.ArgumentParser(fromfile_prefix_chars='@') |
| 55 | parser.convert_arg_line_to_args = convert_arg_line_to_args |
| 56 | parser.add_argument('--project_out', dest='project_out', |
| 57 | help='file to which the project.xml contents will be written.') |
| 58 | parser.add_argument('--config_out', dest='config_out', |
| 59 | help='file to which the lint.xml contents will be written.') |
| 60 | parser.add_argument('--name', dest='name', |
| 61 | help='name of the module.') |
| 62 | parser.add_argument('--srcs', dest='srcs', action='append', default=[], |
| 63 | help='file containing whitespace separated list of source files.') |
| 64 | parser.add_argument('--generated_srcs', dest='generated_srcs', action='append', default=[], |
| 65 | help='file containing whitespace separated list of generated source files.') |
| 66 | parser.add_argument('--resources', dest='resources', action='append', default=[], |
| 67 | help='file containing whitespace separated list of resource files.') |
| 68 | parser.add_argument('--classes', dest='classes', action='append', default=[], |
| 69 | help='file containing the module\'s classes.') |
| 70 | parser.add_argument('--classpath', dest='classpath', action='append', default=[], |
| 71 | help='file containing classes from dependencies.') |
| 72 | parser.add_argument('--extra_checks_jar', dest='extra_checks_jars', action='append', default=[], |
| 73 | help='file containing extra lint checks.') |
| 74 | parser.add_argument('--manifest', dest='manifest', |
| 75 | help='file containing the module\'s manifest.') |
| 76 | parser.add_argument('--merged_manifest', dest='merged_manifest', |
| 77 | help='file containing merged manifest for the module and its dependencies.') |
Jaewoong Jung | a110439 | 2021-04-19 17:38:17 -0700 | [diff] [blame] | 78 | parser.add_argument('--baseline', dest='baseline_path', |
| 79 | help='file containing baseline lint issues.') |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 80 | parser.add_argument('--library', dest='library', action='store_true', |
| 81 | help='mark the module as a library.') |
| 82 | parser.add_argument('--test', dest='test', action='store_true', |
| 83 | help='mark the module as a test.') |
| 84 | parser.add_argument('--cache_dir', dest='cache_dir', |
| 85 | help='directory to use for cached file.') |
Colin Cross | c31efeb | 2020-06-23 10:25:26 -0700 | [diff] [blame] | 86 | parser.add_argument('--root_dir', dest='root_dir', |
| 87 | help='directory to use for root dir.') |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 88 | group = parser.add_argument_group('check arguments', 'later arguments override earlier ones.') |
| 89 | group.add_argument('--fatal_check', dest='checks', action=check_action('fatal'), default=[], |
| 90 | help='treat a lint issue as a fatal error.') |
| 91 | group.add_argument('--error_check', dest='checks', action=check_action('error'), default=[], |
| 92 | help='treat a lint issue as an error.') |
| 93 | group.add_argument('--warning_check', dest='checks', action=check_action('warning'), default=[], |
| 94 | help='treat a lint issue as a warning.') |
| 95 | group.add_argument('--disable_check', dest='checks', action=check_action('ignore'), default=[], |
| 96 | help='disable a lint issue.') |
Jaewoong Jung | a110439 | 2021-04-19 17:38:17 -0700 | [diff] [blame] | 97 | group.add_argument('--disallowed_issues', dest='disallowed_issues', default=[], |
| 98 | help='lint issues disallowed in the baseline file') |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 99 | return parser.parse_args() |
| 100 | |
| 101 | |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 102 | def write_project_xml(f, args): |
| 103 | test_attr = "test='true' " if args.test else "" |
| 104 | |
| 105 | f.write("<?xml version='1.0' encoding='utf-8'?>\n") |
| 106 | f.write("<project>\n") |
Colin Cross | c31efeb | 2020-06-23 10:25:26 -0700 | [diff] [blame] | 107 | if args.root_dir: |
| 108 | f.write(" <root dir='%s' />\n" % args.root_dir) |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 109 | f.write(" <module name='%s' android='true' %sdesugar='full' >\n" % (args.name, "library='true' " if args.library else "")) |
| 110 | if args.manifest: |
| 111 | f.write(" <manifest file='%s' %s/>\n" % (args.manifest, test_attr)) |
| 112 | if args.merged_manifest: |
| 113 | f.write(" <merged-manifest file='%s' %s/>\n" % (args.merged_manifest, test_attr)) |
| 114 | for src_file in args.srcs: |
| 115 | for src in NinjaRspFileReader(src_file): |
| 116 | f.write(" <src file='%s' %s/>\n" % (src, test_attr)) |
| 117 | for src_file in args.generated_srcs: |
| 118 | for src in NinjaRspFileReader(src_file): |
| 119 | f.write(" <src file='%s' generated='true' %s/>\n" % (src, test_attr)) |
| 120 | for res_file in args.resources: |
| 121 | for res in NinjaRspFileReader(res_file): |
| 122 | f.write(" <resource file='%s' %s/>\n" % (res, test_attr)) |
| 123 | for classes in args.classes: |
| 124 | f.write(" <classes jar='%s' />\n" % classes) |
| 125 | for classpath in args.classpath: |
| 126 | f.write(" <classpath jar='%s' />\n" % classpath) |
| 127 | for extra in args.extra_checks_jars: |
| 128 | f.write(" <lint-checks jar='%s' />\n" % extra) |
| 129 | f.write(" </module>\n") |
| 130 | if args.cache_dir: |
| 131 | f.write(" <cache dir='%s'/>\n" % args.cache_dir) |
| 132 | f.write("</project>\n") |
| 133 | |
| 134 | |
| 135 | def write_config_xml(f, args): |
| 136 | f.write("<?xml version='1.0' encoding='utf-8'?>\n") |
| 137 | f.write("<lint>\n") |
| 138 | for check in args.checks: |
| 139 | f.write(" <issue id='%s' severity='%s' />\n" % (check[1], check[0])) |
| 140 | f.write("</lint>\n") |
| 141 | |
| 142 | |
Jaewoong Jung | a110439 | 2021-04-19 17:38:17 -0700 | [diff] [blame] | 143 | def check_baseline_for_disallowed_issues(baseline, forced_checks): |
| 144 | issues_element = baseline.documentElement |
| 145 | if issues_element.tagName != 'issues': |
| 146 | raise RuntimeError('expected issues tag at root') |
| 147 | issues = issues_element.getElementsByTagName('issue') |
Jaewoong Jung | 3c87b1d | 2021-04-22 11:01:36 -0700 | [diff] [blame] | 148 | disallowed = set() |
Jaewoong Jung | a110439 | 2021-04-19 17:38:17 -0700 | [diff] [blame] | 149 | for issue in issues: |
| 150 | id = issue.getAttribute('id') |
| 151 | if id in forced_checks: |
Jaewoong Jung | 3c87b1d | 2021-04-22 11:01:36 -0700 | [diff] [blame] | 152 | disallowed.add(id) |
| 153 | return disallowed |
Jaewoong Jung | a110439 | 2021-04-19 17:38:17 -0700 | [diff] [blame] | 154 | |
| 155 | |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 156 | def main(): |
| 157 | """Program entry point.""" |
| 158 | args = parse_args() |
| 159 | |
Jaewoong Jung | a110439 | 2021-04-19 17:38:17 -0700 | [diff] [blame] | 160 | if args.baseline_path: |
| 161 | baseline = minidom.parse(args.baseline_path) |
Jaewoong Jung | 3c87b1d | 2021-04-22 11:01:36 -0700 | [diff] [blame] | 162 | disallowed_issues = check_baseline_for_disallowed_issues(baseline, args.disallowed_issues) |
Cole Faust | 9809860 | 2024-01-17 17:11:51 -0800 | [diff] [blame] | 163 | if disallowed_issues: |
| 164 | sys.exit('disallowed issues %s found in lint baseline file %s for module %s' |
Jaewoong Jung | 3c87b1d | 2021-04-22 11:01:36 -0700 | [diff] [blame] | 165 | % (disallowed_issues, args.baseline_path, args.name)) |
Jaewoong Jung | a110439 | 2021-04-19 17:38:17 -0700 | [diff] [blame] | 166 | |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 167 | if args.project_out: |
| 168 | with open(args.project_out, 'w') as f: |
| 169 | write_project_xml(f, args) |
| 170 | |
| 171 | if args.config_out: |
| 172 | with open(args.config_out, 'w') as f: |
| 173 | write_config_xml(f, args) |
| 174 | |
| 175 | |
| 176 | if __name__ == '__main__': |
| 177 | main() |