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 20import argparse 21 22from ninja_rsp import NinjaRspFileReader 23 24 25def check_action(check_type): 26 """ 27 Returns an action that appends a tuple of check_type and the argument to the dest. 28 """ 29 class CheckAction(argparse.Action): 30 def __init__(self, option_strings, dest, nargs=None, **kwargs): 31 if nargs is not None: 32 raise ValueError("nargs must be None, was %s" % nargs) 33 super(CheckAction, self).__init__(option_strings, dest, **kwargs) 34 def __call__(self, parser, namespace, values, option_string=None): 35 checks = getattr(namespace, self.dest, []) 36 checks.append((check_type, values)) 37 setattr(namespace, self.dest, checks) 38 return CheckAction 39 40 41def parse_args(): 42 """Parse commandline arguments.""" 43 44 def convert_arg_line_to_args(arg_line): 45 for arg in arg_line.split(): 46 if arg.startswith('#'): 47 return 48 if not arg.strip(): 49 continue 50 yield arg 51 52 parser = argparse.ArgumentParser(fromfile_prefix_chars='@') 53 parser.convert_arg_line_to_args = convert_arg_line_to_args 54 parser.add_argument('--project_out', dest='project_out', 55 help='file to which the project.xml contents will be written.') 56 parser.add_argument('--config_out', dest='config_out', 57 help='file to which the lint.xml contents will be written.') 58 parser.add_argument('--name', dest='name', 59 help='name of the module.') 60 parser.add_argument('--srcs', dest='srcs', action='append', default=[], 61 help='file containing whitespace separated list of source files.') 62 parser.add_argument('--generated_srcs', dest='generated_srcs', action='append', default=[], 63 help='file containing whitespace separated list of generated source files.') 64 parser.add_argument('--resources', dest='resources', action='append', default=[], 65 help='file containing whitespace separated list of resource files.') 66 parser.add_argument('--classes', dest='classes', action='append', default=[], 67 help='file containing the module\'s classes.') 68 parser.add_argument('--classpath', dest='classpath', action='append', default=[], 69 help='file containing classes from dependencies.') 70 parser.add_argument('--extra_checks_jar', dest='extra_checks_jars', action='append', default=[], 71 help='file containing extra lint checks.') 72 parser.add_argument('--manifest', dest='manifest', 73 help='file containing the module\'s manifest.') 74 parser.add_argument('--merged_manifest', dest='merged_manifest', 75 help='file containing merged manifest for the module and its dependencies.') 76 parser.add_argument('--library', dest='library', action='store_true', 77 help='mark the module as a library.') 78 parser.add_argument('--test', dest='test', action='store_true', 79 help='mark the module as a test.') 80 parser.add_argument('--cache_dir', dest='cache_dir', 81 help='directory to use for cached file.') 82 parser.add_argument('--root_dir', dest='root_dir', 83 help='directory to use for root dir.') 84 group = parser.add_argument_group('check arguments', 'later arguments override earlier ones.') 85 group.add_argument('--fatal_check', dest='checks', action=check_action('fatal'), default=[], 86 help='treat a lint issue as a fatal error.') 87 group.add_argument('--error_check', dest='checks', action=check_action('error'), default=[], 88 help='treat a lint issue as an error.') 89 group.add_argument('--warning_check', dest='checks', action=check_action('warning'), default=[], 90 help='treat a lint issue as a warning.') 91 group.add_argument('--disable_check', dest='checks', action=check_action('ignore'), default=[], 92 help='disable a lint issue.') 93 return parser.parse_args() 94 95 96def write_project_xml(f, args): 97 test_attr = "test='true' " if args.test else "" 98 99 f.write("<?xml version='1.0' encoding='utf-8'?>\n") 100 f.write("<project>\n") 101 if args.root_dir: 102 f.write(" <root dir='%s' />\n" % args.root_dir) 103 f.write(" <module name='%s' android='true' %sdesugar='full' >\n" % (args.name, "library='true' " if args.library else "")) 104 if args.manifest: 105 f.write(" <manifest file='%s' %s/>\n" % (args.manifest, test_attr)) 106 if args.merged_manifest: 107 f.write(" <merged-manifest file='%s' %s/>\n" % (args.merged_manifest, test_attr)) 108 for src_file in args.srcs: 109 for src in NinjaRspFileReader(src_file): 110 f.write(" <src file='%s' %s/>\n" % (src, test_attr)) 111 for src_file in args.generated_srcs: 112 for src in NinjaRspFileReader(src_file): 113 f.write(" <src file='%s' generated='true' %s/>\n" % (src, test_attr)) 114 for res_file in args.resources: 115 for res in NinjaRspFileReader(res_file): 116 f.write(" <resource file='%s' %s/>\n" % (res, test_attr)) 117 for classes in args.classes: 118 f.write(" <classes jar='%s' />\n" % classes) 119 for classpath in args.classpath: 120 f.write(" <classpath jar='%s' />\n" % classpath) 121 for extra in args.extra_checks_jars: 122 f.write(" <lint-checks jar='%s' />\n" % extra) 123 f.write(" </module>\n") 124 if args.cache_dir: 125 f.write(" <cache dir='%s'/>\n" % args.cache_dir) 126 f.write("</project>\n") 127 128 129def write_config_xml(f, args): 130 f.write("<?xml version='1.0' encoding='utf-8'?>\n") 131 f.write("<lint>\n") 132 for check in args.checks: 133 f.write(" <issue id='%s' severity='%s' />\n" % (check[1], check[0])) 134 f.write("</lint>\n") 135 136 137def main(): 138 """Program entry point.""" 139 args = parse_args() 140 141 if args.project_out: 142 with open(args.project_out, 'w') as f: 143 write_project_xml(f, args) 144 145 if args.config_out: 146 with open(args.config_out, 'w') as f: 147 write_config_xml(f, args) 148 149 150if __name__ == '__main__': 151 main() 152