2
1

lib_config.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. # See utils/checkpackagelib/readme.txt before editing this file.
  2. # Kconfig generates errors if someone introduces a typo like "boool" instead of
  3. # "bool", so below check functions don't need to check for things already
  4. # checked by running "make menuconfig".
  5. import re
  6. from checkpackagelib.base import _CheckFunction
  7. from checkpackagelib.lib import ConsecutiveEmptyLines # noqa: F401
  8. from checkpackagelib.lib import EmptyLastLine # noqa: F401
  9. from checkpackagelib.lib import NewlineAtEof # noqa: F401
  10. from checkpackagelib.lib import TrailingSpace # noqa: F401
  11. def _empty_or_comment(text):
  12. line = text.strip()
  13. # ignore empty lines and comment lines indented or not
  14. return line == "" or line.startswith("#")
  15. def _part_of_help_text(text):
  16. return text.startswith("\t ")
  17. # used in more than one check
  18. entries_that_should_not_be_indented = [
  19. "choice", "comment", "config", "endchoice", "endif", "endmenu", "if",
  20. "menu", "menuconfig", "source"]
  21. class AttributesOrder(_CheckFunction):
  22. attributes_order_convention = {
  23. "bool": 1, "prompt": 1, "string": 1, "default": 2, "depends": 3,
  24. "select": 4, "help": 5}
  25. def before(self):
  26. self.state = 0
  27. def check_line(self, lineno, text):
  28. if _empty_or_comment(text) or _part_of_help_text(text):
  29. return
  30. attribute = text.split()[0]
  31. if attribute in entries_that_should_not_be_indented:
  32. self.state = 0
  33. return
  34. if attribute not in self.attributes_order_convention.keys():
  35. return
  36. new_state = self.attributes_order_convention[attribute]
  37. wrong_order = self.state > new_state
  38. # save to process next line
  39. self.state = new_state
  40. if wrong_order:
  41. return ["{}:{}: attributes order: type, default, depends on,"
  42. " select, help ({}#_config_files)"
  43. .format(self.filename, lineno, self.url_to_manual),
  44. text]
  45. class CommentsMenusPackagesOrder(_CheckFunction):
  46. menu_of_packages = [""]
  47. package = [""]
  48. print_package_warning = [True]
  49. def before(self):
  50. self.state = ""
  51. self.level = 0
  52. def get_level(self):
  53. return len(self.state.split('-')) - 1
  54. def check_line(self, lineno, text):
  55. # We only want to force sorting for the top-level menus
  56. if self.filename not in ["package/Config.in",
  57. "package/Config.in.host"]:
  58. return
  59. source_line = re.match(r'^\s*source ".*/([^/]*)/Config.in(.host)?"', text)
  60. if text.startswith("comment ") or text.startswith("if ") or \
  61. text.startswith("menu "):
  62. if text.startswith("comment"):
  63. if not self.state.endswith("-comment"):
  64. self.state += "-comment"
  65. elif text.startswith("if") or text.startswith("menu"):
  66. if text.startswith("if"):
  67. self.state += "-if"
  68. elif text.startswith("menu"):
  69. self.state += "-menu"
  70. self.level = self.get_level()
  71. try:
  72. self.menu_of_packages[self.level] = text[:-1]
  73. self.package[self.level] = ""
  74. self.print_package_warning[self.level] = True
  75. except IndexError:
  76. self.menu_of_packages.append(text[:-1])
  77. self.package.append("")
  78. self.print_package_warning.append(True)
  79. elif text.startswith("endif") or text.startswith("endmenu"):
  80. if self.state.endswith("comment"):
  81. self.state = self.state[:-8]
  82. if text.startswith("endif"):
  83. self.state = self.state[:-3]
  84. elif text.startswith("endmenu"):
  85. self.state = self.state[:-5]
  86. self.level = self.get_level()
  87. elif source_line:
  88. new_package = source_line.group(1)
  89. # We order _ before A, so replace it with .
  90. new_package_ord = new_package.replace('_', '.')
  91. if self.package[self.level] != "" and \
  92. self.print_package_warning[self.level] and \
  93. new_package_ord < self.package[self.level]:
  94. self.print_package_warning[self.level] = False
  95. prefix = "{}:{}: ".format(self.filename, lineno)
  96. spaces = " " * len(prefix)
  97. return ["{prefix}Packages in: {menu},\n"
  98. "{spaces}are not alphabetically ordered;\n"
  99. "{spaces}correct order: '-', '_', digits, capitals, lowercase;\n"
  100. "{spaces}first incorrect package: {package}"
  101. .format(prefix=prefix, spaces=spaces,
  102. menu=self.menu_of_packages[self.level],
  103. package=new_package),
  104. text]
  105. self.package[self.level] = new_package_ord
  106. class HelpText(_CheckFunction):
  107. HELP_TEXT_FORMAT = re.compile("^\t .{,62}$")
  108. URL_ONLY = re.compile("^(http|https|git)://\S*$")
  109. def before(self):
  110. self.help_text = False
  111. def check_line(self, lineno, text):
  112. if _empty_or_comment(text):
  113. return
  114. entry = text.split()[0]
  115. if entry in entries_that_should_not_be_indented:
  116. self.help_text = False
  117. return
  118. if text.strip() == "help":
  119. self.help_text = True
  120. return
  121. if not self.help_text:
  122. return
  123. if self.HELP_TEXT_FORMAT.match(text.rstrip()):
  124. return
  125. if self.URL_ONLY.match(text.strip()):
  126. return
  127. return ["{}:{}: help text: <tab><2 spaces><62 chars>"
  128. " ({}#writing-rules-config-in)"
  129. .format(self.filename, lineno, self.url_to_manual),
  130. text,
  131. "\t " + "123456789 " * 6 + "12"]
  132. class Indent(_CheckFunction):
  133. ENDS_WITH_BACKSLASH = re.compile(r"^[^#].*\\$")
  134. entries_that_should_be_indented = [
  135. "bool", "default", "depends", "help", "prompt", "select", "string"]
  136. def before(self):
  137. self.backslash = False
  138. def check_line(self, lineno, text):
  139. if _empty_or_comment(text) or _part_of_help_text(text):
  140. self.backslash = False
  141. return
  142. entry = text.split()[0]
  143. last_line_ends_in_backslash = self.backslash
  144. # calculate for next line
  145. if self.ENDS_WITH_BACKSLASH.search(text):
  146. self.backslash = True
  147. else:
  148. self.backslash = False
  149. if last_line_ends_in_backslash:
  150. if text.startswith("\t"):
  151. return
  152. return ["{}:{}: continuation line should be indented using tabs"
  153. .format(self.filename, lineno),
  154. text]
  155. if entry in self.entries_that_should_be_indented:
  156. if not text.startswith("\t{}".format(entry)):
  157. return ["{}:{}: should be indented with one tab"
  158. " ({}#_config_files)"
  159. .format(self.filename, lineno, self.url_to_manual),
  160. text]
  161. elif entry in entries_that_should_not_be_indented:
  162. if not text.startswith(entry):
  163. # four Config.in files have a special but legitimate indentation rule
  164. if self.filename in ["package/Config.in",
  165. "package/Config.in.host",
  166. "package/kodi/Config.in",
  167. "package/x11r7/Config.in"]:
  168. return
  169. return ["{}:{}: should not be indented"
  170. .format(self.filename, lineno),
  171. text]