forked from python-llfuse/python-llfuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkpatch.py
executable file
·64 lines (52 loc) · 1.75 KB
/
checkpatch.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
#!/usr/bin/env python3
import re
import os
import subprocess
import sys
import shutil
from argparse import ArgumentParser
trailing_w_re = re.compile(r'\s+\n$')
only_w_re = re.compile(r'^\s+\n$')
def checkfile(name, correct=False):
found_problems = False
if correct:
dst = open(name + '.checkpatch.tmp', 'w+')
with open(name, 'r+') as fh:
for (lineno, line) in enumerate(fh):
if only_w_re.search(line):
print('%s:%d: line consists only of whitespace' % (name, lineno+1))
found_problems = True
elif trailing_w_re.search(line):
print('%s:%d: trailing whitespace' % (name, lineno+1))
found_problems = True
if correct:
dst.write(line.rstrip() + '\n')
if correct:
fh.seek(0)
dst.seek(0)
shutil.copyfileobj(dst, fh)
fh.truncate()
os.unlink(dst.name)
found_problems = False
return found_problems
def parse_args():
parser = ArgumentParser(
description="Check tracked files for trailing whitespace")
parser.add_argument("--fix", action="store_true", default=False,
help="Automatically correct problems")
return parser.parse_args()
options = parse_args()
os.chdir(os.path.dirname(__file__))
hg_out = subprocess.check_output(['hg', 'status', '--clean', '--modified', '--added',
'--no-status', '--print0'])
found_problems = False
for b_name in hg_out.split(b'\0'):
if not b_name:
continue
name = b_name.decode('utf-8', errors='surrogatescape')
if checkfile(name, correct=options.fix):
found_problems = True
if found_problems:
sys.exit(1)
else:
sys.exit(0)