-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_pylint.py
More file actions
executable file
·99 lines (83 loc) · 2.46 KB
/
run_pylint.py
File metadata and controls
executable file
·99 lines (83 loc) · 2.46 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
89
90
91
92
93
94
95
96
97
98
99
"""PyLint code"""
# !/usr/bin/env python
import os
import sys
import subprocess
import re
PYLINT = '/usr/bin/pylint'
BASE_PATH = sys.argv[1]
OUTPUT_FILE = None
EXTRA_LIBS = []
DISABLED_SETTINGS = []
IGNORE_PATTERNS = []
ADDITIONAL_PARAMETERS = []
CODE_RATING = re.compile(r'''Your code has been rated at
([-0-9.]*)/10 \(previous run: ([-0-9.]*)/10\)''')
FILE_NAME = re.compile(r'[-a-zA-Z0-9_/]*\.py')
def setup_paths():
"""TODO"""
old_pythonpath = None
old_path = os.environ['PATH']
for path in EXTRA_LIBS:
os.environ["PATH"] += os.pathsep + path
if not os.environ.get("PYTHONPATH"):
os.environ["PYTHONPATH"] = ''
else:
old_pythonpath = os.environ["PYTHONPATH"]
for path in EXTRA_LIBS:
os.environ["PYTHONPATH"] += os.pathsep + path
return old_path, old_pythonpath
def reset_paths(old_path, old_pythonpath=None):
"""TODO"""
os.environ['PATH'] = old_path
if old_pythonpath:
os.environ['PYTHONPATH'] = old_pythonpath
else:
del os.environ['PYTHONPATH']
def construct_command():
"""TODO"""
command = [PYLINT, BASE_PATH, '-f', 'parseable']
if DISABLED_SETTINGS:
command.append('--disable=%s' % ','.join(DISABLED_SETTINGS))
if IGNORE_PATTERNS:
command.append('--ignore=%s' % ','.join(IGNORE_PATTERNS))
if ADDITIONAL_PARAMETERS:
command.extend(ADDITIONAL_PARAMETERS)
return command
def run_pylint():
"""TODO"""
os.chdir(BASE_PATH)
command = construct_command()
try:
output = subprocess.check_output(command, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as error:
output = error.output
match = CODE_RATING.search(output)
if not match or float(match.group(1)) < float(match.group(2)):
exitcode = 1
else:
exitcode = 0
if OUTPUT_FILE:
with open(OUTPUT_FILE, 'w') as file_pointer:
file_pointer.write(output)
return exitcode, output
def add_file_paths(inputs):
"""TODO"""
output = ''
for line in inputs.split('\n'):
if FILE_NAME.match(line):
output += '%s/%s' % (BASE_PATH, line)
else:
output += line
output += '\n'
return output
def main():
"""TODO"""
old_path, old_pythonpath = setup_paths()
exitcode, output = run_pylint()
output = add_file_paths(output)
reset_paths(old_path, old_pythonpath)
print output
sys.exit(exitcode)
if __name__ == '__main__':
main()