2
1

tool.py 1.6 KB

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