kumquat-buildroot/utils/checkpackagelib/tool.py
Ricardo Martincoski 9df6503ed0 Makefile: merge check-flake8 into check-package
Teach check-package to detect python files by type and check them using
flake8.
Do not use subprocess to call 'python3 -m flake8' in order to avoid too
many spawned shells, which in its turn would slow down the check for
multiple files. (make check-package takes twice the time using a shell
for each flake8 call, when compared of importing the main application)

Expand the runtime test and the unit tests for check-package.

Remove check-flake8 from the makefile and also from the GitLab CI
because the exact same checks become part of check-package.

Suggested-by: Arnout Vandecappelle <arnout@mind.be>
Signed-off-by: Ricardo Martincoski <ricardo.martincoski@gmail.com>
[Arnout: add a comment to x-python to explain its purpose]
Signed-off-by: Arnout Vandecappelle <arnout@mind.be>
2023-04-09 22:33:24 +02:00

45 lines
1.6 KiB
Python

import flake8.main.application
import os
import subprocess
import tempfile
from checkpackagelib.base import _Tool
class NotExecutable(_Tool):
def ignore(self):
return False
def run(self):
if self.ignore():
return
if os.access(self.filename, os.X_OK):
return ["{}:0: This file does not need to be executable{}".format(self.filename, self.hint())]
class Flake8(_Tool):
def run(self):
with tempfile.NamedTemporaryFile() as output:
app = flake8.main.application.Application()
app.run(['--output-file={}'.format(output.name), self.filename])
stdout = output.readlines()
processed_output = [str(line.decode().rstrip()) for line in stdout if line]
if len(stdout) == 0:
return
return ["{}:0: run 'flake8' and fix the warnings".format(self.filename),
'\n'.join(processed_output)]
class Shellcheck(_Tool):
def run(self):
cmd = ['shellcheck', self.filename]
try:
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout = p.communicate()[0]
processed_output = [str(line.decode().rstrip()) for line in stdout.splitlines() if line]
if p.returncode == 0:
return
return ["{}:0: run 'shellcheck' and fix the warnings".format(self.filename),
'\n'.join(processed_output)]
except FileNotFoundError:
return ["{}:0: failed to call 'shellcheck'".format(self.filename)]