-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcheck_style.py
More file actions
executable file
·88 lines (62 loc) · 2.12 KB
/
check_style.py
File metadata and controls
executable file
·88 lines (62 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import os
import subprocess
import sys
class ImproperStyleError(Exception):
pass
class PythonStyleChecker(object):
def __init__(self, linter_name):
self.passed = 0
self.failed = set()
self._linter_name = linter_name
@property
def linter_name(self):
return self._linter_name
def check_style(self, module):
raise NotImplementedError
class PylintRunner(PythonStyleChecker):
def __init__(self):
super(PylintRunner, self).__init__('pylint')
def check_style(self, module):
'''Runs pylint on a Python module.'''
try:
subprocess.check_output([self.linter_name, module])
self.passed += 1
except subprocess.CalledProcessError:
self.failed.add(os.path.relpath(module))
class Pep8Runner(PythonStyleChecker):
def __init__(self):
super(Pep8Runner, self).__init__('pep8')
def check_style(self, module):
'''Runs pep8 on a Python module.'''
try:
subprocess.check_output([self.linter_name, module])
self.passed += 1
except subprocess.CalledProcessError:
self.failed.add(os.path.relpath(module))
def _get_starting_directory(args):
try:
base_directory = args[1]
except IndexError:
base_directory = os.getcwd()
return base_directory
def _run_linter(base_directory, linter):
for root, _, files in os.walk(base_directory):
for name in files:
file_path = os.path.join(root, name)
if file_path.endswith('.py'):
linter.check_style(file_path)
return linter.failed
def run_linters(base_directory):
failed_modules = set()
for linter in [PylintRunner(), Pep8Runner()]:
failed = _run_linter(base_directory, linter)
failed_modules.update(failed)
return failed_modules
def main():
base_directory = _get_starting_directory(sys.argv)
failed_modules = run_linters(base_directory)
if failed_modules:
raise ImproperStyleError(
'These modules failed style checking: {}'.format(failed_modules))
if __name__ == '__main__':
main()