Skip to content

Commit

Permalink
Update script in scripts dir
Browse files Browse the repository at this point in the history
  • Loading branch information
trallnag committed Jan 22, 2025
1 parent 355315c commit 39e70aa
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions scripts/filter_pre_commit_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from pathlib import Path
from typing import Literal

VERSION = "1.1.1"
VERSION = "1.1.2"

parser = ArgumentParser(
description=(
Expand Down Expand Up @@ -95,17 +95,17 @@
mode: Literal["id", "tag"] = args.mode
filters: list[str] = args.filters

with open(args.config) as pre_commit_config:
with Path.open(args.config) as pre_commit_config:
pre_commit_config_content: str = pre_commit_config.read()

# All hook identifiers that have been found in the pre-commit config file.
all_hooks: set[str] = set(
all_hooks: set[str] = {
match.group("hook")
for match in re.compile(
r"^ *(?:- )?id: [\'\"]?(?P<hook>[a-z0-9-]+)[\'\"]?(?: +#.*)?$",
re.MULTILINE,
).finditer(pre_commit_config_content)
)
}

# All hook identifiers that should not be part of the final result set.
excluded_hooks: set[str]
Expand All @@ -118,21 +118,19 @@
unknown_hooks = excluded_hooks - all_hooks

if len(unknown_hooks) > 0:
parser.error(f"Unknown hook(s): {sorted(list(unknown_hooks))}")
parser.error(f"Unknown hook(s): {sorted(unknown_hooks)}")
else:
# Validate that all filters are valid tags.
regex = re.compile(r"^[a-z0-9]+$")
for filter in args.filters:
if not regex.match(filter):
for filterv in args.filters:
if not regex.match(filterv):
parser.error(
(
f"Invalid filter value: '{filter}'. "
f"Must match regex: '{regex.pattern}'."
)
f"Invalid filter value: '{filterv}'. "
f"Must match regex: '{regex.pattern}'.",
)

# Excluded hooks are determined based on tags given as filters.
excluded_hooks = set(
excluded_hooks = {
match.group("hook")
for match in re.compile(
(
Expand All @@ -141,8 +139,8 @@
).replace("<tags>", "|".join(args.filters)),
re.MULTILINE,
).finditer(pre_commit_config_content)
)
}

filtered_hooks = all_hooks - excluded_hooks

print(",".join(sorted(list(filtered_hooks))))
print(",".join(sorted(filtered_hooks)))

0 comments on commit 39e70aa

Please sign in to comment.