/ tools / windows / tool_setup / python_page.iss.inc
python_page.iss.inc
  1  { Copyright 2019 Espressif Systems (Shanghai) PTE LTD
  2    SPDX-License-Identifier: Apache-2.0 }
  3  
  4  { ------------------------------ Page to select Python interpreter ------------------------------ }
  5  
  6  #include "python_find_installed.iss.inc"
  7  
  8  var
  9    PythonPage: TInputOptionWizardPage;
 10    PythonVersion, PythonPath, PythonExecutablePath: String;
 11    PythonUseExisting: Boolean;
 12  
 13  
 14  function GetPythonPath(Unused: String): String;
 15  begin
 16    Result := PythonPath;
 17  end;
 18  
 19  function PythonInstallRequired(): Boolean;
 20  begin
 21    Result := not PythonUseExisting;
 22  end;
 23  
 24  function PythonVersionSupported(Version: String): Boolean;
 25  var
 26    Major, Minor: Integer;
 27  begin
 28    Result := False;
 29    if not VersionExtractMajorMinor(Version, Major, Minor) then
 30    begin
 31      Log('PythonVersionSupported: Could not parse version=' + Version);
 32      exit;
 33    end;
 34  
 35    if (Major = 2) and (Minor = 7) then Result := True;
 36    if (Major = 3) and (Minor >= 5) then Result := True;
 37  end;
 38  
 39  procedure OnPythonPagePrepare(Sender: TObject);
 40  var
 41    Page: TInputOptionWizardPage;
 42    FullName: String;
 43    i, Index, FirstEnabledIndex: Integer;
 44    OfferToInstall: Boolean;
 45    VersionToInstall: String;
 46    VersionSupported: Boolean;
 47  begin
 48    Page := TInputOptionWizardPage(Sender);
 49    Log('OnPythonPagePrepare');
 50    if Page.CheckListBox.Items.Count > 0 then
 51      exit;
 52  
 53    FindInstalledPythonVersions();
 54  
 55    VersionToInstall := '{#PythonVersion}';
 56    OfferToInstall := True;
 57    FirstEnabledIndex := -1;
 58  
 59    for i := 0 to InstalledPythonVersions.Count - 1 do
 60    begin
 61      VersionSupported := PythonVersionSupported(InstalledPythonVersions[i]);
 62      FullName := InstalledPythonDisplayNames.Strings[i];
 63      if not VersionSupported then
 64      begin
 65        FullName := FullName + ' (unsupported)';
 66      end;
 67      FullName := FullName + #13#10 + InstalledPythonExecutables.Strings[i];
 68      Index := Page.Add(FullName);
 69      if not VersionSupported then
 70      begin
 71        Page.CheckListBox.ItemEnabled[Index] := False;
 72      end else begin
 73        if FirstEnabledIndex < 0 then FirstEnabledIndex := Index;
 74      end;
 75      if InstalledPythonVersions[i] = VersionToInstall then
 76      begin
 77        OfferToInstall := False;
 78      end;
 79    end;
 80  
 81    if OfferToInstall then
 82    begin
 83      Index := Page.Add('Install Python ' + VersionToInstall);
 84      if FirstEnabledIndex < 0 then FirstEnabledIndex := Index;
 85    end;
 86  
 87    Page.SelectedValueIndex := FirstEnabledIndex;
 88  end;
 89  
 90  procedure OnPythonSelectionChange(Sender: TObject);
 91  var
 92    Page: TInputOptionWizardPage;
 93  begin
 94    Page := TInputOptionWizardPage(Sender);
 95    Log('OnPythonSelectionChange index=' + IntToStr(Page.SelectedValueIndex));
 96  end;
 97  
 98  function OnPythonPageValidate(Sender: TWizardPage): Boolean;
 99  var
100    Page: TInputOptionWizardPage;
101  begin
102    Page := TInputOptionWizardPage(Sender);
103    Log('OnPythonPageValidate index=' + IntToStr(Page.SelectedValueIndex));
104    if Page.SelectedValueIndex < InstalledPythonExecutables.Count then
105    begin
106      PythonUseExisting := True;
107      PythonExecutablePath := InstalledPythonExecutables[Page.SelectedValueIndex];
108      PythonPath := ExtractFilePath(PythonExecutablePath);
109      PythonVersion := InstalledPythonVersions[Page.SelectedValueIndex];
110    end else begin
111      PythonUseExisting := False;
112      PythonExecutablePath := '';
113      PythonPath := '';
114      PythonVersion := '{#PythonVersion}';
115    end;
116    Log('OnPythonPageValidate: PythonPath='+PythonPath+' PythonExecutablePath='+PythonExecutablePath);
117    Result := True;
118  end;
119  
120  procedure PythonExecutablePathUpdateAfterInstall();
121  var
122    Version, DisplayName, ExecutablePath: String;
123  begin
124    if not GetPythonVersionInfoFromKey(
125        HKEY_CURRENT_USER, 'Software\Python', 'PythonCore', '{#PythonVersion}',
126        Version, DisplayName, ExecutablePath) then
127    begin
128      Log('Failed to find ExecutablePath for the installed copy of Python');
129      exit;
130    end;
131    Log('Found ExecutablePath for ' + DisplayName + ': ' + ExecutablePath);
132    PythonExecutablePath := ExecutablePath;
133    PythonPath := ExtractFilePath(PythonExecutablePath);
134    Log('PythonExecutablePathUpdateAfterInstall: PythonPath='+PythonPath+' PythonExecutablePath='+PythonExecutablePath);
135  end;
136  
137  <event('InitializeWizard')>
138  procedure CreatePythonPage();
139  begin
140    PythonPage := ChoicePageCreate(
141      wpLicense,
142      'Python choice', 'Please choose Python version',
143      'Available Python versions',
144      '',
145      False,
146      @OnPythonPagePrepare,
147      @OnPythonSelectionChange,
148      @OnPythonPageValidate);
149  end;