-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstall.py
254 lines (205 loc) · 10.3 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import os
import sys
import ctypes
import subprocess
import shutil
import json
import winreg
from version import __version__
from src.UserPreferences import UserPreferences
APPLICATION_NAME = "Spotify Linker"
PUBLISHER = "Firewe"
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
def request_admin():
if not is_admin():
print("You need to run this script as an administrator.")
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)
sys.exit(1)
def abort_installation():
input("Press Enter to exit...")
sys.exit(1)
def ask_installation_path():
installation_path = os.path.join(os.environ['ProgramFiles'], "SpotifyLinker")
print(f"Default installation path : {installation_path}")
response = input("Do you want to change the installation path? (y/N) ").strip().lower()
if response == "y":
while True: # Équivalent de do-while
installation_path = input("Enter the installation path (absolute path required): ").strip()
if not os.path.isabs(installation_path):
print("Please enter an absolute path.")
continue
if os.path.exists(installation_path) and os.path.isdir(installation_path):
if os.listdir(installation_path):
create_new_dir = input("The directory is not empty. Do you want to create a 'SpotifyLinker' subdirectory? (Y/n) ").strip().lower()
if create_new_dir == "n":
print("Please choose an empty directory.")
continue
else:
installation_path = os.path.join(installation_path, "SpotifyLinker")
os.makedirs(installation_path, exist_ok=True)
print(f"Created new directory: {installation_path}")
break
print(f"Using existing directory: {installation_path}")
break
else:
try:
os.makedirs(installation_path)
print(f"Created new directory: {installation_path}")
break
except OSError as e:
print(f"Error creating directory: {e}")
print("Please enter a valid path.")
else:
if os.path.exists(installation_path) and os.listdir(installation_path):
clean_dir = input("The directory is not empty. Do you want to clean it? (y/N) ").strip().lower()
if clean_dir == "y":
try:
shutil.rmtree(installation_path)
os.makedirs(installation_path, exist_ok=True)
print(f"Cleaned and created new directory: {installation_path}")
except OSError as e:
print(f"Error cleaning directory: {e}")
abort_installation()
else:
print("Installation aborted.")
abort_installation()
else:
try:
os.makedirs(installation_path, exist_ok=True)
except OSError as e:
print(f"Error creating directory: {e}")
abort_installation()
print(f"Using default directory: {installation_path}")
return installation_path
def copy_files(installation_path):
src_files = ['main.py', 'version.py', 'launcher.exe', 'requirements.txt', 'uninstall.exe']
src_dirs = ['src', 'assets', 'fonts']
try:
for file in src_files:
shutil.copy(file, installation_path)
print(f"Copied {file} to {installation_path}")
for directory in src_dirs:
dest_dir = os.path.join(installation_path, directory)
shutil.copytree(directory, dest_dir, ignore=shutil.ignore_patterns('__pycache__'))
print(f"Copied {directory} to {dest_dir}")
except OSError as e:
print(f"Error copying files: {e}")
abort_installation()
print("Files copied successfully.")
def setup_venv(target_dir):
venv_dir = os.path.join(target_dir, 'venv')
if not os.path.exists(venv_dir):
print("Creating virtual environment...")
subprocess.run([sys.executable, '-m', 'venv', venv_dir], check=True)
print("Installing dependencies...")
subprocess.run([os.path.join(venv_dir, 'Scripts', 'pip'), 'install', '-r', 'requirements.txt'], check=True)
def create_user_preferences():
config_dir = os.path.join(os.getenv('APPDATA'), 'SpotifyLinker')
config_file = os.path.join(config_dir, 'config.json')
if not os.path.exists(config_dir):
os.makedirs(config_dir)
if os.path.exists(config_file):
return
# Create a config with default values
user_preferences = UserPreferences.DEFAULT
try:
configure_now = input("Do you want to configure the Spotify API now? (Y/n) ").strip().lower()
if configure_now == "n":
print("Warning: You will need to configure the Spotify API later")
else:
user_preferences["spotify_client_id"] = input("Enter your Spotify Client ID: ").strip()
user_preferences["spotify_client_secret"] = input("Enter your Spotify Client Secret: ").strip()
user_preferences["spotify_redirect_uri"] = input("Enter your Spotify Redirect URI: ").strip()
user_preferences["local_port"] = input("Enter the local port (default: 2408): ").strip() or 2408
with open(config_file, 'w') as f:
json.dump(user_preferences, f, indent=4)
except Exception as e:
print(f"Error creating user preferences: {e}")
abort_installation()
def create_shortcut(installation_path):
script_path = os.path.join(installation_path, "launcher.exe")
shortcut_path = os.path.join(os.environ['APPDATA'], "Microsoft", "Windows", "Start Menu", "Programs", "SpotifyLinker.lnk")
icon_path = os.path.join(installation_path, "assets", "icon.ico")
# Créer le raccourci principal
powershell_command = f'$WshShell = New-Object -comObject WScript.Shell; $Shortcut = $WshShell.CreateShortcut("{shortcut_path}"); $Shortcut.TargetPath = "{script_path}"; $Shortcut.IconLocation = "{icon_path}"; $Shortcut.Save()'
subprocess.run(["powershell", "-Command", powershell_command], check=True)
# Créer le raccourci de débogage
debug_shortcut_path = os.path.join(installation_path, "launcher_debug.lnk")
debug_powershell_command = f'$WshShell = New-Object -comObject WScript.Shell; $Shortcut = $WshShell.CreateShortcut("{debug_shortcut_path}"); $Shortcut.TargetPath = "{script_path}"; $Shortcut.Arguments = "--debug"; $Shortcut.IconLocation = "{icon_path}"; $Shortcut.Save()'
subprocess.run(["powershell", "-Command", debug_powershell_command], check=True)
print(f"Debug shortcut created at {debug_shortcut_path}")
return shortcut_path
def add_to_startup(shortcut_path):
startup_dir = os.path.join(os.getenv('APPDATA'), 'Microsoft\\Windows\\Start Menu\\Programs\\Startup')
startup_shortcut = os.path.join(startup_dir, "SpotifyLinker.lnk")
if os.path.exists(startup_shortcut):
os.unlink(startup_shortcut)
os.symlink(shortcut_path, startup_shortcut)
print(f"Added shortcut to startup: {startup_shortcut}")
def add_to_startup_registry(installation_path):
run_key = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
info_key = r"SOFTWARE\SpotifyLinker"
try:
# Ajouter au démarrage
reg_run = winreg.OpenKey(winreg.HKEY_CURRENT_USER, run_key, 0, winreg.KEY_SET_VALUE)
winreg.SetValueEx(reg_run, "SpotifyLinker", 0, winreg.REG_SZ,
os.path.join(installation_path, "launcher.exe"))
winreg.CloseKey(reg_run)
# Ajouter des informations supplémentaires
reg_info = winreg.CreateKey(winreg.HKEY_CURRENT_USER, info_key)
winreg.SetValueEx(reg_info, "Name", 0, winreg.REG_SZ, APPLICATION_NAME)
winreg.SetValueEx(reg_info, "Publisher", 0, winreg.REG_SZ, PUBLISHER)
winreg.SetValueEx(reg_info, "Version", 0, winreg.REG_SZ, __version__)
winreg.CloseKey(reg_info)
print("Added to startup registry with additional information.")
except Exception as e:
print(f"Error adding to startup registry: {e}")
abort_installation()
def add_to_registry(installation_path):
uninstall_exe = os.path.join(installation_path, "uninstall.exe")
icon_path = os.path.join(installation_path, "assets", "icon.ico")
key = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\SpotifyLinker"
try:
reg = winreg.CreateKey(winreg.HKEY_LOCAL_MACHINE, key)
winreg.SetValueEx(reg, "DisplayName", 0, winreg.REG_SZ, APPLICATION_NAME)
winreg.SetValueEx(reg, "UninstallString", 0, winreg.REG_SZ, uninstall_exe)
winreg.SetValueEx(reg, "InstallLocation", 0, winreg.REG_SZ, installation_path)
winreg.SetValueEx(reg, "Publisher", 0, winreg.REG_SZ, "Firewe")
winreg.SetValueEx(reg, "DisplayVersion", 0, winreg.REG_SZ, __version__)
winreg.SetValueEx(reg, "EstimatedSize", 0, winreg.REG_DWORD, 35000)
winreg.SetValueEx(reg, "DisplayIcon", 0, winreg.REG_SZ, icon_path)
winreg.CloseKey(reg)
print("Added to registry.")
except Exception as e:
print(f"Error adding to registry: {e}")
if __name__ == "__main__":
request_admin()
print("Welcome to SpotifyLinker installation !\n")
# Select installation path
installation_path = ask_installation_path()
# Copy source files to installation path
print("Copying files...")
copy_files(installation_path)
# Setup virtual environment and install dependencies
print("Setting up virtual environment...")
setup_venv(installation_path)
# Create user preferences
print("Creating user preferences...")
create_user_preferences()
# Create shortcut
shortcut = create_shortcut(installation_path)
print(f"Shortcut created at {shortcut}")
# Ask to add to startup
add_to_startup(shortcut)
add_startup = input("Do you want to add SpotifyLinker to startup? (Y/n) ").strip().lower()
if add_startup != "n":
add_to_startup_registry(installation_path)
# Add to registry
print("Adding to registry...")
add_to_registry(installation_path)
print("Installation completed !")
input("Press Enter to exit...")