alpha
Login
or
Join now
xcc.es
/
scripts
Star
0
Fork
0
Atom
Configure Feed
Issues
Pull Requests
Commits
Tags
Feed URL
Select the types of activity you want to include in your feed.
[READ-ONLY] Mirror of https://github.com/mrgnw/scripts.
Star
0
Fork
0
Atom
Configure Feed
Issues
Pull Requests
Commits
Tags
Feed URL
Select the types of activity you want to include in your feed.
Overview
Issues
Pulls
Pipelines
the python that pippy uses
author
Morgan
date
2 years ago
(Jan 5, 2024, 3:00 AM -0500)
commit
aa984af0
aa984af04e760bb81cee79ace749477d33bf9356
parent
a099edd6
a099edd62d8e25754447b1ce27df96e6bf94054c
+39
1 changed file
Expand all
Collapse all
Unified
Split
getreqd
+39
getreqd
View file
Reviewed
···
1
1
+
#!/usr/bin/env python3
2
2
+
import ast
3
3
+
import sys
4
4
+
5
5
+
6
6
+
def is_standard_lib(module_name):
7
7
+
return module_name in getattr(sys, 'stdlib_module_names', [])
8
8
+
9
9
+
10
10
+
def get_module(name):
11
11
+
return name.split('.')[0]
12
12
+
13
13
+
14
14
+
def get_requirements(python_file):
15
15
+
with open(python_file, 'r') as file:
16
16
+
root = ast.parse(file.read(), filename='$python_file')
17
17
+
18
18
+
imports = {
19
19
+
get_module(alias.name)
20
20
+
for node in ast.walk(root)
21
21
+
if isinstance(node, ast.Import)
22
22
+
for alias in node.names
23
23
+
if not is_standard_lib(get_module(alias.name))
24
24
+
}
25
25
+
26
26
+
from_imports = {
27
27
+
get_module(node.module)
28
28
+
for node in ast.walk(root)
29
29
+
if isinstance(node, ast.ImportFrom)
30
30
+
if not is_standard_lib(get_module(node.module))
31
31
+
}
32
32
+
33
33
+
return imports.union(from_imports)
34
34
+
35
35
+
36
36
+
if __name__ == '__main__':
37
37
+
requirements = get_requirements(sys.argv[1])
38
38
+
for r in requirements:
39
39
+
print(r)