-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
66 lines (48 loc) · 1.64 KB
/
setup.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
import os
import ast
import subprocess
def find_python_files(folder):
python_files = []
for root, _, files in os.walk(folder):
for file in files:
if file.endswith(".py"):
python_files.append(os.path.join(root, file))
return python_files
def extract_imports(file_path):
with open(file_path, "r") as file:
content = file.read()
tree = ast.parse(content)
imports = set()
for node in ast.walk(tree):
if isinstance(node, ast.Import):
for alias in node.names:
imports.add(alias.name)
elif isinstance(node, ast.ImportFrom):
for alias in node.names:
imports.add(f"{node.module}.{alias.name}")
return imports
def install_packages(imports):
for package in imports:
try:
subprocess.run(["pip", "install", package], check=True)
except subprocess.CalledProcessError as e:
print(f"Failed to install {package}: {e}")
def main():
print('Running Setup.py')
root_folder = os.getcwd()
all_imports = set()
python_files = find_python_files(root_folder)
for file_path in python_files:
imports = extract_imports(file_path)
all_imports.update(imports)
print("Found the following imports:")
for package in all_imports:
print(package)
install_packages(all_imports)
print('Cleaning...')
## Clean cache
for p in __import__('pathlib').Path('.').rglob('*.py[co]'): p.unlink()
for p in __import__('pathlib').Path('.').rglob('__pycache__'): p.rmdir()
print('Cleaning cache: Done.')
if __name__ == "__main__":
main()