This repository has been archived by the owner on Jul 6, 2020. It is now read-only.
/
pycodestyle.py
92 lines (74 loc) · 2.91 KB
/
pycodestyle.py
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
###########################################################################
# Check if any of the Python files touched by the commit have
# code style problems.
###########################################################################
import os.path
import subprocess
from beefore import diff
LABEL = 'PyCodeStyle'
class Lint:
def __init__(self, filename, line, col, code, description):
self.filename = filename
self.line = line
self.col = col
self.code = code
self.description = description
def __str__(self):
return 'Line %s, col %s: [%s] %s' % (self.line, self.col, self.code, self.description)
def add_comment(self, pull_request, commit, position):
pull_request.create_review_comment(
# body="At column %(col)d: [(%(code)s) %(description)s](http://.../%(code)s)" % {
body="At column %(col)d: (%(code)s) %(description)s" % {
'col': self.col,
'code': self.code,
'description': self.description
},
commit_id=commit.sha,
path=self.filename,
position=position,
)
@staticmethod
def find(directory):
directory = os.path.abspath(directory)
proc = subprocess.Popen(
['flake8'],
cwd=directory,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE
)
out, err = proc.communicate()
out_lines = [line for line in out.decode('utf-8').strip().split('\n') if line]
problems = {}
for problem in out_lines:
fname, line, col, remainder = problem.split(':', 3)
filename = os.path.abspath(fname)[len(directory)+1:]
code, description = remainder.strip().split(' ', 1)
problems.setdefault(filename, []).append(Lint(
filename=filename,
line=int(line),
col=int(col),
code=code,
description=description,
))
return problems
def prepare(directory):
pass
def check(directory, diff_content, commit):
results = []
lint_results = Lint.find(directory=directory)
diff_mappings = diff.positions(directory, diff_content)
for filename, problems in lint_results.items():
print(" * %s" % filename)
if filename in diff_mappings:
for problem in sorted(problems, key=lambda p: p.line):
try:
position = diff_mappings[filename][problem.line]
print(' - %s' % problem)
results.append((problem, position))
except KeyError:
# Line doesn't exist in the diff; so we can ignore this problem
print(' - Line %s not in diff' % problem.line)
else:
# File has been changed, but wasn't in the diff
print(' - file not in diff')
return results