[libcamera-devel] [PATCH] utils: checkstyle.py: Add pep8 checker
Kieran Bingham
kieran.bingham at ideasonboard.com
Thu Jul 4 11:50:07 CEST 2019
On 04/07/2019 10:35, Kieran Bingham wrote:
> Process python additions with pep8 and report any errors that are added.
>
> Signed-off-by: Kieran Bingham <kieran.bingham at ideasonboard.com>
> ---
> utils/checkstyle.py | 29 +++++++++++++++++++++++++++++
> 1 file changed, 29 insertions(+)
>
> This checker allows the checkstyle script to self-identify any issue.
> Isn't that recursivly fun?
>
>
> Todo:
> - This should not fail if pep8 is not present, and should instead report an
> issue that pep8 is not installed.
For reference, this was actually easy to add.
The following is now in v2, but I'll await any further comments before
reposting.
> - ret = subprocess.run(['pep8', '--ignore=E501', '-'],
> - input=self.__data, stdout=subprocess.PIPE)
> + try:
> + ret = subprocess.run(['pep8', '--ignore=E501', '-'],
> + input=self.__data, stdout=subprocess.PIPE)
> + except FileNotFoundError:
> + issues.append(StyleIssue(0, "", "Please install pep8 to validate python additions"))
> + return issues
> - The line_no_regex should ideally be compiled once per class, or globally?
> But how then do you access a class object from the class instance...
> - All of the pep8 failures in checkstyle.py should be fixed up ...
>
> diff --git a/utils/checkstyle.py b/utils/checkstyle.py
> index fab4b116d2ff..a960affc71a9 100755
> --- a/utils/checkstyle.py
> +++ b/utils/checkstyle.py
> @@ -276,6 +276,35 @@ class MesonChecker(StyleChecker):
> return issues
>
>
> +class Pep8Checker(StyleChecker):
> + patterns = ('*.py',)
> +
> + def __init__(self, content):
> + super().__init__()
> + self.__content = content
> + self.__data = "".join(content).encode('utf-8')
> +
> + def check(self, line_numbers):
> + issues = []
> + line_no_regex = re.compile('stdin:([0-9]+):([0-9]+)(.*)')
> +
> + ret = subprocess.run(['pep8', '--ignore=E501', '-'],
> + input=self.__data, stdout=subprocess.PIPE)
> +
> + results = ret.stdout.decode('utf-8').splitlines()
> + for item in results:
> + search = re.search(line_no_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
> #
>
--
Regards
--
Kieran
More information about the libcamera-devel
mailing list