lint_project_xml.py 7.5 KB

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