forked from PyCQA/pycodestyle
/
codeclimate-pep8
executable file
·87 lines (71 loc) · 2.51 KB
/
codeclimate-pep8
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
#!/usr/bin/env python3
import json
import os.path
from pycodestyle import BaseReport, StyleGuide
import sys
class CodeClimateReport(BaseReport):
"""Print results of the checks in Code Climate format."""
def error(self, line_number, offset, text, check):
"""Print an error in Code Climate format."""
code = super(CodeClimateReport, self).error(line_number, offset,
text, check)
if code:
issue = {
'type': 'issue',
'check_name': code,
'categories': ['Style'],
'description': text[5:].capitalize(),
'remediation_points': 50000,
'location': {
'path': os.path.normpath(self.filename),
'positions': {
'begin': {
'line': line_number,
'column': offset + 1
},
'end': {
'line': line_number,
'column': offset + 1
}
}
},
'content': {
'body': check.__doc__.strip()
}
}
print(json.dumps(issue)+'\0')
return code
def run(paths):
"""Parse options and run checks on Python source."""
import signal
# Handle "Broken pipe" gracefully
try:
signal.signal(signal.SIGPIPE, lambda signum, frame: sys.exit(1))
except AttributeError:
pass # not supported on Windows
style_guide = StyleGuide(
paths=paths,
reporter=CodeClimateReport)
options = style_guide.options
report = style_guide.check_files()
if report.total_errors:
if options.count:
sys.stderr.write(str(report.total_errors) + '\n')
sys.exit(1)
include_paths = ["."]
if os.path.exists("/config.json"):
contents = open("/config.json").read()
config = json.loads(contents)
i_paths = config.get("include_paths")
if type(i_paths) == list:
python_paths = []
for i in i_paths:
ext = os.path.splitext(i)[1]
if os.path.isdir(i) or ext == ".py":
python_paths.append(i)
include_paths = python_paths
if len(include_paths) > 0:
args = " ".join(include_paths)
sys.stderr.write(args)
run(include_paths)
#os.system("/usr/src/app/pep8.py --codeclimate {0}".format(args))