|
| 1 | +(**************************************************************************) |
| 2 | +(* *) |
| 3 | +(* Module: Unit 'AppEnvironment' Copyright (c) 2021 *) |
| 4 | +(* *) |
| 5 | +(* Lucas Moura Belo - lmbelo *) |
| 6 | +(* lucas.belo@live.com *) |
| 7 | +(* Brazil *) |
| 8 | +(* *) |
| 9 | +(* PyScripter *) |
| 10 | +(* e-mail: pyscripter@gmail.com *) |
| 11 | +(* *) |
| 12 | +(* Project pages: https://github.com/Embarcadero/python4delphi *) |
| 13 | +(* https://github.com/pyscripter/python4delphi *) |
| 14 | +(**************************************************************************) |
| 15 | +(* Functionality: App environment set up *) |
| 16 | +(* *) |
| 17 | +(* *) |
| 18 | +(**************************************************************************) |
| 19 | +(* This source code is distributed with no WARRANTY, for no reason or use.*) |
| 20 | +(* Everyone is allowed to use and change this code free for his own tasks *) |
| 21 | +(* and projects, as long as this header and its copyright text is intact. *) |
| 22 | +(* For changed versions of this code, which are public distributed the *) |
| 23 | +(* following additional conditions have to be fullfilled: *) |
| 24 | +(* 1) The header has to contain a comment on the change and the author of *) |
| 25 | +(* it. *) |
| 26 | +(* 2) A copy of the changed source has to be sent to the above E-Mail *) |
| 27 | +(* address or my then valid address, if this is possible to the *) |
| 28 | +(* author. *) |
| 29 | +(* The second condition has the target to maintain an up to date central *) |
| 30 | +(* version of the component. If this condition is not acceptable for *) |
| 31 | +(* confidential or legal reasons, everyone is free to derive a component *) |
| 32 | +(* or to generate a diff file to my or other original sources. *) |
| 33 | +(**************************************************************************) |
| 34 | +unit AppEnvironment; |
| 35 | + |
| 36 | +interface |
| 37 | + |
| 38 | +uses |
| 39 | + System.Classes, System.SysUtils, System.Threading, System.Zip, PythonEngine; |
| 40 | + |
| 41 | +type |
| 42 | + IProgressNotifier = interface |
| 43 | + ['{7A2D1743-D4D8-4093-B372-04D814536708}'] |
| 44 | + procedure Start(const ADescription, AFirstAction: string; const ATotal: Int64); |
| 45 | + procedure Update(const ACurrentAction: string; const AProgress: Int64); |
| 46 | + procedure Stop(); |
| 47 | + end; |
| 48 | + |
| 49 | + TAppEnvInit = reference to procedure(const AInitialized: boolean; const ALastErrorMsg: string); |
| 50 | + |
| 51 | + TAppEnvironment = class |
| 52 | + private |
| 53 | + FInitialized: boolean; |
| 54 | + FProgressNotifier: IProgressNotifier; |
| 55 | + procedure OnZipProgressEvent(Sender: TObject; FileName: string; Header: TZipHeader; Position: Int64); |
| 56 | + procedure DoInitializeEnvironmentAsync(const APythonEngine: TPythonEngine; |
| 57 | + const ACheckPyLib: boolean = true); |
| 58 | + public |
| 59 | + constructor Create(const AProgressNotifier: IProgressNotifier); |
| 60 | + |
| 61 | + procedure InitializeEnvironmentAsync(const APythonEngine: TPythonEngine; |
| 62 | + const ACheckPyLib: boolean; const AAppEnvInit: TAppEnvInit); |
| 63 | + |
| 64 | + property Initialized: boolean read FInitialized; |
| 65 | + end; |
| 66 | + |
| 67 | +implementation |
| 68 | + |
| 69 | +uses |
| 70 | + System.IOUtils, FMX.Dialogs, PythonLoad; |
| 71 | + |
| 72 | +{ TAppEnvironment } |
| 73 | + |
| 74 | +constructor TAppEnvironment.Create(const AProgressNotifier: IProgressNotifier); |
| 75 | +begin |
| 76 | + Assert(Assigned(AProgressNotifier), '"AProgressNotifier" undefined'); |
| 77 | + FInitialized := false; |
| 78 | + FProgressNotifier := AProgressNotifier; |
| 79 | +end; |
| 80 | + |
| 81 | +procedure TAppEnvironment.DoInitializeEnvironmentAsync(const APythonEngine: TPythonEngine; |
| 82 | + const ACheckPyLib: boolean = true); |
| 83 | +begin |
| 84 | + try |
| 85 | + //Lock user iteractions |
| 86 | + TThread.Synchronize(nil, procedure() begin |
| 87 | + FProgressNotifier.Start('Searching for Python installation', String.Empty, 0); |
| 88 | + end); |
| 89 | + |
| 90 | + Sleep(200); |
| 91 | + |
| 92 | + //Python distibution unzip |
| 93 | + TPythonLoad.Extract( |
| 94 | + procedure(const AFolderExists: boolean; var AReplaceFiles: boolean) begin |
| 95 | + if not AFolderExists then begin |
| 96 | + TThread.Synchronize(nil, procedure() begin |
| 97 | + FProgressNotifier.Start('Installing Python', String.Empty, 0); |
| 98 | + end); |
| 99 | + end; |
| 100 | + AReplaceFiles := false; |
| 101 | + end, OnZipProgressEvent); |
| 102 | + |
| 103 | + //Configure Python for Android |
| 104 | + TThread.Synchronize(nil, procedure() begin |
| 105 | + FProgressNotifier.Start('Configuring Python for Android', String.Empty, 3); |
| 106 | + FProgressNotifier.Update('Check for files', 1) |
| 107 | + end); |
| 108 | + |
| 109 | + TPythonLoad.Configure(APythonEngine); |
| 110 | + Sleep(1000); |
| 111 | + |
| 112 | + //Load python library |
| 113 | + TThread.Synchronize(nil, procedure() begin |
| 114 | + FProgressNotifier.Start('Loading Python', String.Empty, 3); |
| 115 | + FProgressNotifier.Update('Loading and mapping library', 2) |
| 116 | + end); |
| 117 | + |
| 118 | + TThread.Synchronize(nil, procedure() begin |
| 119 | + APythonEngine.LoadDll(); |
| 120 | + end); |
| 121 | + Sleep(1000); |
| 122 | + |
| 123 | + //All done notification |
| 124 | + TThread.Synchronize(nil, procedure() begin |
| 125 | + FProgressNotifier.Start('Loading environment', String.Empty, 3); |
| 126 | + FProgressNotifier.Update('All ready', 3) |
| 127 | + end); |
| 128 | + Sleep(1000); |
| 129 | + finally |
| 130 | + TThread.Synchronize(nil, procedure() begin |
| 131 | + FProgressNotifier.Stop(); |
| 132 | + end); |
| 133 | + end; |
| 134 | +end; |
| 135 | + |
| 136 | +procedure TAppEnvironment.InitializeEnvironmentAsync( |
| 137 | + const APythonEngine: TPythonEngine; const ACheckPyLib: boolean; |
| 138 | + const AAppEnvInit: TAppEnvInit); |
| 139 | +begin |
| 140 | + TTask.Run( |
| 141 | + procedure() begin |
| 142 | + try |
| 143 | + DoInitializeEnvironmentAsync(APythonEngine, ACheckPyLib); |
| 144 | + FInitialized := true; |
| 145 | + if Assigned(AAppEnvInit) then |
| 146 | + AAppEnvInit(true, String.Empty); |
| 147 | + except |
| 148 | + on E: Exception do begin |
| 149 | + if Assigned(AAppEnvInit) then |
| 150 | + AAppEnvInit(false, E.Message); |
| 151 | + end; |
| 152 | + end; |
| 153 | + end); |
| 154 | +end; |
| 155 | + |
| 156 | +procedure TAppEnvironment.OnZipProgressEvent(Sender: TObject; FileName: string; |
| 157 | + Header: TZipHeader; Position: Int64); |
| 158 | +begin |
| 159 | + TThread.Queue(nil, procedure() begin |
| 160 | + FProgressNotifier.Start('Extracting files', String.Empty, Header.UncompressedSize); |
| 161 | + FProgressNotifier.Update(TPath.GetFileName(FileName), Position); |
| 162 | + end); |
| 163 | +end; |
| 164 | + |
| 165 | +end. |
0 commit comments