Debian Codegraph @

test/sanity/code-smell/trailing-newline.py

raw
1 """Require files to end in \n"""
2
3 from __future__ import annotations
4
5 import sys
6
7
8 def main():
9 """Main entry point."""
10 for path in sys.argv[1:] or sys.stdin.read().splitlines():
11 with open(path, 'rb') as path_fd:
12 try:
13 path_fd.seek(-1, 2) # End of the file minus one byte
14 except OSError as e: # catch empty files
15 continue
16 last_char = path_fd.read(1)
17 if last_char != b'\n':
18 print(f'{path}: text files should end with a newline character "\\n"')
19
20
21 if __name__ == '__main__':
22 main()