check-package 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/usr/bin/env python
  2. # See utils/checkpackagelib/readme.txt before editing this file.
  3. from __future__ import print_function
  4. import argparse
  5. import inspect
  6. import os
  7. import re
  8. import sys
  9. import checkpackagelib.lib_config
  10. import checkpackagelib.lib_hash
  11. import checkpackagelib.lib_mk
  12. import checkpackagelib.lib_patch
  13. VERBOSE_LEVEL_TO_SHOW_IGNORED_FILES = 3
  14. flags = None # Command line arguments.
  15. def parse_args():
  16. parser = argparse.ArgumentParser()
  17. # Do not use argparse.FileType("r") here because only files with known
  18. # format will be open based on the filename.
  19. parser.add_argument("files", metavar="F", type=str, nargs="*",
  20. help="list of files")
  21. parser.add_argument("--br2-external", "-b", dest='intree_only', action="store_false",
  22. help="do not apply the pathname filters used for intree files")
  23. parser.add_argument("--manual-url", action="store",
  24. default="http://nightly.buildroot.org/",
  25. help="default: %(default)s")
  26. parser.add_argument("--verbose", "-v", action="count", default=0)
  27. # Now the debug options in the order they are processed.
  28. parser.add_argument("--include-only", dest="include_list", action="append",
  29. help="run only the specified functions (debug)")
  30. parser.add_argument("--exclude", dest="exclude_list", action="append",
  31. help="do not run the specified functions (debug)")
  32. parser.add_argument("--dry-run", action="store_true", help="print the "
  33. "functions that would be called for each file (debug)")
  34. return parser.parse_args()
  35. CONFIG_IN_FILENAME = re.compile("Config\.\S*$")
  36. DO_CHECK_INTREE = re.compile("|".join([
  37. "Config.in",
  38. "arch/",
  39. "boot/",
  40. "fs/",
  41. "linux/",
  42. "package/",
  43. "system/",
  44. "toolchain/",
  45. ]))
  46. DO_NOT_CHECK_INTREE = re.compile("|".join([
  47. "boot/barebox/barebox\.mk$",
  48. "fs/common\.mk$",
  49. "package/doc-asciidoc\.mk$",
  50. "package/pkg-\S*\.mk$",
  51. "toolchain/helpers\.mk$",
  52. "toolchain/toolchain-external/pkg-toolchain-external\.mk$",
  53. ]))
  54. def get_lib_from_filename(fname):
  55. if flags.intree_only:
  56. if DO_CHECK_INTREE.match(fname) is None:
  57. return None
  58. if DO_NOT_CHECK_INTREE.match(fname):
  59. return None
  60. if CONFIG_IN_FILENAME.search(fname):
  61. return checkpackagelib.lib_config
  62. if fname.endswith(".hash"):
  63. return checkpackagelib.lib_hash
  64. if fname.endswith(".mk"):
  65. return checkpackagelib.lib_mk
  66. if fname.endswith(".patch"):
  67. return checkpackagelib.lib_patch
  68. return None
  69. def is_a_check_function(m):
  70. if not inspect.isclass(m):
  71. return False
  72. # do not call the base class
  73. if m.__name__.startswith("_"):
  74. return False
  75. if flags.include_list and m.__name__ not in flags.include_list:
  76. return False
  77. if flags.exclude_list and m.__name__ in flags.exclude_list:
  78. return False
  79. return True
  80. def print_warnings(warnings):
  81. # Avoid the need to use 'return []' at the end of every check function.
  82. if warnings is None:
  83. return 0 # No warning generated.
  84. for level, message in enumerate(warnings):
  85. if flags.verbose >= level:
  86. print(message.replace("\t", "< tab >").rstrip())
  87. return 1 # One more warning to count.
  88. def check_file_using_lib(fname):
  89. # Count number of warnings generated and lines processed.
  90. nwarnings = 0
  91. nlines = 0
  92. lib = get_lib_from_filename(fname)
  93. if not lib:
  94. if flags.verbose >= VERBOSE_LEVEL_TO_SHOW_IGNORED_FILES:
  95. print("{}: ignored".format(fname))
  96. return nwarnings, nlines
  97. classes = inspect.getmembers(lib, is_a_check_function)
  98. if flags.dry_run:
  99. functions_to_run = [c[0] for c in classes]
  100. print("{}: would run: {}".format(fname, functions_to_run))
  101. return nwarnings, nlines
  102. objects = [c[1](fname, flags.manual_url) for c in classes]
  103. for cf in objects:
  104. nwarnings += print_warnings(cf.before())
  105. for lineno, text in enumerate(open(fname, "r").readlines()):
  106. nlines += 1
  107. for cf in objects:
  108. nwarnings += print_warnings(cf.check_line(lineno + 1, text))
  109. for cf in objects:
  110. nwarnings += print_warnings(cf.after())
  111. return nwarnings, nlines
  112. def __main__():
  113. global flags
  114. flags = parse_args()
  115. if flags.intree_only:
  116. # change all paths received to be relative to the base dir
  117. base_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
  118. files_to_check = [os.path.relpath(os.path.abspath(f), base_dir) for f in flags.files]
  119. # move current dir so the script find the files
  120. os.chdir(base_dir)
  121. else:
  122. files_to_check = flags.files
  123. if len(files_to_check) == 0:
  124. print("No files to check style")
  125. sys.exit(1)
  126. # Accumulate number of warnings generated and lines processed.
  127. total_warnings = 0
  128. total_lines = 0
  129. for fname in files_to_check:
  130. nwarnings, nlines = check_file_using_lib(fname)
  131. total_warnings += nwarnings
  132. total_lines += nlines
  133. # The warning messages are printed to stdout and can be post-processed
  134. # (e.g. counted by 'wc'), so for stats use stderr. Wait all warnings are
  135. # printed, for the case there are many of them, before printing stats.
  136. sys.stdout.flush()
  137. print("{} lines processed".format(total_lines), file=sys.stderr)
  138. print("{} warnings generated".format(total_warnings), file=sys.stderr)
  139. if total_warnings > 0:
  140. sys.exit(1)
  141. __main__()