forked from python-llfuse/python-llfuse
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckpatch.py
More file actions
Latest commit
executable file
·64 lines (52 loc) · 1.75 KB
/
Copy pathcheckpatch.py
File metadata and controls
executable file
·64 lines (52 loc) · 1.75 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
#!/usr/bin/env python3
importre
importos
importsubprocess
importsys
importshutil
fromargparseimportArgumentParser
trailing_w_re=re.compile(r'\s+\n$')
only_w_re=re.compile(r'^\s+\n$')
defcheckfile(name, correct=False):
found_problems=False
ifcorrect:
dst=open(name+'.checkpatch.tmp', 'w+')
withopen(name, 'r+') asfh:
for (lineno, line) inenumerate(fh):
ifonly_w_re.search(line):
print('%s:%d: line consists only of whitespace'% (name, lineno+1))
found_problems=True
eliftrailing_w_re.search(line):
print('%s:%d: trailing whitespace'% (name, lineno+1))
found_problems=True
ifcorrect:
dst.write(line.rstrip() +'\n')
ifcorrect:
fh.seek(0)
dst.seek(0)
shutil.copyfileobj(dst, fh)
fh.truncate()
os.unlink(dst.name)
found_problems=False
returnfound_problems
defparse_args():
parser=ArgumentParser(
description="Check tracked files for trailing whitespace")
parser.add_argument("--fix", action="store_true", default=False,
help="Automatically correct problems")
returnparser.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
forb_nameinhg_out.split(b'\0'):
ifnotb_name:
continue
name=b_name.decode('utf-8', errors='surrogatescape')
ifcheckfile(name, correct=options.fix):
found_problems=True
iffound_problems:
sys.exit(1)
else:
sys.exit(0)