-
Notifications
You must be signed in to change notification settings - Fork 0
/
python-detect-deps.py
executable file
·108 lines (91 loc) · 3.16 KB
/
python-detect-deps.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python3
import os
import sys
from typing import List, Any
def deduplicate(lst: List[Any]) -> List[Any]:
dedup_lst = []
for item in lst:
if item not in dedup_lst:
dedup_lst.append(item)
return dedup_lst
def get_internal_modules() -> List[str]:
items = list(sys.builtin_module_names)
items += os.listdir(
f"/usr/lib/python{sys.version_info.major}.{sys.version_info.minor}/"
)
dynlibs = os.listdir(
f"/usr/lib/python{sys.version_info.major}.{sys.version_info.minor}/lib-dynload"
)
for dynlib in dynlibs:
items.append(dynlib.split(".")[0])
modules = []
for item in items:
if item == "site-packages":
continue
name = item
if item.endswith(".py"):
name = item[:-3]
elif os.path.isdir(item):
name = item
modules.append(name)
return modules
def process_file(path: str, internal_modules: List[str]) -> List[str]:
text = ""
with open(path, "r") as f:
text = f.read()
modules = []
for raw_line in text.split("\n"):
if len(raw_line) == 0:
continue
line = raw_line.strip().split()
if len(line) == 0:
continue
if line[0] == "import":
for i, module in enumerate(line[1:]):
if module == "as":
assert i != 0 # import as
break
if module.startswith("#"):
break
modules.append(module)
elif line[0] == "from" and len(line) >= 3 and line[2] == "import":
modules.append(line[1])
external_modules = []
for module in modules:
# strip "," because import a, b, c is valid
# TODO: import a.b might be from a different projects than import a
if module == "":
continue
module_name = module.strip(",").split(".")[0]
if module_name == "":
continue
if module_name not in internal_modules:
external_modules.append(module_name)
return external_modules
def process_dir(path: str, internal_modules: List[str]) -> List[str]:
external_modules = []
for root, dirs, files in os.walk(path):
for name in files:
if not name.endswith(".py"):
continue
external_modules += process_file(
os.path.join(root, name), internal_modules=internal_modules
)
for name in dirs:
external_modules += process_dir(
os.path.join(root, name), internal_modules=internal_modules
)
return external_modules
def process(path: str, internal_modules: List[str]) -> List[str]:
if os.path.isdir(path):
return process_dir(path=path, internal_modules=internal_modules)
return process_file(path=path, internal_modules=internal_modules)
def cli() -> None:
internal_modules = get_internal_modules()
external_modules = []
for arg in sys.argv[1:]:
external_modules += process(path=arg, internal_modules=internal_modules)
for module in deduplicate(external_modules):
print(module)
if __name__ == "__main__":
cli()