-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_installer.py
More file actions
399 lines (322 loc) · 13.4 KB
/
Copy pathbuild_installer.py
File metadata and controls
399 lines (322 loc) · 13.4 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!/usr/bin/env python3
"""
Minimal NSIS builder for NCSI Resolver - Clean Installation
This creates a minimal installer with only essential files
"""
import os
import subprocess
import shutil
def get_version():
"""Get version from version.py"""
try:
with open('version.py', 'r') as f:
content = f.read()
for line in content.split('\n'):
if '__version__' in line and '=' in line:
return line.split('=')[1].strip().strip('"\'')
except:
return "1.0.0"
def create_minimal_nsis_script():
"""Create a minimal NSIS script with only essential files"""
version = get_version()
script_content = f'''!include "MUI2.nsh"
!include "StrFunc.nsh"
${{StrLoc}}
; Product info
!define PRODUCT_NAME "NCSI Resolver"
!define PRODUCT_VERSION "{version}"
; Installer settings
Name "${{PRODUCT_NAME}}"
OutFile "NCSI_Resolver_v{version}_setup.exe"
InstallDir "$PROGRAMFILES64\\${{PRODUCT_NAME}}"
RequestExecutionLevel admin
ShowInstDetails show
; Interface settings
!define MUI_ABORTWARNING
!define MUI_FINISHPAGE_RUN
!define MUI_FINISHPAGE_RUN_TEXT "Test installation"
!define MUI_FINISHPAGE_RUN_FUNCTION LaunchTest
; Pages
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH
; Language
!insertmacro MUI_LANGUAGE "English"
; Variables
Var PYTHON_EXE
; Main installation section
Section "Install"
SetOutPath "$INSTDIR"
; Copy core installer files
File "installer.py"
File "service_installer.py"
File "system_config.py"
File "firewall_helper.py"
File "version.py"
File "nssm.exe"
; Create NCSIresolver subdirectory and copy files there
SetOutPath "$INSTDIR\\NCSIresolver"
File "NCSIresolver\\ncsi_server.py"
File "NCSIresolver\\service_wrapper.py"
File "NCSIresolver\\config.json"
File "NCSIresolver\\config_manager.py"
File "NCSIresolver\\logger.py"
File "NCSIresolver\\directory_manager.py"
File "NCSIresolver\\redirect.html"
; Return to root directory
SetOutPath "$INSTDIR"
; Find Python
Call FindPython
; Run installer using the actual installer.py and save output to log
DetailPrint "Running NCSI Resolver installer..."
SetOutPath "$TEMP"
nsExec::ExecToLog 'cmd /c ""$PYTHON_EXE" "$INSTDIR\\installer.py" --install --quick --nobanner > "$TEMP\\ncsi_install.log" 2>&1"'
Pop $0
; Check result
IntCmp $0 0 success
MessageBox MB_OK "Installation failed with exit code $0.$\\r$\\n$\\r$\\nCheck log file at: $TEMP\\ncsi_install.log$\\r$\\n$\\r$\\nCommon issues:$\\r$\\n- Port 80 may be in use (try different port)$\\r$\\n- Windows Firewall blocking connection$\\r$\\n- Antivirus software interference$\\r$\\n- Another service using the port$\\r$\\n$\\r$\\nOr run installer.py manually for detailed output"
Goto done
success:
DetailPrint "Installation completed successfully"
Call CreateShortcuts
Call WriteRegistry
done:
SectionEnd
; Find Python function - Future-proof dynamic detection
Function FindPython
Push $0
Push $1
Push $2
DetailPrint "Searching for Python installation..."
; Strategy 1: Try py launcher first (most reliable for getting latest Python)
StrCpy $PYTHON_EXE "$WINDIR\\py.exe"
IfFileExists "$PYTHON_EXE" test_python
; Strategy 2: Try python.exe in system PATH
DetailPrint "Checking system PATH for python.exe..."
nsExec::ExecToStack 'where python.exe'
Pop $0 ; exit code
Pop $1 ; output
IntCmp $0 0 0 try_registry try_registry
; Extract first line from where output (in case multiple pythons)
StrCpy $2 $1 1024 ; Get first 1024 chars
; Find first newline and extract path before it
${{StrLoc}} $0 $2 "$\\r$\\n" ">"
IntCmp $0 0 path_found path_found
StrCpy $PYTHON_EXE $2 $0 ; Extract substring up to newline
Goto test_python
path_found:
StrCpy $PYTHON_EXE $2
Goto test_python
try_registry:
; Strategy 3: Enumerate registry for any Python 3.x version (future-proof)
DetailPrint "Searching Windows registry for Python installations..."
; Try Python 3.13 through 3.8 in descending order (prefer newer)
StrCpy $0 313
registry_loop:
IntCmp $0 37 registry_done ; Stop at 3.7 (we need 3.8+)
; Convert version number to registry format (e.g., 313 -> "3.13")
IntOp $1 $0 / 10
IntOp $2 $0 % 10
StrCpy $1 "$1.$2"
DetailPrint "Checking registry for Python $1..."
; Try HKLM first (system-wide install)
ReadRegStr $PYTHON_EXE HKLM "SOFTWARE\\Python\\PythonCore\\$1\\InstallPath" "ExecutablePath"
IfFileExists "$PYTHON_EXE" test_python
; Try HKCU (user install)
ReadRegStr $PYTHON_EXE HKCU "SOFTWARE\\Python\\PythonCore\\$1\\InstallPath" "ExecutablePath"
IfFileExists "$PYTHON_EXE" test_python
; Decrement and try next version
IntOp $0 $0 - 1
Goto registry_loop
registry_done:
; Strategy 4: Try common installation directories (newest first)
DetailPrint "Checking standard installation locations..."
; System-wide installations (Program Files)
StrCpy $0 313
dir_loop:
IntCmp $0 37 user_dir_check
IntOp $1 $0 / 10
IntOp $2 $0 % 10
StrCpy $1 "Python$1$2"
StrCpy $PYTHON_EXE "$PROGRAMFILES\\$1\\python.exe"
IfFileExists "$PYTHON_EXE" test_python
StrCpy $PYTHON_EXE "$PROGRAMFILES32\\$1\\python.exe"
IfFileExists "$PYTHON_EXE" test_python
IntOp $0 $0 - 1
Goto dir_loop
user_dir_check:
; User-specific installations
StrCpy $0 313
user_loop:
IntCmp $0 37 python_not_found
IntOp $1 $0 / 10
IntOp $2 $0 % 10
StrCpy $1 "Python$1$2"
StrCpy $PYTHON_EXE "$PROFILE\\AppData\\Local\\Programs\\Python\\$1\\python.exe"
IfFileExists "$PYTHON_EXE" test_python
; Microsoft Store Python location
StrCpy $PYTHON_EXE "$LOCALAPPDATA\\Microsoft\\WindowsApps\\python.exe"
IfFileExists "$PYTHON_EXE" test_python
IntOp $0 $0 - 1
Goto user_loop
test_python:
DetailPrint "Found potential Python at: $PYTHON_EXE"
DetailPrint "Validating Python installation..."
; Test if Python actually works and is version 3.8+
nsExec::ExecToStack '"$PYTHON_EXE" -c "import sys; exit(0 if sys.version_info >= (3, 8) else 1)"'
Pop $0 ; exit code
Pop $1 ; output (unused)
IntCmp $0 0 python_valid
DetailPrint "Python validation failed (exit code $0), continuing search..."
Goto try_registry
python_valid:
; Get and display Python version
nsExec::ExecToStack '"$PYTHON_EXE" --version'
Pop $0
Pop $1
DetailPrint "Python validation successful: $1"
DetailPrint "Using Python: $PYTHON_EXE"
Goto found
python_not_found:
MessageBox MB_ICONSTOP "Python 3.8 or higher not found!$\\r$\\n$\\r$\\nPlease install Python from:$\\r$\\nhttps://www.python.org/downloads/$\\r$\\n$\\r$\\nMake sure to check 'Add Python to PATH' during installation.$\\r$\\n$\\r$\\nAfter installing Python, run this installer again."
Abort "Python not found"
found:
Pop $2
Pop $1
Pop $0
FunctionEnd
; Create shortcuts
Function CreateShortcuts
CreateDirectory "$SMPROGRAMS\\NCSI Resolver"
; Diagnostics shortcut - run pre-installation checks
CreateShortCut "$SMPROGRAMS\\NCSI Resolver\\Run Diagnostics.lnk" "$PYTHON_EXE" '"$INSTDIR\\installer.py" --diagnose --nobanner'
; Status shortcut - check installation status
CreateShortCut "$SMPROGRAMS\\NCSI Resolver\\Check Status.lnk" "$PYTHON_EXE" '"$INSTDIR\\installer.py" --check --nobanner'
; Uninstaller shortcut - runs the actual installer.py with --uninstall
CreateShortCut "$SMPROGRAMS\\NCSI Resolver\\Uninstall NCSI Resolver.lnk" "$PYTHON_EXE" '"$INSTDIR\\installer.py" --uninstall --quick --nobanner'
; Help shortcut
FileOpen $0 "$SMPROGRAMS\\NCSI Resolver\\Get Help (GitHub).url" w
FileWrite $0 "[InternetShortcut]$\\r$\\n"
FileWrite $0 "URL=https://github.com/djdarcy/Windows-No-Internet-Secured-BUGFIX/discussions$\\r$\\n"
FileClose $0
FunctionEnd
; Write registry entries
Function WriteRegistry
WriteRegStr HKLM "Software\\NCSI Resolver" "InstallPath" "$INSTDIR"
WriteRegStr HKLM "Software\\NCSI Resolver" "Version" "{version}"
WriteRegStr HKLM "Software\\NCSI Resolver" "PythonPath" "$PYTHON_EXE"
WriteUninstaller "$INSTDIR\\uninstall.exe"
WriteRegStr HKLM "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\NCSI Resolver" "DisplayName" "NCSI Resolver"
WriteRegStr HKLM "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\NCSI Resolver" "UninstallString" "$INSTDIR\\uninstall.exe"
WriteRegStr HKLM "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\NCSI Resolver" "DisplayVersion" "{version}"
WriteRegStr HKLM "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\NCSI Resolver" "Publisher" "NCSI Resolver Team"
WriteRegStr HKLM "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\NCSI Resolver" "URLInfoAbout" "https://github.com/djdarcy/Windows-No-Internet-Secured-BUGFIX"
WriteRegDWORD HKLM "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\NCSI Resolver" "NoModify" 1
WriteRegDWORD HKLM "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\NCSI Resolver" "NoRepair" 1
FunctionEnd
; Test page launcher
Function LaunchTest
ExecShell "open" "http://localhost/redirect"
FunctionEnd
; Uninstaller
Section "Uninstall"
; Get Python path from registry
ReadRegStr $PYTHON_EXE HKLM "Software\\NCSI Resolver" "PythonPath"
; Run uninstall script if Python and installer are available
IfFileExists "$PYTHON_EXE" 0 no_python
IfFileExists "$INSTDIR\\installer.py" 0 no_installer
DetailPrint "Running NCSI Resolver uninstaller..."
ExecWait '"$PYTHON_EXE" "$INSTDIR\\installer.py" --uninstall --quick' $0
Goto cleanup
no_python:
DetailPrint "Python not found, performing manual cleanup..."
Goto cleanup
no_installer:
DetailPrint "Installer script not found, performing manual cleanup..."
Goto cleanup
cleanup:
; Remove all files (thorough cleanup)
Delete "$INSTDIR\\*.py"
Delete "$INSTDIR\\*.exe"
Delete "$INSTDIR\\*.json"
Delete "$INSTDIR\\*.html"
Delete "$INSTDIR\\*.txt"
Delete "$INSTDIR\\*.log"
Delete "$INSTDIR\\*.reg"
; Remove any remaining subdirectories
RMDir /r "$INSTDIR\\__pycache__"
RMDir /r "$INSTDIR\\Logs"
RMDir /r "$INSTDIR\\Backups"
RMDir /r "$INSTDIR\\build"
RMDir /r "$INSTDIR\\NCSIresolver"
RMDir /r "$INSTDIR\\scripts"
; Remove installation directory
RMDir "$INSTDIR"
; Remove Start Menu shortcuts
Delete "$SMPROGRAMS\\NCSI Resolver\\*.lnk"
Delete "$SMPROGRAMS\\NCSI Resolver\\*.url"
RMDir "$SMPROGRAMS\\NCSI Resolver"
; Remove registry entries
DeleteRegKey HKLM "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\NCSI Resolver"
DeleteRegKey HKLM "Software\\NCSI Resolver"
SectionEnd'''
with open('ncsi_minimal.nsi', 'w') as f:
f.write(script_content)
print(f"Created ncsi_minimal.nsi for version {version}")
def build_installer():
"""Build with NSIS"""
nsis_paths = [
r"C:\\Program Files (x86)\\NSIS\\makensis.exe",
r"C:\\Program Files\\NSIS\\makensis.exe",
"makensis.exe"
]
makensis = None
for path in nsis_paths:
if os.path.exists(path) or shutil.which(path):
makensis = path
break
if not makensis:
print("NSIS not found. Install from https://nsis.sourceforge.io/")
return False
print(f"Building with: {makensis}")
try:
result = subprocess.run([makensis, "ncsi_minimal.nsi"],
capture_output=True, text=True, check=True)
print("Build successful!")
return True
except subprocess.CalledProcessError as e:
print("Build failed:")
print(e.stdout)
print(e.stderr)
return False
def main():
print("NCSI Resolver Minimal NSIS Builder")
print("=" * 40)
# Check required files exist
required = [
'installer.py', 'service_installer.py', 'system_config.py',
'firewall_helper.py', 'version.py', 'nssm.exe',
'NCSIresolver/ncsi_server.py', 'NCSIresolver/service_wrapper.py',
'NCSIresolver/config.json', 'NCSIresolver/config_manager.py',
'NCSIresolver/logger.py', 'NCSIresolver/directory_manager.py',
'NCSIresolver/redirect.html'
]
missing = [f for f in required if not os.path.exists(f)]
if missing:
print(f"Missing files: {missing}")
return 1
create_minimal_nsis_script()
if build_installer():
version = get_version()
print(f"Success! Created: NCSI_Resolver_v{version}_setup.exe")
print("\\nInstallation will now contain only essential files:")
print(" - Service files (ncsi_server.py, service_wrapper.py, etc.)")
print(" - Configuration files (config.json)")
print(" - System utilities (installer.py, system_config.py)")
print(" - Service manager (nssm.exe)")
print("\\nNo duplicate directories or unnecessary files!")
return 0
return 1
if __name__ == "__main__":
exit(main())