check-package 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 re
  7. import six
  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("--manual-url", action="store",
  22. default="http://nightly.buildroot.org/",
  23. help="default: %(default)s")
  24. parser.add_argument("--verbose", "-v", action="count", default=0)
  25. # Now the debug options in the order they are processed.
  26. parser.add_argument("--include-only", dest="include_list", action="append",
  27. help="run only the specified functions (debug)")
  28. parser.add_argument("--exclude", dest="exclude_list", action="append",
  29. help="do not run the specified functions (debug)")
  30. parser.add_argument("--dry-run", action="store_true", help="print the "
  31. "functions that would be called for each file (debug)")
  32. return parser.parse_args()
  33. CONFIG_IN_FILENAME = re.compile("/Config\.\S*$")
  34. FILE_IS_FROM_A_PACKAGE = re.compile("package/[^/]*/")
  35. def get_lib_from_filename(fname):
  36. if FILE_IS_FROM_A_PACKAGE.search(fname) is None:
  37. return None
  38. if CONFIG_IN_FILENAME.search(fname):
  39. return checkpackagelib.lib_config
  40. if fname.endswith(".hash"):
  41. return checkpackagelib.lib_hash
  42. if fname.endswith(".mk"):
  43. return checkpackagelib.lib_mk
  44. if fname.endswith(".patch"):
  45. return checkpackagelib.lib_patch
  46. return None
  47. def is_a_check_function(m):
  48. if not inspect.isclass(m):
  49. return False
  50. # do not call the base class
  51. if m.__name__.startswith("_"):
  52. return False
  53. if flags.include_list and m.__name__ not in flags.include_list:
  54. return False
  55. if flags.exclude_list and m.__name__ in flags.exclude_list:
  56. return False
  57. return True
  58. def print_warnings(warnings):
  59. # Avoid the need to use 'return []' at the end of every check function.
  60. if warnings is None:
  61. return 0 # No warning generated.
  62. for level, message in enumerate(warnings):
  63. if flags.verbose >= level:
  64. print(message.replace("\t", "< tab >").rstrip())
  65. return 1 # One more warning to count.
  66. def check_file_using_lib(fname):
  67. # Count number of warnings generated and lines processed.
  68. nwarnings = 0
  69. nlines = 0
  70. lib = get_lib_from_filename(fname)
  71. if not lib:
  72. if flags.verbose >= VERBOSE_LEVEL_TO_SHOW_IGNORED_FILES:
  73. print("{}: ignored".format(fname))
  74. return nwarnings, nlines
  75. classes = inspect.getmembers(lib, is_a_check_function)
  76. if flags.dry_run:
  77. functions_to_run = [c[0] for c in classes]
  78. print("{}: would run: {}".format(fname, functions_to_run))
  79. return nwarnings, nlines
  80. objects = [c[1](fname, flags.manual_url) for c in classes]
  81. for cf in objects:
  82. nwarnings += print_warnings(cf.before())
  83. if six.PY3:
  84. f = open(fname, "r", errors="surrogateescape")
  85. else:
  86. f = open(fname, "r")
  87. for lineno, text in enumerate(f.readlines()):
  88. nlines += 1
  89. for cf in objects:
  90. nwarnings += print_warnings(cf.check_line(lineno + 1, text))
  91. f.close()
  92. for cf in objects:
  93. nwarnings += print_warnings(cf.after())
  94. return nwarnings, nlines
  95. def __main__():
  96. global flags
  97. flags = parse_args()
  98. if len(flags.files) == 0:
  99. print("No files to check style")
  100. sys.exit(1)
  101. # Accumulate number of warnings generated and lines processed.
  102. total_warnings = 0
  103. total_lines = 0
  104. for fname in flags.files:
  105. nwarnings, nlines = check_file_using_lib(fname)
  106. total_warnings += nwarnings
  107. total_lines += nlines
  108. # The warning messages are printed to stdout and can be post-processed
  109. # (e.g. counted by 'wc'), so for stats use stderr. Wait all warnings are
  110. # printed, for the case there are many of them, before printing stats.
  111. sys.stdout.flush()
  112. print("{} lines processed".format(total_lines), file=sys.stderr)
  113. print("{} warnings generated".format(total_warnings), file=sys.stderr)
  114. if total_warnings > 0:
  115. sys.exit(1)
  116. __main__()