Skip to content

Commit

Permalink
Speed up boring cyborg consistency pre-commit check (apache#42589)
Browse files Browse the repository at this point in the history
This is typically the slowest pre-commit besides mypy, and it runs every time. Previously it loaded all filenames into memory and ran glob filter on that. It seems faster to apply glob against the file system directly. This makes pre-commit much faster. Previously took around 4 seconds, now about a half a second.
  • Loading branch information
dstandish authored and ellisms committed Nov 13, 2024
1 parent 68bb306 commit e606ae4
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions scripts/ci/pre_commit/boring_cyborg.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
# under the License.
from __future__ import annotations

import subprocess
import sys
from pathlib import Path

import yaml
from termcolor import colored
from wcmatch import glob

if __name__ not in ("__main__", "__mp_main__"):
raise SystemExit(
Expand All @@ -33,22 +31,23 @@

CONFIG_KEY = "labelPRBasedOnFilePath"

current_files = subprocess.check_output(["git", "ls-files"]).decode().splitlines()
git_root = Path(subprocess.check_output(["git", "rev-parse", "--show-toplevel"]).decode().strip())
cyborg_config_path = git_root / ".github" / "boring-cyborg.yml"
repo_root = Path(__file__).parent.parent.parent.parent
cyborg_config_path = repo_root / ".github" / "boring-cyborg.yml"
cyborg_config = yaml.safe_load(cyborg_config_path.read_text())
if CONFIG_KEY not in cyborg_config:
raise SystemExit(f"Missing section {CONFIG_KEY}")

errors = []
for label, patterns in cyborg_config[CONFIG_KEY].items():
for pattern in patterns:
if glob.globfilter(current_files, pattern, flags=glob.G | glob.E):
try:
next(Path(repo_root).glob(pattern))
continue
yaml_path = f"{CONFIG_KEY}.{label}"
errors.append(
f"Unused pattern [{colored(pattern, 'cyan')}] in [{colored(yaml_path, 'cyan')}] section."
)
except StopIteration:
yaml_path = f"{CONFIG_KEY}.{label}"
errors.append(
f"Unused pattern [{colored(pattern, 'cyan')}] in [{colored(yaml_path, 'cyan')}] section."
)

if errors:
print(f"Found {colored(str(len(errors)), 'red')} problems:")
Expand Down

0 comments on commit e606ae4

Please sign in to comment.