mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-04 19:26:52 +00:00
Add new byte-order-marker checker/fixer
This commit is contained in:
parent
5bd9e74adf
commit
d18bd5b75f
5 changed files with 56 additions and 5 deletions
30
pre_commit_hooks/fix_byte_order_marker.py
Normal file
30
pre_commit_hooks/fix_byte_order_marker.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import argparse
|
||||
from typing import Optional
|
||||
from typing import Sequence
|
||||
|
||||
|
||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('filenames', nargs='*', help='Filenames to check')
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
retv = 0
|
||||
|
||||
for filename in args.filenames:
|
||||
with open(filename, 'rb') as f_b:
|
||||
bts = f_b.read(3)
|
||||
|
||||
if bts == b'\xef\xbb\xbf':
|
||||
with open(filename, newline='', encoding='utf-8-sig') as f:
|
||||
contents = f.read()
|
||||
with open(filename, 'w', newline='', encoding='utf-8') as f:
|
||||
f.write(contents)
|
||||
|
||||
print(f'{filename}: removed byte-order marker')
|
||||
retv = 1
|
||||
|
||||
return retv
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
exit(main())
|
||||
Loading…
Add table
Add a link
Reference in a new issue