Skip to content

Commit

Permalink
Add new byte-order-marker checker/fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
jgowdy committed Oct 5, 2020
1 parent 5bd9e74 commit 5a7ca4e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
language: python
always_run: true
pass_filenames: false
- id: byte-order-marker
name: Remove or disallow byte-order marker
description: Removes or disallows UTF-8 byte-order marker
entry: byte-order-marker
language: python
types: [text]
- id: check-added-large-files
name: Check for added large files
description: Prevent giant files from being committed
Expand Down
38 changes: 38 additions & 0 deletions pre_commit_hooks/byte_order_marker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os, sys, codecs
import argparse
from typing import Optional
from typing import Sequence


def main(argv: Optional[Sequence[str]] = None) -> int:
parser = argparse.ArgumentParser()
parser.add_argument(
'-f', '--fix',
choices=('auto', 'no'),
default='auto',
help='Remove the byte-order marker. Default is "auto"',
)
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:
if f.read(3) == b'\xef\xbb\xbf':
if args.fix == 'no':
print(f'{filename}: Found byte-order marker')
else:
with open(..., newline='', encoding='utf-8-sig') as f:
contents = f.read()

with open(..., 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())
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ python_requires = >=3.6.1

[options.entry_points]
console_scripts =
byte-order-marker = pre_commit_hooks.byte_order_marker:main
check-added-large-files = pre_commit_hooks.check_added_large_files:main
check-ast = pre_commit_hooks.check_ast:main
check-builtin-literals = pre_commit_hooks.check_builtin_literals:main
Expand Down
25 changes: 25 additions & 0 deletions tests/byte_order_marker_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from pre_commit_hooks import byte_order_marker


def test_failure(tmpdir):
f = tmpdir.join('f.txt')
f.write_text('ohai', encoding='utf-8-sig')
assert byte_order_marker.main((str(f),)) == 1


def test_success(tmpdir):
f = tmpdir.join('f.txt')
f.write_text('ohai', encoding='utf-8')
assert byte_order_marker.main((str(f),)) == 0


def test_failure_nofix(tmpdir):
f = tmpdir.join('f.txt')
f.write_text('ohai', encoding='utf-8-sig')
assert byte_order_marker.main((str(f), '--fix=no')) == 1


def test_success_nofix(tmpdir):
f = tmpdir.join('f.txt')
f.write_text('ohai', encoding='utf-8')
assert byte_order_marker.main((str(f), '--fix=no')) == 0

0 comments on commit 5a7ca4e

Please sign in to comment.