1import flake8.main.application
2import os
3import subprocess
4import tempfile
5from checkpackagelib.base import _Tool
6
7
8class NotExecutable(_Tool):
9    def ignore(self):
10        return False
11
12    def run(self):
13        if self.ignore():
14            return
15        if os.access(self.filename, os.X_OK):
16            return ["{}:0: This file does not need to be executable{}".format(self.filename, self.hint())]
17
18
19class Flake8(_Tool):
20    def run(self):
21        with tempfile.NamedTemporaryFile() as output:
22            app = flake8.main.application.Application()
23            app.run(['--output-file={}'.format(output.name), self.filename])
24            stdout = output.readlines()
25            processed_output = [str(line.decode().rstrip()) for line in stdout if line]
26            if len(stdout) == 0:
27                return
28            return ["{}:0: run 'flake8' and fix the warnings".format(self.filename),
29                    '\n'.join(processed_output)]
30
31
32class Shellcheck(_Tool):
33    def run(self):
34        cmd = ['shellcheck', self.filename]
35        try:
36            p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
37            stdout = p.communicate()[0]
38            processed_output = [str(line.decode().rstrip()) for line in stdout.splitlines() if line]
39            if p.returncode == 0:
40                return
41            return ["{}:0: run 'shellcheck' and fix the warnings".format(self.filename),
42                    '\n'.join(processed_output)]
43        except FileNotFoundError:
44            return ["{}:0: failed to call 'shellcheck'".format(self.filename)]
45