-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAnalysisPlatform.bat
More file actions
1912 lines (1785 loc) · 72.5 KB
/
AnalysisPlatform.bat
File metadata and controls
1912 lines (1785 loc) · 72.5 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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
@echo off
chcp 65001 > nul
: _____________________________________________________________________________
:
: Analysis Platform StartUp
: _____________________________________________________________________________
: Important notice:
: Running the batch file is regarded as you agreed to the Terms of Use.
: Terms of Use:
: https://github.com/apdn7/AnalysisPlatform/about/terms_of_use_en.md
: _____________________________________________________________________________
: _____________________________________________________________________________
: Initialize
:: Set powershell common option
set "pshell=powershell -NoLogo -NoProfile -ExecutionPolicy Bypass"
:: Get Escape $e 0x1b 27 and Tab
for /f "delims=" %%i in ('%pshell% -Command [char]27') do set "esc=%%i"
for /f "delims=" %%i in ('%pshell% -Command [char]09') do set "tab=%%i"
:: Definition
call :defSetting
:: Log
if not defined startTime call :getDateTime startTime
call :clearLog "%log_startap%"
>"%log_stageStatus%" echo.
if exist "%log_stageTime%" del "%log_stageTime%"
:: Alias
doskey ap="AnalysisPlatform.bat" $*
set "arg1=%~1"
if /i "%arg1%"==":downloadOracleInstance" goto downloadOracleInstance
call :logStartap startTime "===== Startup ====="
set "app_title=Analysis Platform Port: %port% Lang: %lang% %subtitle% py: %ver_pyt% Path: %CD%"
title %app_title%
call :saveStartTimeOfStage MAIN
call :mode_cmd_init
cls
echo.
echo %clTitle%Start Analysis Platform%cl_Base%
echo.
: Read AP+DN7 User Settings: Version and Product (DN, OSS)
for /f "tokens=1,* delims=:" %%a in ('findstr /n /r "^" "%file_vers%"') do (
if "%%a"=="1" set "ver_str=%%b"
if "%%a"=="2" set "app_yml=%%b"
if "%%a"=="3" set "app_prd=%%b"
)
for /f "tokens=1-3 delims=.v" %%a in ("%ver_str%") do set "app_ver=%%a%%b%%c"
set /a app_ver=%app_ver% >nul 2>&1 || set /a app_ver=9999
echo Version: %esc%[36m%app_ver%%esc%[0m Product: %esc%[36m%app_prd%%esc%[0m
call :logStartap lapTime "VERSION ver:%app_ver% [%ver_str%] yml:%app_yml% Type:%app_prd%"
: Get PID
for /f "tokens=2 delims=," %%a in ('
tasklist /v /fo csv /fi "imagename eq cmd.exe" ^| findstr /c:"Port: %port%"
') do ( set "cmd_pid=%%~a" )
call :logStartap startTime "PID=%cmd_pid%: %app_title%"
rem prompt AP:%port%$g
echo.
: Check Product Type
rem Since there is a delay when updating the text color, directly enter the color code at the top after the change.
echo %esc%[30;107m%cl_Base%Check Product
if exist "%file_prod_dn%" (
set "prod=%product_dn%"
echo Detected Product Type: dn7
set "requirements=%file_prod_dn%"
) else (
set "prod=%product_oss%"
echo Detected Product Type: oss
set "requirements=%file_prod_oss%"
)
call :logStartap startTime "Product Type: %prod%"
if "prod"=="app_prd" (
call :logStartap startTime "Product Type: Match %prod%" app:
)
echo.
: Check Port
call :checkPort %port% || goto FINISH
: Run start program
: Close old start_ap.exe before run
call :stopLoadingApp
start "" "%start_file%" %port%
rem start with Chrome (App window)
set "url_ap=http://localhost:%port%"
if not "%launch_chrome%"=="0" (
where chrome.exe >nul 2>&1 && start "" chrome.exe --app="%url_ap%" || start "" "%url_ap%"
)
rem start with Edge (App window)
if not "%launch_edge%"=="0" (
where msedge.exe >nul 2>&1 && start "" msedge.exe --app="%url_ap%" || start "" "%url_ap%"
)
echo.
call :logStartap lapTime "Call start_ap.exe to open welcome page"
: _____________________________________________________________________________
: Check version for reinstallation
:CHECK_STATUS
echo %clTitle%Status Check Sequence%cl_Base%
call :readIniSettings "%stat_ini%"
%pshell% -Command if($env:install_vers -match '^\d{3,4}$') {} else {exit 1} || (
set /a install_vers=0
call :logStartap lapTime "Missing Vaild Version Info: use ver=0"
)
if %app_ver% neq %install_vers% (
set /a status=%status_install%
set /a wait_netcheck=%wait_netcheck_inst%
call :logStartap lapTime "[mode] Status File is INSTALL"
) else (
set /a status=%status_run_app%
set /a wait_netcheck=%wait_netcheck_appl%
call :logStartap lapTime "[mode] Status File is RUN APP"
)
echo.
: _____________________________________________________________________________
: Check Component Status
echo Check Component Existence
set /a noComp=0
if exist "%main_python%" ( echo Detect python ) else set /a noComp^|=1
if exist "%main_pip%" ( echo Detect pip ) else set /a noComp^|=2
if exist "%main_env%" ( echo Detect venv ) else set /a noComp^|=4
if exist "%main_oracle%" ( echo Detect oracle ) else set /a noComp^|=8
call :checkPythonLib_rough || set /a noComp^|=16
if %noComp% equ 0 call :checkPythonLib_full || set /a noComp^|=32
if %noComp% equ 0 (
call :logStartap lapTime "[mode] All component detected Code:%noComp%"
) else (
if %noComp% equ 31 (
call :logStartap lapTime "[mode] First Installation Code:%noComp%"
) else (
echo.
echo %clWarng% Installation is corrupted. Forcing component reinstallation... %cl_Base%
call :logStartap lapTime "[mode] Retry (Component loss) Code:%noComp%"
timeout /t 3
)
set /a status=%status_install%
)
: Mode Setting
if %status% equ %status_run_app% (
rem cls
call :logStartap lapTime "[mode] Start AP+DN7"
) else (
call :mode_cmd_install
echo %clTitle%Installation%cl_Base%
echo %clNotic%[Need Network or Proxy Settings and Connection]%esc%[30;107m %cl_Base%
echo %esc%[30;107m %cl_Base%
call :logStartap lapTime "[mode] Install (Path or Ver changed)"
)
echo.
: _____________________________________________________________________________
: Remove Oracle, python_embedded if size not enough
call :checkZip "%path_oc_zip%" %limitkB_oracle% "Oracle"
call :checkZip "%path_pi_pyz%" %limitkB_pippyz% "pippyz"
call :checkZip "%path_py_zip%" %limitkB_python% "python"
: _____________________________________________________________________________
: Check Disk Space before Installation and Start AP
call :checkDiskSpace || goto FINISH
echo.
: Direct Startup Mode
if "%startup_mode%"=="8" (
echo %clTitle%Direct Startup Mode%cl_Base% === Force to bypass Installation ===
set "network_nck=True"
rem call :saveStartUpSetting
goto Start_AP
)
echo Check Network and Proxy
: _____________________________________________________________________________
: Check Network and Proxy
call :getDefaultProxy
set /a active_com=0
call :checkProxy %prxy% "http"
call :checkProxy %prxs% "https"
if %active_com% equ 0 (
if %status% equ %status_install% (
echo %clAlarm% Alarm: Unable to communicate network %cl_Base%
echo Check the network connection
echo In an environment with proxy, write proxy settings to startup.ini
echo Delete proxy settings in startup.ini when no proxy is used.
call :logStartap lapTime "Network offline Proxy=%Connectivity_proxy% Direct=%Connectivity_direct%"
call :logStageStatus 102 "Unable to use network"
) else (
call :logStageStatus 102 "Unable to communicate network" "NoCount"
)
) else (
call :logStartap lapTime "Network is connected"
)
echo.
:: Check Proxy Status
set "proxy=Not Active"
if defined target_prxy (
set "HTTP_PROXY=%target_prxy%"
) else (
echo HTTP_PROXY : No Proxy
)
if defined target_prxs (
set "HTTPS_PROXY=%target_prxs%"
set "proxy=Use Proxy"
) else (
echo HTTPS_PROXY : No Proxy
set "proxy=No Proxy"
)
echo Proxy: %proxy%
echo HTTP_PROXY : %HTTP_PROXY%
echo HTTPS_PROXY : %HTTPS_PROXY%
call :logStartap lapTime "Set HTTP_PROXY%tab%[%HTTP_PROXY%]"
call :logStartap lapTime "Set HTTPS_PROXY%tab%[%HTTPS_PROXY%]"
: Check Network Connection
%pshell% -Command ^
$ErrorActionPreference='Stop'; $url=$env:url_testhp; ^
try { ^
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12; ^
$res = Invoke-WebRequest -Uri $url -Method Head -UseBasicParsing; ^
Write-Host ('Network is available.'); ^
Write-Host ('StatusCode: ' + $res.StatusCode) ^
} catch { exit 1 } && (
set "network_nck=False"
) || (
set "network_nck=True"
echo %clAlarm% Warning: Check Network Connection %cl_Base%
timeout %wait_netcheck%
)
call :logStartap lapTime "Check Network after set PROXY network_nck = %network_nck%"
echo.
: Branching point between installation and normal startup
if %status% equ %status_run_app% (
echo %clTitle%Start AP+DN7...%cl_Base%
goto Start_AP
)
: _____________________________________________________________________________
: Installing Analysis Platform
: _____________________________________________________________________________
:Install_AP
echo %clTitle%Start Installation...%cl_Base%
call :logStartap startTime "[Install] Install process initiated"
echo.
: _____________________________________________________________________________
: Download Components and Libraries
echo %clNotic% First boot may take 5-30 minutes depending on network ^& CPU speed. %cl_Base%
rem timeout /t 2
echo.
rem Move to Installer
@REM :: Ensure the user has full control over the folder.
@REM for %%a in ("..\..") do set "abs=%%~fa"
@REM call :logStartap lapTime "Change User Folder Permissions on %abs%"
@REM icacls "%abs%" /grant "BUILTIN\Users":(OI)(CI)F
@REM if errorlevel 1 (
@REM call :logStartap lapTime "Change User Folder Permissions: Denied"
@REM ) else (
@REM call :logStartap lapTime "Change User Folder Permissions: Done"
@REM )
@REM echo.
: Check R Existence
if not exist "%path_R%" (
if "%prod%"=="%product_dn%" (
echo %clAlarm% Make sure you have R-Portable folder before running this application. %cl_Base%
echo Or prepare R-Portable folder and reboot AP before using R function
rem call :logStageStatus 999 "R is not installed"
echo %esc%[32mYou can continue if you do not use R components.%cl_Base%
call :logStartap lapTime "Check R: R is missing"
echo.
choice /c YN /m "Press Y to continue or N to cancel:" /t 60 /d Y
if errorlevel 2 (
echo Process canceled.
call :logStartap lapTime "Check R: Canceled by user"
goto FINISH
)
call :logStartap lapTime "Check R: Forced execution by user"
)
call :logStartap lapTime "Check R: No R (OSS)"
) else (
call :logStartap lapTime "Check R: R is available"
)
echo %clTitle%Download Components...%cl_Base%
: Download Python
if exist "%main_python%" (
echo Detect python
call :logStartap startTime "Detect %main_python%"
) else (
call :downloadPythonEmbedded || goto FINISH
call :logStartap startTime "Fetched %path_py_zip%"
)
:: python isolation - Lib and DLLs
set "PYTHONHOME=%path_python%"
call :checkPythonIsolation "%main_python%"
: Download pip
if exist "%main_pip%" (
echo Detect pip
call :logStartap startTime "Detect %main_pip%"
)
call :preparepip && call :logStartap startTime "PreFetched pip" || goto FINISH
: Download Python libraries (Rapid Mode: pre-download without installation)
call :checkPythonLib_full && (
echo Detect python libraries
call :logStartap startTime "Detect python libraries"
) || (
call :logStartap startTime "Missing Python libraries"
if "%rapid_useCache%"=="True" call :getpylib ^
&& call :logStartap startTime "PreFetched pyLib - wheel"
)
: Download Oracle
if exist "%main_oracle%" (
echo Detect Oracle
call :logStartap startTime "Detect %main_oracle%"
) else (
if "%rapid_async_DL%"=="True" (
start "" cmd /c call "%~f0" :downloadOracleInstance
) else (
call :downloadOracleInstance ^
&& call :logStartap startTime "Fetched %path_oc_zip%" ^
|| goto FINISH
)
)
: _____________________________________________________________________________
: install packages
:: Get pip
echo %clTitle%Setup python... %clNotic% Components Install. %cl_Base%
call :logStartap lapTime "[Install] Setup python"
call :get_version_pip
if "%cur_pip%"=="Invalid" call :setpyIsoMode
if not exist "%main_pip%" (
call :logStartap lapTime "Check pip: Not found. Installing"
call :downloadPipByPipPyz
call :get_version_pip
)
if not "%cur_pip%"=="%ver_pip%" (
call :logStartap lapTime "Check pip: Ver mismatch. Updating"
call :downloadPipByPipPyz
call :get_version_pip
)
if not "%cur_pip%"=="%ver_pip%" (
echo %clAlarm% Failed to install pip %cl_Base%
echo - Check if the network is up and running.
echo - Verify that the proxy settings are correct if a proxy is in use.
echo - Ensure that access to Python libraries such as pypi.org is not blocked
echo Terminating startup process...
rem :::::: Make New Error Code and Page
call :logStageStatus 201 "Failed to install pip"
goto FINISH
) else (
call :logStartap lapTime "Check pip: pip is available"
)
: -----------------------------------------------------------------------------
: Install virtual environment
call :logStartap lapTime "Install virtualenv"
echo %clTitle%Creating Virtualenv...%cl_Base%
set "com_venv=""%main_python%"" -I -m pip install virtualenv --find-links ""%rapid_dirCache%"""
set "com_venv=%com_venv% --no-warn-script-location -q"
call :pyGetComp "%com_venv%" "Download venv" "venv"
ECHO Creating Virtual Environment at "%path_env%"
"%main_python%" -I -m virtualenv "%path_env%" --python "%main_python%" ^
--no-download --extra-search-dir "%rapid_dirCache%" && (
call :logStartap lapTime "Create virtualenv: Done"
) || (
call :logStartap lapTime "Create virtualenv: Failed"
echo %clAlarm% Failed to create Virtual Env %cl_Base%
goto FINISH
)
:: copy complied sqlite3.dll to env Scripts folder
if exist "%path_sqlite_dll%" copy /y "%path_sqlite_dll%" "%path_env%\Scripts\"
:: copy complied sqlite3.dll to python embedded Scripts folder
::powershell -ExecutionPolicy Bypass -Command if (Test-Path -Path \"$env:path_sqlite_dll\") { Copy-Item \"$env:path_sqlite_dll\" -Destination \"$env:path_python\" }
: -----------------------------------------------------------------------------
: Activate Virtual Environment 1st time before pip/venv
echo Activate Virtual Environment
CALL "%path_env%\Scripts\activate.bat"
if errorlevel 1 (
call :logStartap startTime "virtualenv activated: Failed"
) else (
call :logStartap startTime "virtualenv activated: %VIRTUAL_ENV%"
)
:: python isolation - Lib and DLLs for virtualenv in installation
set "PYTHONHOME=%path_python%"
call :checkPythonIsolation "%main_venvpy%"
:: Install pip to venv
call :logStartap lapTime "Install pip to virtualenv"
"%main_venvpy%" -I "%path_pi_pyz%" install pip==%ver_pip% --find-links "%rapid_dirCache%" ^
--exists-action=i --no-warn-script-location --retries 2
call :logStartap lapTime "Install venv pip by pyz"
set "cmd_dlp=""%main_venvpy%"" -I -m pip install %pkg_com% --no-index --retries 1"
set "cmd_dlp=%cmd_dlp% --find-links ""%rapid_dirCache%"" --only-binary=:all:"
call :pyGetComp "%cmd_dlp%" "Install venv pip " "pip.whl" && set /a err=0 || set /a err=1
call :get_version_pip_env
if %err% neq 0 (
set "cmd_dlp=""%main_venvpy%"" -I -m pip install %pkg_com% --no-index --retries 2"
set "cmd_dlp=%cmd_dlp% --find-links ""%rapid_dirCache%"" --no-build-isolation"
call :pyGetComp "%cmd_dlp%" "Install venv pip+" "pip.whl+" && (
set /a err=0
call :get_version_pip_env
) || set /a err=1
)
if %err% neq 0 (
set "cmd_dlp=""%main_venvpy%"" -I -m pip install %pkg_com% --retries 2 "
call :pyGetComp "%cmd_dlp%" "Install venv pip#" "pip.whl#" && call :get_version_pip_env
)
echo %clTitle%Installing python libraries...%cl_Base%
:: Install Library with pip
call :logStartap startTime "Install pylib to virtualenv"
rem Rapid pylib Install
set "cmd_libR=""%main_venvpy%"" -I -m pip install -r ""%requirements%"" --retries 3"
set "cmd_libR=%cmd_libR% --find-links ""%rapid_dirCache%"" --no-warn-script-location"
rem Normal pylib Install
set "cmd_libN=""%main_venvpy%"" -I -m pip install -r ""%requirements%"" --retries 3"
set "cmd_libN=%cmd_libN% --no-warn-script-location"
if "rapid_useCache"=="True" (
rem Rapid pylib Install
call :getpylib
call :pyGetComp "%cmd_libR%" "Install venv lib" "py.lib" && set /a err=0 || set /a err=1
) else (
rem Normal pylib Install
call :pyGetComp "%cmd_libN%" "Install venv lib" "py.lib" && set /a err=0 || set /a err=1
)
call :checkPythonLib_full && set /a err=0 || set /a err=1
if %err% neq 0 (
echo %clAlarm% Failed to install python libraries %cl_Base%
echo - Check if the network is up and running.
echo - Verify that the proxy settings are correct if a proxy is in use.
echo - Ensure that access to Python libraries such as pypi.org is not blocked
echo Terminating startup process...
rem rem rem rem rem Make New Error Code and Page
call :logStageStatus 202 "Failed to install python libraries"
timeout /t 5
goto FINISH
) else (
call :logStartap lapTime "Download pylib: Completed"
)
if exist "%path_R%" (
if "%prod%"=="%product_dn%" "%path_R%\bin\R" CMD BATCH "r_install_packages.r"
)
call :logStartap lapTime "Install pylib: Completed"
: log application status : Installation Completed
rem call :readIniSettings "%stat_ini%"
call :getDateTime current
call :getTimeDiff "%startTime%" "%current%" diff
if not defined initial_date set "initial_date=%current%"
if not defined install_path (
set "log="
) else (
set "log=%install_date%, %install_span%, %install_vers%, %install_prod%, %install_path%"
)
>"%stat_ini%" (
echo(;; This INI file is controlled by AnalysisPlatform.bat installer
echo(; Application Status
echo(install_vers=%app_ver%
echo(install_prod=%app_prd%
echo(install_path=%~dp0
echo(install_date=%current%
echo(install_from=%startTime%
echo(install_span=%diff%
echo(initial_date=%initial_date%
echo(install_log1=%log%
echo(install_log2=%install_log1%
echo(install_log3=%install_log2%
)
call :logStartap lapTime "Install Completed"
echo.
echo Download components, libraries and installation is completed.
echo.
: _____________________________________________________________________________
: Run App: Analysis Platform
: _____________________________________________________________________________
:Start_AP
call :mode_cmd_run_ap "Keep"
call :logStartap startTime "[Start AP] Startup process initiated"
@REM :REMOVE_ZIPPED_FILES
@REM rem ::::::Blocking this from running
@REM if "%status%"=="" (
@REM IF exist "%path_oc_zip%" (
@REM del "%path_oc_zip%"
@REM echo Removed Oracle zipped files.
@REM call :logStartap lapTime "Delete zip Oracle"
@REM )
@REM IF exist "%path_py_zip%" (
@REM del "%path_py_zip%"
@REM echo Removed Python embedded zipped files.
@REM call :logStartap lapTime "Delete zip python"
@REM )
@REM echo.
@REM )
: -----------------------------------------------------------------------------
:: Activate python in virtual environment (2nd for instllation 1st for RUN APP)
echo Activate Virtual Environment
call "%path_env%\Scripts\activate.bat" ^
&& call :logStartap startTime "virtualenv activated: %VIRTUAL_ENV%" ^
|| call :logStartap startTime "virtualenv activated: Failed"
:: python isolation - Lib and DLLs for virtualenv in RUN APP
set "PYTHONHOME=%path_python%"
call :checkPythonIsolation "%main_venvpy%"
:: Set Proxy to venv
if "%proxy%"=="Use Proxy" (
if defined target_prxy (set "HTTP_PROXY=%target_prxy%")
if defined target_prxs (set "HTTP_PROXY=%target_prxs%")
)
call :logStartap lapTime "Set HTTP_PROXY%tab%[%HTTP_PROXY%]"
call :logStartap lapTime "Set HTTPS_PROXY%tab%[%HTTPS_PROXY%]"
: ensure to assign the app title
title %app_title%
call :saveStartUpSetting
call :logStartap lapTime "Activate and set proxy to venv python"
echo.
: Skip main.py
if "%only_install%"=="1" (
echo Skip AP startup and end sequences. "only_install" option is enabled.
rem Keep CMD stay when install check (control by %/a %)
goto FINISH
)
if /i "%arg1%"=="skip_main" goto FINISH
:::::::Temporarily stopped. Modify Python side and remove this.
call :stopLoadingApp
: Minimize com.exe window
call :mode_cmd_run_ap
REM run application
echo Starting Up Analysis Platform... %clTitle% port:%port% %cd% %cl_Base%
echo.
call :logStartap startTime "Run main.py"
"%main_venvpy%" -I "%cd%\main.py"
rem "%main_venvpy%" -I -c "import sys, runpy; sys.path.insert(0, r'%CD%'); runpy.run_path(r'%CD%\main.py', run_name='__main__')"
echo.
echo.
: Close CMD.exe if no error
:FINISH
call :logStartap startTime "[Closing] Closing process initiated errCount:%errCount%"
echo.
echo.
echo ===== Closing AP+DN7 =====
echo.
call :stopLoadingApp
for /f "usebackq tokens=1" %%a in ("%log_stageStatus%") do ( set "errCode=%%a" )
if %errCount% neq 0 (
rem In case error occur, terminal window is not closed and show error to user know that.
echo %clAlarm% Abnormal Termination %cl_Base%
echo Error Count : %errCount%
echo Error Code : %errCode%
call :logStartap startTime "===== Error End Code:%errCode% Count: %errCount%"
exit /b 0
)
if "%only_install%"=="1" (
echo Installation Completed ...
exit /b 0
)
rem call :stopBrowser
rem In case of NO ERROR, terminal window will be closed automatically in 5 seconds.
echo Normal End
timeout /t 5
:: find current running cmd and stop it through title
::title STOPPING...
::powershell -ExecutionPolicy Bypass -Command Get-Process ^| Where-Object { $_.Path -like \"*cmd.exe\" -and $_.MainWindowTitle -like \"STOPPING...*\" } ^| ForEach-Object { Stop-Process -Id $_.Id -Force }
if /I "%arg1%"=="skip_main" (
echo skip end
exit /b 0
)
exit
: _____________________________________________________________________________
: Sub Program
: _____________________________________________________________________________
: _____________________________________________________________________________
: Subroutine: function DownloadViaBITS
: In/Out - | In %1: Source URL
: | In %2: Destination file path
: | Out %ERRORLEVEL: 0=Success / non-zero=Failure
:downloadViaBITS
set "url=%~1"
set "dst=%~2"
%pshell% -Command ^
$ErrorActionPreference='Stop'; $url=$env:url; $dst=$env:dst; $ok=$false; ^
try { ^
$job = Start-BitsTransfer -Source $url -Destination $dst ^
-DisplayName 'BITS Download' -Description 'BITS normal' ^
-ProxyUsage PreConfig -ErrorAction Stop; ^
Complete-BitsTransfer -BitsJob $job; exit 1 ^
} catch { } ^
try { ^
$job = Start-BitsTransfer -Source $url -Destination $dst ^
-DisplayName 'BITS Download' -Description 'BITS normal' ^
-ProxyUsage Override -ProxyList $env:HTTPS_PROXY -ErrorAction Stop; ^
Complete-BitsTransfer -BitsJob $job; exit 2 ^
} catch { } ^
try { ^
$job = Start-BitsTransfer -Source $url -Destination $dst ^
-DisplayName 'BITS Download' -Description 'BITS normal' ^
-ProxyUsage NoProxy -ErrorAction Stop; ^
Complete-BitsTransfer -BitsJob $job; exit 3 ^
} catch { } ^
exit 4
set "err=%errorlevel%"
exit /b %err%
:end
: _____________________________________________________________________________
: Subroutine: function DownloadFile
: In/Out - | In %1: Source URL
: | In %2: Destination file path
: | Out %ERRORLEVEL: 0=Success / non-zero=Failure
:downloadFile
set "url=%~1"
set "dst=%~2"
set "stage=%~3"
set "tgt=%~nx2"
if "%url%"=="" exit /b 2
if "%dst%"=="" exit /b 2
for %%D in ("%dst%") do if not exist "%%~dpD" mkdir "%%~dpD"
:: Invoke-WebRequest by 1:UseProxy 2:NoProxy 3:WinHTTP 4:WinINET
call :logStartap startTime "Download by Invk%tab%%tgt%"
%pshell% -Command ^
$ErrorActionPreference='Stop'; $url=$env:url; $dst=$env:dst; ^
try { ^
$st = Get-Date -Format 'yyyy-MM-dd HH:mm:ss.fff'; ^
$base = Split-Path -Path $dst -Leaf; $tab = [char]9; ^
$self_pid = (Get-Process -Id $PID).Parent.Id; ^
$log = $st, $st, 'zip', '00:00', 'PID:'+$PID, 'PS Invoke-WebReq', $base, $self_pid -join $tab; ^
Add-Content -LiteralPath $env:log_startap -Value $log; ^
if(-not [string]::IsNullOrWhiteSpace($env:stage)) { ^
Add-Content -LiteralPath $env:log_stageTime -Value ('{0}-{1}-{2}' -f $PID, $env:stage, $st) }; ^
} catch { } ^
$proxy = if (-not [string]::IsNullOrWhiteSpace($env:HTTPS_PROXY)) { $env:HTTPS_PROXY } ^
elseif (-not [string]::IsNullOrWhiteSpace($env:HTTP_PROXY)) { $env:HTTP_PROXY } ^
else { $null } ^
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12; ^
try { ^
if (-not [string]::IsNullOrWhiteSpace($proxy)) { ^
Invoke-WebRequest -Uri $url -OutFile $dst -UseBasicParsing ^
-Proxy $proxy -ProxyUseDefaultCredentials ^
} else { ^
Invoke-WebRequest -Uri $url -OutFile $dst -UseBasicParsing ^
}; exit 1 ^
} catch { } ^
try { ^
[System.Net.WebRequest]::DefaultWebProxy = $null; ^
Invoke-WebRequest -Uri $url -OutFile $dst -UseBasicParsing; exit 2 ^
} catch { } ^
try { ^
$winhttp = netsh winhttp show proxy; ^
$proxy = ($winhttp ^| Select-String -Pattern 'Proxy Server.*:\s*(.+)$').Matches.Groups[1].Value.Trim(); ^
Invoke-WebRequest -Uri $url -OutFile $dst -UseBasicParsing ^
-Proxy $proxy -ProxyUseDefaultCredentials; exit 3 ^
} catch { } ^
try { ^
$p=[System.Net.WebRequest]::GetSystemWebProxy(); ^
$p.Credentials=[System.Net.CredentialCache]::DefaultNetworkCredentials; ^
[System.Net.WebRequest]::DefaultWebProxy=$p; ^
Invoke-WebRequest -Uri $url -OutFile $dst -UseBasicParsing ^
-Proxy $proxy -ProxyUseDefaultCredentials; exit 4 ^
} catch { } ^
exit 5
if not errorlevel 5 (
if errorlevel 4 ( call :logStartap lapTime "Get File by Invk%tab%%tgt%%tab%via WinINET"
) else ( if errorlevel 3 ( call :logStartap lapTime "Get File by Invk%tab%%tgt%%tab%via WinHTTP"
) else ( if errorlevel 2 ( call :logStartap lapTime "Get File by Invk%tab%%tgt%%tab%via NoProxy"
) else ( if errorlevel 1 ( call :logStartap lapTime "Get File by Invk%tab%%tgt%%tab%via Proxy" ))))
exit /b 0
) else (
call :logStartap lapTime "Download by BITS%tab%%tgt%"
echo ---- Try BITS
call :downloadViaBITS "%url%" "%dst%"
)
if not errorlevel 4 (
if errorlevel 3 ( call :logStartap lapTime "Get File by BITS%tab%%tgt%%tab%via NoProxy"
) else ( if errorlevel 2 ( call :logStartap lapTime "Get File by BITS%tab%%tgt%%tab%via Proxy"
) else ( if errorlevel 1 ( call :logStartap lapTime "Get File by BITS%tab%%tgt%%tab%via PreConfig" )))
exit /b 0
) else (
call :logStartap lapTime "Download by CAUt%tab%%tgt%"
echo ---- Try Certutil
certutil -urlcache -split -f "%url%" "%dst%"
)
if not errorlevel 1 (
call :logStartap lapTime "Get File by CAUt%tab%%tgt%"
exit /b 0
) else (
call :logStartap lapTime "Download by Curl%tab%%tgt%"
echo ---- Try Curl
curl.exe -v %HTTPS_PROXY%
curl.exe --fail --silent --show-error --location "%url%" -o "%dst%"
rem Error on Curl: Check network connection or use latest Win10 ^>1803
)
if not exist "%dst%" exit /b 1
exit /b 0
:end
: _____________________________________________________________________________
: Subroutine: Check Python library
: In/Out - |In -
:checkPythonLib_rough
echo Check python libraries
rem Packages=Flask numpy scikit-learn chardet SQLAlchemy cutlet networkx
rem set "lib_python=flask numpy sklearn chardet sqlalchemy cutlet networkx" :: Take Time
set "lib_python=flask chardet sqlalchemy networkx"
set "pycode=import %lib_python: =; import %"
"%main_venvpy%" -I -c "%pycode%" 1>nul 2>&1
if errorlevel 1 (
echo [Judge] One or more imports failed.
call :logStartap lapTime "Check pylib rouph: Missing"
exit /b 1
) else (
echo Detect python libraries: Pass
call :logStartap lapTime "Check pylib rough: Exist"
)
exit /b 0
:end
:checkPythonLib_full
set "file_com=%path_req%\common.txt"
set "file_req=%path_req%\reqall.txt"
set "file_frz=%path_req%\freeze.txt"
set "file_dif=%path_req%\reqdif.txt"
set "exceptlb=(Babel|pyper)"
if not exist "%main_pip%" (
echo python libraries missing
call :logStartap lapTime "Check pylib full: Missing pip"
rem exit /b 2
)
rem Generate list of libraries that are not installed in file_dif, but keep only Flask-Babel if it differs by case.
if exist "%main_venvpy%" (
"%main_venvpy%" -I -m pip freeze | sort > %file_frz% ^
&& call :logStartap lapTime "Freeze (Installed lib) List: %file_frz%"
) else (
call :logStartap lapTime "Check pylib full: Missing python in virtualenv"
type nul > "%file_frz%"
)
rem Join two requirements files into file_req
%pshell% -Command ^
$req_com=$env:file_com; $req_prd=$env:requirements; $req_all=$env:file_req; ^
Get-Content $req_com, $req_prd ^| Select-String -NotMatch 'common.txt' ^
^| Sort-Object ^| Set-Content -LiteralPath $req_all -Encoding UTF8 ^
&& call :logStartap lapTime "Create All Requirements List: %file_req%"
rem Output the differences between the two files to file_dif
%pshell% -Command ^
$ErrorActionPreference='Stop'; ^
$req_all=$env:file_req; $lib_frz=$env:file_frz; $lib_dif=$env:file_dif; $exc_lib=$env:exceptlb; ^
$req = Get-Content -LiteralPath $req_all ^| Where-Object { $_.Trim() -ne '' } ^
^| ForEach-Object { $line = $_ -replace '\[.*?\]', ''; $line = $line -replace '~', '='; $line.Trim() }; ^
$frz = Get-Content -LiteralPath $lib_frz ^| Where-Object { $_.Trim() -ne '' }; ^
$dif = $req ^| Where-Object { $_ -cnotin $frz }; ^
$dif ^| Set-Content -LiteralPath $lib_dif -Encoding UTF8; ^
$dif = $dif ^| Where-Object { $_ -notmatch $exc_lib } ^| Where-Object { $_ -notmatch '^\s*#' }; ^
if (@($dif).Count -eq 0) { exit 0 } else { exit 1 } && (
call :logStartap lapTime "Create Dif Requirements List: %file_dif% - Missing library except keyword in exceptlb"
echo python libraries exist
call :logStartap lapTime "Check pylib full: Exist"
exit /b 0
) || (
call :logStartap lapTime "Create Dif Requirements List: %file_dif% - Missing library except keyword in exceptlb"
echo python libraries missing
call :logStartap lapTime "Check pylib full: Missing"
exit /b 1
)
exit /b 0
:end
: _____________________________________________________________________________
: Subroutine: Download Python Embedded
: In/Out - |In -
:downloadPythonEmbedded
echo Download python
if not exist "%path_py_zip%" (
call :downloadFile "%url_python%" "%path_py_zip%" "PYTHON_EMBEDDED" && (
call :logStartap lapTime "Download Completed%tab%%path_py_zip%"
) || (
echo %clAlarm% Error on CA Check network connection or network security %cl_Base%
echo Fail to download Python Embedded
call :logStageStatus 200
exit /b 1
)
)
echo Unzip python
if exist "%path_py_zip%" (
echo Unzip python_embedded
%pshell% -Command ^
$ErrorActionPreference='Stop'; $zip=$env:path_py_zip; $dst=$env:path_python; ^
Expand-Archive -Path $zip -DestinationPath $dst -Force && (
call :logStartap lapTime "Unzip Completed%tab%python.zip"
) || (
call :logStartap lapTime "Unzip Failed%tab%python.zip"
)
)
call :setpyIsoMode
exit /b 0
:end
: _____________________________________________________________________________
: Subroutine: Download Oracle Instance
: In/Out - |In -
:downloadOracleInstance
setlocal EnableExtensions EnableDelayedExpansion
if /i "%arg1%"==":downloadOracleInstance" (
call :logStartap lapTime "Background Download%tab%%path_oc_zip%"
)
echo Download oracle instance
if not exist "%path_oc_zip%" (
call :downloadFile "%url_oracle%" "%path_oc_zip%" "ORACLE_INSTANCE" || (
echo %clAlarm% Error on CA Check network connection or network security %cl_Base%
echo Fail to download Oracle instance
call :logStageStatus 210
pause
endlocal & exit /b 1
)
call :logStartap lapTime "Download Completed%tab%%path_oc_zip%"
)
echo Unzip oracle instance
if exist "%path_oc_zip%" (
%pshell% -Command ^
$ErrorActionPreference='Stop'; $zip=$env:path_oc_zip; $dst=$env:path_oracle; ^
Expand-Archive -Force -Path $zip -DestinationPath $dst && (
call :logStartap lapTime "Unzip Completed%tab%Oracle.zip"
) || (
echo %clAlarm% Error on Unzip File %cl_Base%
echo Fail to unzip Oracle instance
call :logStartap lapTime "Unzip Failed%tab%%Oracle.zip"
call :logStageStatus 210
pause
endlocal & exit /b 1
)
if /i "%arg1%"==":downloadOracleInstance" (
echo Install Completed%tab%%path_oc_zip%
call :logStartap startTime "Background Install Completed: Oracle.lib"
)
)
if /i "%arg1%"==":downloadOracleInstance" (
timeout /t 30
)
timeout /t 30
endlocal & exit /b 0
:end
: _____________________________________________________________________________
: Subroutine: Check python Isolation
: In/Out - |In -
:checkPythonIsolation
set "py_exe=%~1"
if %py_exe% == %main_python% set "py=py_embd"
if %py_exe% == %main_venvpy% set "py=py_venv"
call :logStartap lapTime "%py% PYTHONHOME=%PYTHONHOME% PYTHONNOUSERSITE=%PYTHONNOUSERSITE%"
:: Check python settings and sys path of virtual environment
for /f "delims=" %%a in ('%py_exe% -c "import sys; print(sys.prefix)"') do set "sys_pref=%%a"
for /f "delims=" %%a in ('%py_exe% -c "import site; print(getattr(site,'getusersitepackages',lambda:'N/A')())"') do set "site_usr=%%a"
call :logStartap startTime "%py% sys.prefix: %sys_pref% usersite: %site_usr%"
rem for /f "delims=" %%a in ('%py_exe% -c "import sys; print(sys.path)"') do set "sys_path=%%a"
for /f "delims=" %%a in ('
%py_exe% -c "import sys; print('\n'.join(sys.path))"
') do (
call :logStartap lapTime "%py% sys.path: %%a"
)
for /f "delims=" %%a in ('%py_exe% -c "import sys,os,os.path as P; base=P.normcase(P.normpath(os.path.expandvars(r'%LocalAppData%\Programs\Python'))); print(any(P.normcase(P.normpath(p or '')) .startswith(base) for p in sys.path))"') do set "sys_appd=%%a"
call :logStartap lapTime "%py% AppData Lib/DLL in sys.path: %sys_appd% (Should be False in isolation)"
for /f "delims=" %%a in ('%py_exe% -c "import sys,site; print(any(p and p.lower()==site.getusersitepackages().lower() for p in sys.path))"') do set "sys_site=%%a"
call :logStartap lapTime "%py% site:user.local in sys.path: %sys_site% (Should be False in isolation)"
for /f "delims=" %%a in ('%py_exe% -c "import site; print(site.ENABLE_USER_SITE)"') do set "site_ena=%%a"
call :logStartap lapTime "%py% site.ENABLE_USER_SITE: %site_ena% (Should be False in isolation)"
exit /b 0
:end
: _____________________________________________________________________________
: Subroutine: Download pip.pyz, pip.whl and Install pip.exe
: In/Out - |In -
:downloadPipByPipPyz
call :logStartap startTime "Try to install pip with pip.pyz"
if exist "%path_pi_pyz%" (
call :logStartap startTime "Detect pip.pyz"
) else (
call :downloadFile "%url_pippyz%" "%path_pi_pyz%" "PIP_DOWNLOAD" ^
&& call :logStartap lapTime "Fetched pip.pyz"
)
set "cmd_pyz=""%main_python%"" -I ""%path_pi_pyz%"" download pip==%ver_pip% -d ""%rapid_dirCache%"""
set "cmd_pyz=%cmd_pyz% --exists-action=i --retries 2 --disable-pip-version-check --only-binary=:all: --no-deps"
call :pyGetComp "%cmd_pyz%" "Download pip.pyz" "pip.pyz"
set "cmd_pyz=""%main_python%"" -I ""%path_pi_pyz%"" install pip==%ver_pip% --find-links ""%rapid_dirCache%"""
set "cmd_pyz=%cmd_pyz% --exists-action=i --retries 2 --disable-pip-version-check --no-warn-script-location"
call :pyGetComp "%cmd_pyz%" "Download pip by pyz" "pip/pip.pyz"
call :get_version_pip
exit /b 0
:end
: _____________________________________________________________________________
: Subroutine: function Get pip Version
: In/Out - |In -
:get_version_pip
set "cur_pip=Invalid"
for /f "tokens=2 delims= " %%a in ('"%main_python%" -I -m pip --version') do set "cur_pip=%%~a"
echo Current pip version: %cl_Note%%cur_pip%%cl_Base% target: %ver_pip%
call :logStartap lapTime "pip version: %cur_pip% target: %ver_pip%"
exit /b 0
:end
:get_version_pip_env
set "env_pip=Invalid"
for /f "tokens=2 delims= " %%a in ('"%main_venvpy%" -I -m pip --version') do set "env_pip=%%~a"
for /f "delims=" %%a in ('
%main_venvpy% -c "import sys; sys.dont_write_bytecode=True; import pip; print(getattr(pip,'__version__',''))"
') do set "epy_var=%%a"
echo Current venv pip version: %cl_Note%%env_pip%%cl_Base% byPy: %epy_var% target: %ver_pip%
call :logStartap lapTime "venv pip version: %env_pip% byPy: %epy_var% target: %ver_pip%"
exit /b 0
:end
: _____________________________________________________________________________
: Subroutine: function Get pip Version
: In/Out - |In -
:setpyIsoMode
if not exist "%path_py_pth%.bak" copy "%path_py_pth%" "%path_py_pth%.bak"
: Activate python isolated mode
for %%a in ("Lib" "Lib\site-packages" "import site") do (
findstr /x /c:%%a "%path_py_pth%" >nul || echo %%~a>>"%path_py_pth%"
)
call :logStartap startTime "Set python Isolated mode"
exit /b 0
:end
: _____________________________________________________________________________
: Subroutine: function Get pip.pyz and install pip
: In/Out - |In -
:preparepip
rem Remove incomplete installation folder like ~ip
for /d %%d in ("%path_python%\Lib\site-packages\~*") do ( rmdir /s /q "%%d" )
call :get_version_pip
if "%cur_pip%"=="%ver_pip%" (
echo pip is available
goto preparation_pip
) else (
echo pip is missing or old
)
if "%cur_pip%"=="Invalid" call :setpyIsoMode
call :downloadPip
if not "%rapid_useCache%"=="True" exit /b 0
echo Install pip.pyz and pip
if not exist "%rapid_dirCache%" mkdir "%rapid_dirCache%"
call :downloadPipByPipPyz
:preparation_pip
:: virtualenv is downloaded here
set "pkg=%pkg_com% %pkg_env%"
set "cmd_dlp=""%main_python%"" -I -m pip download %pkg% -d ""%rapid_dirCache%"""
set "cmd_dlp=%cmd_dlp% --exists-action=i --only-binary=:all: --retries 2"
call :pyGetComp "%cmd_dlp%" "DownloadPre seed" "seed.whl" && set /a err=0 || set /a err=1
if %err% neq 0 (
set "cmd_dlp=""%main_python%"" -I -m pip download %pkg% -d ""%rapid_dirCache%"""
set "cmd_dlp=%cmd_dlp% --exists-action=i --retries 2"
call :pyGetComp "%cmd_dlp%" "DownloadPre seed#" "seed.whl"
)
:: pip install
set "cmd_pip=""%main_python%"" -I -m pip install %pkg% --find-links ""%rapid_dirCache%"""
set "cmd_pip=%cmd_pip% --no-index --only-binary=:all: --no-warn-script-location --retries 2"
call :pyGetComp "%cmd_pip%" "InstallPre seed" "seed.exe"
exit /b 0
:end
: _____________________________________________________________________________
: Subroutine: function Get wheel
: In/Out - |In -
:getpylib