Skip to content

Commit 3a8cf99

Browse files
chore: Change check-init-files hook from sh to python
1 parent 92abfc9 commit 3a8cf99

3 files changed

Lines changed: 63 additions & 16 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""Pre-commit hook for checking __all__ dunder in __init__.py files."""
2+
3+
from __future__ import annotations
4+
5+
import re
6+
from glob import glob
7+
8+
9+
def main():
10+
"""Check if the __all__ dunder exists and it's a list."""
11+
check_patterns = [
12+
"extensions/gubbins/gubbins-*/**/__init__.py",
13+
"diracx-*/**/__init__.py",
14+
]
15+
exclude_patterns = ["_generated"]
16+
17+
files = []
18+
19+
for pattern in check_patterns:
20+
files.extend(glob(pattern, recursive=True))
21+
22+
for pattern in exclude_patterns:
23+
for file in files[:]:
24+
if pattern in file:
25+
files.remove(file)
26+
27+
files_without_all = []
28+
files_incorrect_format_all = []
29+
30+
for file in files:
31+
with open(file, "r") as f:
32+
content = f.read()
33+
34+
# __all__ dunder exists
35+
if not re.search("__all__ =", content):
36+
files_without_all.append(file)
37+
38+
# If exists, make sure its a list
39+
elif not re.search("__all__ =[\S\s]*\[", content):
40+
files_incorrect_format_all.append(file)
41+
42+
ret_val = 0
43+
44+
if files_without_all:
45+
ret_val = 1
46+
print("> Files without __all__ defined")
47+
for filename in files_without_all:
48+
print(f"\t- {filename}")
49+
50+
if files_incorrect_format_all:
51+
ret_val = 1
52+
print("> Files with __all__ not defined as a list")
53+
for filename in files_incorrect_format_all:
54+
print(f"\t- {filename}")
55+
56+
return ret_val
57+
58+
59+
if __name__ == "__main__":
60+
raise SystemExit(main())

.github/workflows/check_init_files_precommit_hook.sh

Lines changed: 0 additions & 12 deletions
This file was deleted.

.pre-commit-config.yaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ repos:
104104
- repo: local
105105
hooks:
106106
- id: check-init-files
107-
name: Check for __all__ presence in __init__.py files
107+
name: Check __all__ dunder in __init__.py files
108108
language: system
109-
entry: "sh .github/workflows/check_init_files_precommit_hook.sh"
110-
files: ^(diracx-|extensions/gubbins/gubbins-).*__init__\.py$
111-
exclude: _generated
109+
entry: pixi run -e default python .github/workflows/check_init_files_precommit_hook.py
110+
pass_filenames: false

0 commit comments

Comments
 (0)