checkpackagelib/lib_config.py: check packages alphabetical order in {Config.in, Confi...
authorJerzy Grzegorek <jerzy.m.grzegorek@gmail.com>
Tue, 11 Jun 2019 20:49:46 +0000 (22:49 +0200)
committerArnout Vandecappelle (Essensium/Mind) <arnout@mind.be>
Sat, 13 Jul 2019 22:21:26 +0000 (00:21 +0200)
Signed-off-by: Jerzy Grzegorek <jerzy.m.grzegorek@gmail.com>
[Arnout:
 - calculate level by counting - instead of with a static array;
 - new_package is only used locally, so don't make it a class member;
 - do indentation according to length of prefix;
 - don't split string in the middle of a line;
 - report first wrong package per menu;
 - do replace() only once;
 - add comment why we do replace().
]
Signed-off-by: Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>
utils/checkpackagelib/lib_config.py

index 89d44da57e9b82c5438a8fe5e8a51754350a5a4d..f0edb9993de241c56e6ba078eb30d05c4cd7e9f9 100644 (file)
@@ -60,6 +60,72 @@ class AttributesOrder(_CheckFunction):
                     text]
 
 
+class CommentsMenusPackagesOrder(_CheckFunction):
+    print_package_warning = [True, True, True, True, True, True]
+    menu_of_packages = ["", "", "", "", "", ""]
+    package = ["", "", "", "", "", ""]
+
+    def before(self):
+        self.state = ""
+
+    def get_level(self):
+        return len(self.state.split('-')) - 1
+
+    def check_line(self, lineno, text):
+        if text.startswith("comment") or text.startswith("if") or \
+           text.startswith("menu"):
+
+            if text.startswith("comment"):
+                if not self.state.endswith("-comment"):
+                    self.state += "-comment"
+
+            elif text.startswith("if") or text.startswith("menu"):
+                if text.startswith("if"):
+                    self.state += "-if"
+
+                elif text.startswith("menu"):
+                    self.state += "-menu"
+
+            level = self.get_level()
+            self.package[level] = ""
+            self.print_package_warning[level] = True
+            self.menu_of_packages[level] = text[:-1]
+
+        elif text.startswith("endif") or text.startswith("endmenu"):
+            if self.state.endswith("comment"):
+                self.state = self.state[:-8]
+
+            if text.startswith("endif"):
+                self.state = self.state[:-3]
+
+            elif text.startswith("endmenu"):
+                self.state = self.state[:-5]
+
+        elif text.startswith('\tsource "package/'):
+            level = self.get_level()
+            new_package = text[17: -(len(self.filename)-5):]
+
+            # We order _ before A, so replace it with .
+            new_package_ord = new_package.replace('_', '.')
+
+            if self.package[level] != "" and \
+               self.print_package_warning[level] and \
+               new_package_ord < self.package[level]:
+                self.print_package_warning[level] = False
+                prefix = "{}:{}: ".format(self.filename, lineno)
+                spaces = " " * len(prefix)
+                return ["{prefix}Packages in: {menu},\n"
+                        "{spaces}are not alphabetically ordered;\n"
+                        "{spaces}correct order: '-', '_', digits, capitals, lowercase;\n"
+                        "{spaces}first incorrect package: {package}"
+                        .format(prefix=prefix, spaces=spaces,
+                                menu=self.menu_of_packages[level],
+                                package=new_package),
+                        text]
+
+            self.package[level] = new_package_ord
+
+
 class HelpText(_CheckFunction):
     HELP_TEXT_FORMAT = re.compile("^\t  .{,62}$")
     URL_ONLY = re.compile("^(http|https|git)://\S*$")