-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.py
160 lines (135 loc) · 5.75 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python3
import os
import sys
import subprocess
import shutil
# Variables
HOME_PATH = os.path.expanduser("~")
INSTALL_DIR = os.path.join(HOME_PATH, ".local/bin")
SCRIPT_NAME = "main.py"
EXECUTABLE_NAME = "ywfm"
PYTHON_REQUIREMENTS = ["tqdm"]
DEPENDENCIES = {
"darwin": ["terminal-notifier"],
"linux": ["notify-send", "xdg-open"]
}
def check_command(command):
"""Check if a command is available on the system."""
return shutil.which(command) is not None
def prompt_user(message):
"""Prompt the user with a yes/no question."""
while True:
response = input(f"{message} [y/n]: ").strip().lower()
if response in ["y", "yes"]:
return True
elif response in ["n", "no"]:
return False
else:
print("Please enter 'y' or 'n'.")
def list_missing_dependencies():
"""List dependencies that are missing for the current platform."""
os_type = sys.platform
missing_dependencies = []
if os_type == "darwin":
for dep in DEPENDENCIES["darwin"]:
if not check_command(dep):
missing_dependencies.append(dep)
elif os_type.startswith("linux"):
for dep in DEPENDENCIES["linux"]:
if not check_command(dep):
missing_dependencies.append(dep)
else:
print(f"Unsupported OS: {sys.platform}")
sys.exit(1)
return missing_dependencies
def install_dependencies():
"""Install platform-specific dependencies with user confirmation."""
print("Checking dependencies...")
missing_deps = list_missing_dependencies()
if not missing_deps:
print("All required dependencies are already installed.")
return
print("The following dependencies are missing and will be installed:")
for dep in missing_deps:
print(f"- {dep}")
if not prompt_user("Would you like to proceed with the installation?"):
print("Installation aborted by the user.")
sys.exit(0)
try:
if sys.platform == "darwin": # macOS
if "terminal-notifier" in missing_deps:
if check_command("brew"):
print("Installing terminal-notifier via Homebrew...")
subprocess.run(["brew", "install", "terminal-notifier"], check=True)
else:
print("Error: Homebrew is not installed. Please install Homebrew first from https://brew.sh/")
sys.exit(1)
elif sys.platform.startswith("linux"): # Linux
if "notify-send" in missing_deps:
print("Installing notify-send...")
if check_command("sudo"):
subprocess.run(["sudo", "apt", "update"], check=True)
subprocess.run(["sudo", "apt", "install", "-y", "libnotify-bin"], check=True)
else:
print("Error: 'sudo' is required for apt installation. Please run this script as a privileged user.")
sys.exit(1)
if "xdg-open" in missing_deps:
print("Installing xdg-utils...")
if check_command("sudo"):
subprocess.run(["sudo", "apt", "install", "-y", "xdg-utils"], check=True)
else:
print("Error: 'sudo' is required for apt installation. Please run this script as a privileged user.")
sys.exit(1)
except subprocess.CalledProcessError as e:
print(f"Error occurred during dependency installation: {e}")
sys.exit(1)
def install_python_libraries():
"""Install Python libraries required by the script."""
print("Checking Python libraries...")
print("The following Python libraries are required:")
for lib in PYTHON_REQUIREMENTS:
print(f"- {lib}")
if not prompt_user("Would you like to install these libraries?"):
print("Python library installation aborted by the user.")
return
try:
subprocess.run([sys.executable, "-m", "pip", "install", "--user"] + PYTHON_REQUIREMENTS, check=True)
print("Python libraries installed successfully.")
except subprocess.CalledProcessError as e:
print(f"Error occurred while installiing Python libraries: {e}")
sys.exit(1)
def install_script():
"""Install the reminder script to a directory in PATH."""
script_path = os.path.join(os.getcwd(), SCRIPT_NAME)
if not os.path.exists(script_path):
print(f"Error: {SCRIPT_NAME} not found in the current directory.")
sys.exit(1)
destination = os.path.join(INSTALL_DIR, EXECUTABLE_NAME)
print(f"Installing {SCRIPT_NAME} to {destination}...")
# Ensure the install directory exists
os.makedirs(INSTALL_DIR, exist_ok=True)
# Make the script executable
os.chmod(script_path, 0o755)
# Copy the script to the target directory
try:
shutil.copy(script_path, destination)
print(f"{EXECUTABLE_NAME} installed successfully to {INSTALL_DIR}.")
print(f"Ensure the following directory is in your PATH environment variable:")
print(f"\t{INSTALL_DIR}")
print(f"if not, you can do by adding the following line of code into your .bashrc or .zshrc file:")
print(f"\texport PATH=\"$HOME/.local/bin:$PATH\"")
except PermissionError:
print("Permission denied. Please ensure you have write permissions to the installation directory.")
sys.exit(1)
def main():
print("'ywfm' Installation Script")
print("============================")
# Check and install platform-specific dependencies
install_dependencies()
# Check and install Python libraries
install_python_libraries()
# Install the script
install_script()
print("\nInstallation complete. You can now use the 'ywfm' command!")
if __name__ == "__main__":
main()