Skip to content

Commit 414e9e8

Browse files
committed
Add pre-commit license header check
Add a local pre-commit hook that validates every file covered by `NOTICE.yml` has the expected license header. The new `tools/check_license_headers.py` script reuses the existing NOTICE traversal logic, reports files with missing or outdated headers, and keeps the check read-only so header updates still go through the dedicated update script.
1 parent 23919ab commit 414e9e8

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

‎.pre-commit-config.yaml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ repos:
3535
- id: validate-pyproject
3636
stages: [pre-commit, manual]
3737

38+
- repo: local
39+
hooks:
40+
# Checks every file NOTICE.yml covers, not only the changed ones; add headers with tools/update_license_headers.py
41+
- id: license-headers
42+
name: license headers (NOTICE.yml)
43+
entry: python tools/check_license_headers.py
44+
language: python
45+
additional_dependencies: [pyyaml]
46+
pass_filenames: false
47+
always_run: true
48+
stages: [pre-commit, manual]
49+
3850
- repo: https://github.com/astral-sh/ruff-pre-commit
3951
rev: v0.15.6
4052
hooks:

‎tools/check_license_headers.py‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#
2+
# DeepLabCut Toolbox (deeplabcut.org)
3+
# © A. & M.W. Mathis Labs
4+
# https://github.com/DeepLabCut/DeepLabCut
5+
#
6+
# Please see AUTHORS for contributors.
7+
# https://github.com/DeepLabCut/DeepLabCut/blob/master/AUTHORS
8+
#
9+
# Licensed under GNU Lesser General Public License v3.0
10+
#
11+
12+
"""Check that every file NOTICE.yml covers starts with its header.
13+
14+
Reads only; use update_license_headers.py for writing.
15+
"""
16+
17+
import sys
18+
from itertools import takewhile
19+
from pathlib import Path
20+
21+
from update_license_headers import load_config, walk_directory
22+
23+
24+
def leading_comment(path: Path) -> list[str]:
25+
"""Return the file's first comment block, with comment markers and one following space removed."""
26+
lines = path.read_text(encoding="utf-8").splitlines()
27+
return [line.lstrip("#").removeprefix(" ").rstrip() for line in takewhile(lambda line: line.startswith("#"), lines)]
28+
29+
30+
def has_header(path: Path, header: list[str]) -> bool:
31+
"""Return whether the header lines appear, in order and contiguous, in the file's first comment block."""
32+
comment = leading_comment(path)
33+
return any(comment[start : start + len(header)] == header for start in range(len(comment) - len(header) + 1))
34+
35+
36+
def main(input_file: str = "NOTICE.yml") -> int:
37+
"""Print each file missing its header and return 1 if there is any, else 0."""
38+
missing = []
39+
for entry in load_config(input_file):
40+
header = [line.rstrip() for line in entry["header"].splitlines()]
41+
missing += [name for name in walk_directory(entry) if not has_header(Path(name), header)]
42+
43+
for name in sorted(missing):
44+
print(f"{name}: missing or outdated license header (see {input_file})")
45+
46+
return 1 if missing else 0
47+
48+
49+
if __name__ == "__main__":
50+
sys.exit(main())

0 commit comments

Comments
 (0)