-
Notifications
You must be signed in to change notification settings - Fork 540
/
check_disallowed_imports.py
executable file
·55 lines (40 loc) · 1.59 KB
/
check_disallowed_imports.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python3
from __future__ import annotations
import collections
import fnmatch
import json
import pathlib
import subprocess
import sys
CURRENT_DIR = pathlib.Path(__file__).parent.absolute()
def generate_dependency_graph(*args):
command = ("pydeps", "--show-deps", *args)
print(f"Running: {' '.join(command)}") # noqa: T201
result = subprocess.check_output(command, text=True)
return json.loads(result)
def check_dependency_rules(dependency_graph, disallowed_imports):
prohibited_deps = collections.defaultdict(set)
for module, module_data in dependency_graph.items():
imports = module_data.get("imports", [])
for pattern, disallow_rules in disallowed_imports.items():
if fnmatch.fnmatch(module, pattern):
for disallow_rule in disallow_rules:
for imported in imports:
if fnmatch.fnmatch(imported, disallow_rule):
prohibited_deps[module].add(imported)
return prohibited_deps
disallowed_imports = {
"ibis.expr.*": ["numpy", "pandas"],
}
if __name__ == "__main__":
dependency_graph = generate_dependency_graph(*sys.argv[1:])
prohibited_deps = check_dependency_rules(dependency_graph, disallowed_imports)
print("\n") # noqa: T201
print("Prohibited dependencies:") # noqa: T201
print("------------------------") # noqa: T201
for module, deps in prohibited_deps.items():
print(f"\n{module}:") # noqa: T201
for dep in deps:
print(f" <= {dep}") # noqa: T201
if prohibited_deps:
sys.exit(1)