forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrailing_newlines.py
More file actions
executable file
·37 lines (29 loc) · 882 Bytes
/
trailing_newlines.py
File metadata and controls
executable file
·37 lines (29 loc) · 882 Bytes
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
#!/usr/bin/env python3
import fileinput
import os
import sys
NEWLINE, = b'\n'
def correct_trailing_newlines(filename: str) -> bool:
with open(filename, 'rb') as f:
a = len(f.read(2))
if a == 0:
return True
elif a == 1:
# file is wrong whether or not the only byte is a newline
return False
else:
f.seek(-2, os.SEEK_END)
b, c = f.read(2)
# no ASCII byte is part of any non-ASCII character in UTF-8
return b != NEWLINE and c == NEWLINE
def main() -> int:
# mimic git grep exit code behavior
exit_code = 1
for line in fileinput.input():
stripped = line.rstrip()
if not correct_trailing_newlines(stripped):
exit_code = 0
print(stripped)
return exit_code
if __name__ == '__main__':
sys.exit(main())