pre-commit-hooks/pre_commit_hooks/check_permitted_path_characters.py
Noah Pendleton 9bb6ebc34d Add check for permitted characters in pathnames
Add a new checker that checks for allowed characters in pathnames.

Includes arguments for overriding the default allowed list and extending
it.

Default allow list is alphanumeric plus `.-_`.
2022-03-01 18:13:56 -05:00

49 lines
1.5 KiB
Python

from __future__ import annotations
import argparse
import string
from typing import Sequence
DEFAULT_ALLOWLIST = string.ascii_letters + string.digits + '-_./'
def main(argv: Sequence[str] | None = None) -> int:
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*')
parser.add_argument(
'--allowlist',
default=DEFAULT_ALLOWLIST,
help=(
'Override the default allowlist of permitted characters. The'
' default is %(default)s'
),
)
parser.add_argument(
'--extra-allowlist',
default='',
help='Extend the default allowlist with these characters.',
)
args = parser.parse_args(argv)
allowlist = set(args.allowlist + args.extra_allowlist)
retcode = 0
for filename in args.filenames:
# check the entire path, not just the filename, to catch directories
# with invalid characters
file_chars = set(filename)
if not file_chars.issubset(allowlist):
# sorted and stringified for readability
pretty_allowlist = ''.join(sorted(allowlist))
pretty_banlist = repr(''.join(sorted(file_chars - allowlist)))
print(
f'"{filename}" contains characters not in the allowlist:'
f' "{pretty_banlist}". Allowlist is: "{pretty_allowlist}".',
)
retcode = 1
return retcode
if __name__ == '__main__':
raise SystemExit(main())