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 f3cee4e
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 4 deletions.
8 changes: 7 additions & 1 deletion .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 All @@ -17,7 +23,7 @@
language: python
types: [python]
- id: check-byte-order-marker
name: Check for byte-order marker
name: Check for byte-order marker (deprecated, use byte-order-marker)
description: Forbid files which have a UTF-8 byte-order marker
entry: check-byte-order-marker
language: python
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ Add this to your `.pre-commit-config.yaml`

### Hooks available

#### `byte-order-marker`
Fix or forbid files which have a UTF-8 byte-order marker
- `--fix={auto,no}`
- `auto` - Removes BOMs automatically. This is the default argument.
- `no` - Checks if there are BOMs without modifying any file.

#### `check-added-large-files`
Prevent giant files from being committed.
- Specify what is "too large" with `args: ['--maxkb=123']` (default=500kB).
Expand All @@ -42,9 +48,6 @@ Require literal syntax when initializing empty or zero Python builtin types.
- Ignore this requirement for specific builtin types with `--ignore=type1,type2,…`.
- Forbid `dict` keyword syntax with `--no-allow-dict-kwargs`.

#### `check-byte-order-marker`
Forbid files which have a UTF-8 byte-order marker

#### `check-case-conflict`
Check for files with names that would conflict on a case-insensitive filesystem like MacOS HFS+ or Windows FAT.

Expand Down Expand Up @@ -183,6 +186,7 @@ Trims trailing whitespace.
[mirrors-autopep8](https://github.com/pre-commit/mirrors-autopep8)
- `pyflakes`: instead use `flake8`
- `flake8`: instead use [upstream flake8](https://gitlab.com/pycqa/flake8)
- `check-byte-order-marker`: instead use byte-order-marker

### As a standalone package

Expand Down
45 changes: 45 additions & 0 deletions pre_commit_hooks/byte_order_marker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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:
f = open(filename, 'rb')
if f.read(3) == b'\xef\xbb\xbf':
f.close()
if args.fix == 'no':
print(f'{filename}: Found byte-order marker')
else:
with open(
filename, newline='',
encoding='utf-8-sig',
) as rewritefile:
contents = rewritefile.read()

with open(
filename, 'w', newline='',
encoding='utf-8',
) as rewritefile:
rewritefile.writelines(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 f3cee4e

Please sign in to comment.