[libcamera-devel] [PATCH v2] utils: checkstyle.py: Add pep8 checker

Kieran Bingham kieran.bingham at ideasonboard.com
Thu Jul 4 17:06:58 CEST 2019


Process python additions with pep8 and report any errors that are added.

Signed-off-by: Kieran Bingham <kieran.bingham at ideasonboard.com>

--
v2:
 - Now catches if pep8 is not available
 - line_no_regex renamed to results_regex and contained in Pep8Checker
   class
 - Single quotes used instead of double quotes
 - 'data' moved to local variable instance of class instance
---
 utils/checkstyle.py | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/utils/checkstyle.py b/utils/checkstyle.py
index fab4b116d2ff..c5ecdc12112b 100755
--- a/utils/checkstyle.py
+++ b/utils/checkstyle.py
@@ -276,6 +276,39 @@ class MesonChecker(StyleChecker):
         return issues
 
 
+class Pep8Checker(StyleChecker):
+    patterns = ('*.py',)
+    results_regex = re.compile('stdin:([0-9]+):([0-9]+)(.*)')
+
+    def __init__(self, content):
+        super().__init__()
+        self.__content = content
+
+    def check(self, line_numbers):
+        issues = []
+        data = ''.join(self.__content).encode('utf-8')
+
+        try:
+            ret = subprocess.run(['pep8', '--ignore=E501', '-'],
+                                 input=data, stdout=subprocess.PIPE)
+        except FileNotFoundError:
+            issues.append(StyleIssue(0, '', "Please install pep8 to validate python additions"))
+            return issues
+
+        results = ret.stdout.decode('utf-8').splitlines()
+        for item in results:
+            search = re.search(Pep8Checker.results_regex, item)
+            line_number = int(search.group(1))
+            position = int(search.group(2))
+            msg = search.group(3)
+
+            if line_number in line_numbers:
+                line = self.__content[line_number - 1]
+                issues.append(StyleIssue(line_number, line, msg))
+
+        return issues
+
+
 # ------------------------------------------------------------------------------
 # Formatters
 #
-- 
2.20.1



More information about the libcamera-devel mailing list