From 021af795c298d10317c2931dc5ba05d887c49ab6 Mon Sep 17 00:00:00 2001 From: Kieran Bingham Date: Thu, 4 Jul 2019 10:25:18 +0100 Subject: utils: checkstyle.py: Add pep8 checker Process python additions with pep8 and report any errors that are added. Reviewed-by: Laurent Pinchart Signed-off-by: Kieran Bingham --- utils/checkstyle.py | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/utils/checkstyle.py b/utils/checkstyle.py index fab4b116..42a96f6d 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, None, "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 # @@ -450,7 +483,8 @@ def check_file(top_level, commit, filename): issues = sorted(issues, key=lambda i: i.line_number) for issue in issues: print('%s#%u: %s' % (Colours.fg(Colours.Yellow), issue.line_number, issue.msg)) - print('+%s%s' % (issue.line.rstrip(), Colours.reset())) + if issue.line is not None: + print('+%s%s' % (issue.line.rstrip(), Colours.reset())) return len(formatted_diff) + len(issues) -- cgit v1.2.1