]> rtime.felk.cvut.cz Git - coffee/buildroot.git/blob - utils/checkpackagelib/lib_config.py
check-package: fix code style
[coffee/buildroot.git] / utils / checkpackagelib / lib_config.py
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
6 import re
7
8 from base import _CheckFunction
9 from lib import ConsecutiveEmptyLines  # noqa: F401
10 from lib import EmptyLastLine          # noqa: F401
11 from lib import NewlineAtEof           # noqa: F401
12 from lib import TrailingSpace          # noqa: F401
13
14
15 def _empty_or_comment(text):
16     line = text.strip()
17     # ignore empty lines and comment lines indented or not
18     return line == "" or line.startswith("#")
19
20
21 def _part_of_help_text(text):
22     return text.startswith("\t  ")
23
24
25 # used in more than one check
26 entries_that_should_not_be_indented = [
27     "choice", "comment", "config", "endchoice", "endif", "endmenu", "if",
28     "menu", "menuconfig", "source"]
29
30
31 class AttributesOrder(_CheckFunction):
32     attributes_order_convention = {
33         "bool": 1, "prompt": 1, "string": 1, "default": 2, "depends": 3,
34         "select": 4, "help": 5}
35
36     def before(self):
37         self.state = 0
38
39     def check_line(self, lineno, text):
40         if _empty_or_comment(text) or _part_of_help_text(text):
41             return
42
43         attribute = text.split()[0]
44
45         if attribute in entries_that_should_not_be_indented:
46             self.state = 0
47             return
48         if attribute not in self.attributes_order_convention.keys():
49             return
50         new_state = self.attributes_order_convention[attribute]
51         wrong_order = self.state > new_state
52
53         # save to process next line
54         self.state = new_state
55
56         if wrong_order:
57             return ["{}:{}: attributes order: type, default, depends on,"
58                     " select, help ({}#_config_files)"
59                     .format(self.filename, lineno, self.url_to_manual),
60                     text]
61
62
63 class HelpText(_CheckFunction):
64     HELP_TEXT_FORMAT = re.compile("^\t  .{,62}$")
65     URL_ONLY = re.compile("^(http|https|git)://\S*$")
66
67     def before(self):
68         self.help_text = False
69
70     def check_line(self, lineno, text):
71         if _empty_or_comment(text):
72             return
73
74         entry = text.split()[0]
75
76         if entry in entries_that_should_not_be_indented:
77             self.help_text = False
78             return
79         if text.strip() == "help":
80             self.help_text = True
81             return
82
83         if not self.help_text:
84             return
85
86         if self.HELP_TEXT_FORMAT.match(text.rstrip()):
87             return
88         if self.URL_ONLY.match(text.strip()):
89             return
90         return ["{}:{}: help text: <tab><2 spaces><62 chars>"
91                 " ({}#writing-rules-config-in)"
92                 .format(self.filename, lineno, self.url_to_manual),
93                 text,
94                 "\t  " + "123456789 " * 6 + "12"]
95
96
97 class Indent(_CheckFunction):
98     ENDS_WITH_BACKSLASH = re.compile(r"^[^#].*\\$")
99     entries_that_should_be_indented = [
100         "bool", "default", "depends", "help", "prompt", "select", "string"]
101
102     def before(self):
103         self.backslash = False
104
105     def check_line(self, lineno, text):
106         if _empty_or_comment(text) or _part_of_help_text(text):
107             self.backslash = False
108             return
109
110         entry = text.split()[0]
111
112         last_line_ends_in_backslash = self.backslash
113
114         # calculate for next line
115         if self.ENDS_WITH_BACKSLASH.search(text):
116             self.backslash = True
117         else:
118             self.backslash = False
119
120         if last_line_ends_in_backslash:
121             if text.startswith("\t"):
122                 return
123             return ["{}:{}: continuation line should be indented using tabs"
124                     .format(self.filename, lineno),
125                     text]
126
127         if entry in self.entries_that_should_be_indented:
128             if not text.startswith("\t{}".format(entry)):
129                 return ["{}:{}: should be indented with one tab"
130                         " ({}#_config_files)"
131                         .format(self.filename, lineno, self.url_to_manual),
132                         text]
133         elif entry in entries_that_should_not_be_indented:
134             if not text.startswith(entry):
135                 return ["{}:{}: should not be indented"
136                         .format(self.filename, lineno),
137                         text]