check-package 6.0 KB

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