python_page.iss.inc 4.3 KB

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