2022-01-16 08:24:05 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2014-03-23 12:40:57 +08:00
|
|
|
import argparse
|
|
|
|
import os
|
2024-10-12 07:30:07 +08:00
|
|
|
from collections.abc import Sequence
|
2019-02-01 11:19:10 +08:00
|
|
|
from typing import IO
|
2014-03-23 12:40:57 +08:00
|
|
|
|
|
|
|
|
2020-02-06 03:10:42 +08:00
|
|
|
def fix_file(file_obj: IO[bytes]) -> int:
|
2014-03-23 12:40:57 +08:00
|
|
|
# Test for newline at end of file
|
|
|
|
# Empty files will throw IOError here
|
|
|
|
try:
|
|
|
|
file_obj.seek(-1, os.SEEK_END)
|
2020-02-06 03:10:42 +08:00
|
|
|
except OSError:
|
2014-03-23 12:40:57 +08:00
|
|
|
return 0
|
|
|
|
last_character = file_obj.read(1)
|
|
|
|
# last_character will be '' for an empty file
|
2018-10-14 07:44:02 +08:00
|
|
|
if last_character not in {b'\n', b'\r'} and last_character != b'':
|
2015-01-20 08:43:10 +08:00
|
|
|
# Needs this seek for windows, otherwise IOError
|
|
|
|
file_obj.seek(0, os.SEEK_END)
|
2014-04-14 13:09:14 +08:00
|
|
|
file_obj.write(b'\n')
|
2014-03-23 12:40:57 +08:00
|
|
|
return 1
|
|
|
|
|
2018-10-14 07:44:02 +08:00
|
|
|
while last_character in {b'\n', b'\r'}:
|
2014-03-23 12:40:57 +08:00
|
|
|
# Deal with the beginning of the file
|
|
|
|
if file_obj.tell() == 1:
|
|
|
|
# If we've reached the beginning of the file and it is all
|
|
|
|
# linebreaks then we can make this file empty
|
|
|
|
file_obj.seek(0)
|
|
|
|
file_obj.truncate()
|
|
|
|
return 1
|
|
|
|
|
|
|
|
# Go back two bytes and read a character
|
|
|
|
file_obj.seek(-2, os.SEEK_CUR)
|
|
|
|
last_character = file_obj.read(1)
|
|
|
|
|
|
|
|
# Our current position is at the end of the file just before any amount of
|
2018-10-14 07:00:22 +08:00
|
|
|
# newlines. If we find extraneous newlines, then backtrack and trim them.
|
|
|
|
position = file_obj.tell()
|
|
|
|
remaining = file_obj.read()
|
2018-10-14 07:44:02 +08:00
|
|
|
for sequence in (b'\n', b'\r\n', b'\r'):
|
|
|
|
if remaining == sequence:
|
|
|
|
return 0
|
|
|
|
elif remaining.startswith(sequence):
|
2018-10-14 07:00:22 +08:00
|
|
|
file_obj.seek(position + len(sequence))
|
|
|
|
file_obj.truncate()
|
|
|
|
return 1
|
2014-03-23 12:40:57 +08:00
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2022-01-16 08:24:05 +08:00
|
|
|
def main(argv: Sequence[str] | None = None) -> int:
|
2014-03-23 12:40:57 +08:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
|
|
|
|
args = parser.parse_args(argv)
|
|
|
|
|
|
|
|
retv = 0
|
|
|
|
|
|
|
|
for filename in args.filenames:
|
|
|
|
# Read as binary so we can read byte-by-byte
|
|
|
|
with open(filename, 'rb+') as file_obj:
|
|
|
|
ret_for_file = fix_file(file_obj)
|
|
|
|
if ret_for_file:
|
2020-02-06 03:10:42 +08:00
|
|
|
print(f'Fixing {filename}')
|
2014-03-23 12:40:57 +08:00
|
|
|
retv |= ret_for_file
|
|
|
|
|
|
|
|
return retv
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-10-24 01:23:50 +08:00
|
|
|
raise SystemExit(main())
|