-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetreqd
More file actions
executable file
·39 lines (28 loc) · 832 Bytes
/
getreqd
File metadata and controls
executable file
·39 lines (28 loc) · 832 Bytes
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
#!/usr/bin/env python3
import ast
import sys
def is_standard_lib(module_name):
return module_name in getattr(sys, 'stdlib_module_names', [])
def get_module(name):
return name.split('.')[0]
def get_requirements(python_file):
with open(python_file, 'r') as file:
root = ast.parse(file.read(), filename='$python_file')
imports = {
get_module(alias.name)
for node in ast.walk(root)
if isinstance(node, ast.Import)
for alias in node.names
if not is_standard_lib(get_module(alias.name))
}
from_imports = {
get_module(node.module)
for node in ast.walk(root)
if isinstance(node, ast.ImportFrom)
if not is_standard_lib(get_module(node.module))
}
return imports.union(from_imports)
if __name__ == '__main__':
requirements = get_requirements(sys.argv[1])
for r in requirements:
print(r)