Skip to content

Commit

Permalink
338 pre commit hooks (#10)
Browse files Browse the repository at this point in the history
* Auto formatting items in test dir by commit hooks

* Auto formatting test data by commit hooks

* Auto formatting of pre-commit function by commit hooks

* Auto formatting construction and requirements using commit hooks

* Auto formatting and modifying L112 to fix E712

* Auto formatting workflow yaml by commit hooks

* Adding mixed line ending and detecting secrets

* Removing unused commit hook functions
  • Loading branch information
Jday7879 authored May 10, 2024
1 parent 0e30173 commit 3a8b237
Show file tree
Hide file tree
Showing 18 changed files with 288 additions and 307 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ jobs:
- name: Run pytest
run: |
pytest -v
pytest -v
32 changes: 20 additions & 12 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ repos:
language: script
stages: [commit]

#works
- repo: local
hooks:
- id: mixed-line-endings
entry: pre-commits/mixed_line_endings.py
name: Check for consistent end of line type LF to CRLF to CR (auto-fixes)
language: script
stages: [commit]

#works
#if using on different file types, it will need a seperate hook per file type
- repo: local
Expand Down Expand Up @@ -76,18 +85,17 @@ repos:
stages: [commit]


# #needs to remove the password in hello_world.py
# - repo: local
# hooks:
# - id: detect-secrets
# entry: detect-secrets
# name: detect-secrets - Detect secrets in staged code
# #args: [ "--baseline", ".secrets.baseline" ]
# args: [scan, audit]
# language: system
# types: [python]
# stages: [commit]
# exclude: .*/tests/.*|^\.cruft\.json$
# works in testing
- repo: local
hooks:
- id: detect-secrets
entry: detect-secrets-hook
name: detect-secrets - Detect secrets in staged code
args: [ "--baseline", ".secrets.baseline" ]
#args: [scan, audit]
language: system
types: [python]
stages: [commit]



Expand Down
27 changes: 13 additions & 14 deletions pre-commits/check_added_large_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,20 @@
import json
import math
import os
from typing import Optional
from typing import Sequence
from typing import Set
from typing import Optional, Sequence, Set

from pre_commit_hooks.util import added_files
from pre_commit_hooks.util import CalledProcessError
from pre_commit_hooks.util import cmd_output
from pre_commit_hooks.util import CalledProcessError, added_files, cmd_output


def _lfs_files() -> Set[str]:
"""Private function."""
try:
# Introduced in git-lfs 2.2.0, first working in 2.2.1
lfs_ret = cmd_output('git', 'lfs', 'status', '--json')
lfs_ret = cmd_output("git", "lfs", "status", "--json")
except CalledProcessError: # pragma: no cover (with git-lfs)
lfs_ret = '{"files":{}}'

return set(json.loads(lfs_ret)['files'])
return set(json.loads(lfs_ret)["files"])


def _find_large_added_files(filenames: Sequence[str], maxkb: int) -> int:
Expand All @@ -32,7 +28,7 @@ def _find_large_added_files(filenames: Sequence[str], maxkb: int) -> int:
for filename in (added_files() & set(filenames)) - _lfs_files():
kb = int(math.ceil(os.stat(filename).st_size / 1024))
if kb > maxkb:
print(f'{filename} ({kb} KB) exceeds {maxkb} KB.')
print(f"{filename} ({kb} KB) exceeds {maxkb} KB.")
retv = 1

return retv
Expand All @@ -42,17 +38,20 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
"""Entry function for script."""
parser = argparse.ArgumentParser()
parser.add_argument(
'filenames', nargs='*',
help='Filenames pre-commit believes are changed.',
"filenames",
nargs="*",
help="Filenames pre-commit believes are changed.",
)
parser.add_argument(
'--maxkb', type=int, default=500,
help='Maxmimum allowable KB for added files',
"--maxkb",
type=int,
default=500,
help="Maxmimum allowable KB for added files",
)

args = parser.parse_args(argv)
return _find_large_added_files(args.filenames, args.maxkb)


if __name__ == '__main__':
if __name__ == "__main__":
exit(main())
33 changes: 14 additions & 19 deletions pre-commits/check_merge_conflict.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,49 @@
"""Pre commit hook to check for merge conflict flags in file."""
import argparse
import os.path
from typing import Optional
from typing import Sequence

from typing import Optional, Sequence

CONFLICT_PATTERNS = [
b'<<<<<<< ',
b'======= ',
b'=======\n',
b'>>>>>>> ',
b"<<<<<<< ",
b"======= ",
b"=======\n",
b">>>>>>> ",
]


def _is_in_merge() -> int:
"""Private function."""
return (
os.path.exists(os.path.join('.git', 'MERGE_MSG')) and
(
os.path.exists(os.path.join('.git', 'MERGE_HEAD')) or
os.path.exists(os.path.join('.git', 'rebase-apply')) or
os.path.exists(os.path.join('.git', 'rebase-merge'))
)
return os.path.exists(os.path.join(".git", "MERGE_MSG")) and (
os.path.exists(os.path.join(".git", "MERGE_HEAD"))
or os.path.exists(os.path.join(".git", "rebase-apply"))
or os.path.exists(os.path.join(".git", "rebase-merge"))
)


def main(argv: Optional[Sequence[str]] = None) -> int:
"""Entry function for script."""
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*')
parser.add_argument('--assume-in-merge', action='store_true')
parser.add_argument("filenames", nargs="*")
parser.add_argument("--assume-in-merge", action="store_true")
args = parser.parse_args(argv)

if not _is_in_merge() and not args.assume_in_merge:
return 0

retcode = 0
for filename in args.filenames:
with open(filename, 'rb') as inputfile:
with open(filename, "rb") as inputfile:
for i, line in enumerate(inputfile):
for pattern in CONFLICT_PATTERNS:
if line.startswith(pattern):
print(
f'Merge conflict string "{pattern.decode()}" '
f'found in {filename}:{i + 1}',
f"found in {filename}:{i + 1}",
)
retcode = 1

return retcode


if __name__ == '__main__':
if __name__ == "__main__":
exit(main())
41 changes: 0 additions & 41 deletions pre-commits/commit_msg.py

This file was deleted.

20 changes: 9 additions & 11 deletions pre-commits/end_of_line_fixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
"""Pre commit hook to ensure single blank line at end of python file."""
import argparse
import os
from typing import IO
from typing import Optional
from typing import Sequence
from typing import IO, Optional, Sequence


def _fix_file(file_obj: IO[bytes]) -> int:
Expand All @@ -17,13 +15,13 @@ def _fix_file(file_obj: IO[bytes]) -> int:
return 0
last_character = file_obj.read(1)
# last_character will be '' for an empty file
if last_character not in {b'\n', b'\r'} and last_character != b'':
if last_character not in {b"\n", b"\r"} and last_character != b"":
# Needs this seek for windows, otherwise IOError
file_obj.seek(0, os.SEEK_END)
file_obj.write(b'\n')
file_obj.write(b"\n")
return 1

while last_character in {b'\n', b'\r'}:
while last_character in {b"\n", b"\r"}:
# 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
Expand All @@ -40,7 +38,7 @@ def _fix_file(file_obj: IO[bytes]) -> int:
# newlines. If we find extraneous newlines, then backtrack and trim them.
position = file_obj.tell()
remaining = file_obj.read()
for sequence in (b'\n', b'\r\n', b'\r'):
for sequence in (b"\n", b"\r\n", b"\r"):
if remaining == sequence:
return 0
elif remaining.startswith(sequence):
Expand All @@ -54,21 +52,21 @@ def _fix_file(file_obj: IO[bytes]) -> int:
def main(argv: Optional[Sequence[str]] = None) -> int:
"""Entry function for script."""
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
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:
with open(filename, "rb+") as file_obj:
ret_for_file = _fix_file(file_obj)
if ret_for_file:
print(f'Fixing {filename}')
print(f"Fixing {filename}")
retv |= ret_for_file

return retv


if __name__ == '__main__':
if __name__ == "__main__":
exit(main())
42 changes: 20 additions & 22 deletions pre-commits/mixed_line_endings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,28 @@
"""Pre commit hook to ensure all EOL characters are the same."""
import argparse
import collections
from typing import Dict
from typing import Optional
from typing import Sequence
from typing import Dict, Optional, Sequence


CRLF = b'\r\n'
LF = b'\n'
CR = b'\r'
CRLF = b"\r\n"
LF = b"\n"
CR = b"\r"
# Prefer LF to CRLF to CR, but detect CRLF before LF
ALL_ENDINGS = (CR, CRLF, LF)
FIX_TO_LINE_ENDING = {'cr': CR, 'crlf': CRLF, 'lf': LF}
FIX_TO_LINE_ENDING = {"cr": CR, "crlf": CRLF, "lf": LF}


def _fix(filename: str, contents: bytes, ending: bytes) -> None:
"""Private function."""
new_contents = b''.join(
line.rstrip(b'\r\n') + ending for line in contents.splitlines(True)
new_contents = b"".join(
line.rstrip(b"\r\n") + ending for line in contents.splitlines(True)
)
with open(filename, 'wb') as f:
with open(filename, "wb") as f:
f.write(new_contents)


def fix_filename(filename: str, fix: str) -> int:
"""Private function."""
with open(filename, 'rb') as f:
with open(filename, "rb") as f:
contents = f.read()

counts: Dict[bytes, int] = collections.defaultdict(int)
Expand All @@ -40,10 +37,10 @@ def fix_filename(filename: str, fix: str) -> int:
# Some amount of mixed line endings
mixed = sum(bool(x) for x in counts.values()) > 1

if fix == 'no' or (fix == 'auto' and not mixed):
if fix == "no" or (fix == "auto" and not mixed):
return mixed

if fix == 'auto':
if fix == "auto":
max_ending = LF
max_lines = 0
# ordering is important here such that lf > crlf > cr
Expand All @@ -70,24 +67,25 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
"""Entry function for script."""
parser = argparse.ArgumentParser()
parser.add_argument(
'-f', '--fix',
choices=('auto', 'no') + tuple(FIX_TO_LINE_ENDING),
default='auto',
"-f",
"--fix",
choices=("auto", "no") + tuple(FIX_TO_LINE_ENDING),
default="auto",
help='Replace line ending with the specified. Default is "auto"',
)
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
parser.add_argument("filenames", nargs="*", help="Filenames to fix")
args = parser.parse_args(argv)

retv = 0
for filename in args.filenames:
if fix_filename(filename, args.fix):
if args.fix == 'no':
print(f'{filename}: mixed line endings')
if args.fix == "no":
print(f"{filename}: mixed line endings")
else:
print(f'{filename}: fixed mixed line endings')
print(f"{filename}: fixed mixed line endings")
retv = 1
return retv


if __name__ == '__main__':
if __name__ == "__main__":
exit(main())
Loading

0 comments on commit 3a8b237

Please sign in to comment.