-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.py
65 lines (55 loc) · 2.11 KB
/
install.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
import subprocess
import sys
import os
def generate_distribution_package():
try:
subprocess.check_call([sys.executable, "setup.py", "sdist"])
print("Package distribution created successfully!")
except subprocess.CalledProcessError as e:
print(f"Failed to create package distribution: {e}")
sys.exit(1)
def install_package(package_path):
try:
subprocess.check_call([sys.executable, "-m", "pip", "install", package_path])
print("Installation successful!")
except subprocess.CalledProcessError as e:
print(f"Installation failed: {e}")
sys.exit(1)
def add_to_windows_path(script_path):
try:
sys.path.append(script_path)
print("Path added to system PATH!")
except subprocess.CalledProcessError as e:
print(f"Failed to add path to system PATH: {e}")
sys.exit(1)
def add_to_linux_path(script_path):
try:
with open(os.path.expanduser("~/.bashrc"), "a") as bashrc:
bashrc.write(f'export PATH=$PATH:{script_path}\n')
print("Path added to system PATH!")
except Exception as e:
print(f"Failed to add path to system PATH: {e}")
sys.exit(1)
if __name__ == "__main__":
# Generate distribution package
generate_distribution_package()
# Find the generated package
dist_folder = os.path.join(os.getcwd(), "dist")
package_files = os.listdir(dist_folder)
package_path = None
for file in package_files:
if file.endswith(".tar.gz"):
package_path = os.path.join(dist_folder, file)
break
if not package_path:
print("Package not found!")
sys.exit(1)
# Install the package
install_package(package_path)
# Add the directory to PATH based on the system
if sys.platform.startswith('win'):
add_to_windows_path(os.path.join(os.path.expanduser("~"), "AppData", "Roaming", "Python", "Python311", "Scripts"))
elif sys.platform.startswith('linux'):
add_to_linux_path(os.path.join(os.path.expanduser("~"), ".local", "bin"))
else:
print("Unsupported platform. Couldn't add to PATH.")