forked from numpy/numpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinter.py
71 lines (59 loc) · 2.11 KB
/
linter.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
import os
import sys
import subprocess
from argparse import ArgumentParser
from git import Repo, exc
CONFIG = os.path.join(
os.path.abspath(os.path.dirname(__file__)),
'lint_diff.ini',
)
class DiffLinter:
def __init__(self, branch):
self.branch = branch
self.repo = Repo('.')
self.head = self.repo.head.commit
def get_branch_diff(self, uncommitted = False):
"""
Determine the first common ancestor commit.
Find diff between branch and FCA commit.
Note: if `uncommitted` is set, check only
uncommitted changes
"""
try:
commit = self.repo.merge_base(self.branch, self.head)[0]
except exc.GitCommandError:
print(f"Branch with name `{self.branch}` does not exist")
sys.exit(1)
if uncommitted:
diff = self.repo.git.diff(self.head, '--unified=0', '***.py')
else:
diff = self.repo.git.diff(commit, self.head,
'--unified=0', '***.py')
return diff
def run_pycodestyle(self, diff):
"""
Original Author: Josh Wilson (@person142)
Source:
https://github.com/scipy/scipy/blob/main/tools/lint_diff.py
Run pycodestyle on the given diff.
"""
res = subprocess.run(
['pycodestyle', '--diff', '--config', CONFIG],
input=diff,
stdout=subprocess.PIPE,
encoding='utf-8',
)
return res.returncode, res.stdout
def run_lint(self, uncommitted):
diff = self.get_branch_diff(uncommitted)
retcode, errors = self.run_pycodestyle(diff)
errors and print(errors)
sys.exit(retcode)
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument("--branch", type=str, default='main',
help="The branch to diff against")
parser.add_argument("--uncommitted", action='store_true',
help="Check only uncommitted changes")
args = parser.parse_args()
DiffLinter(args.branch).run_lint(args.uncommitted)