python_page.iss.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. { Copyright 2019-2020 Espressif Systems (Shanghai) CO 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. VersionToInstall := '{#PythonVersion}';
  44. OfferToInstall := True;
  45. FirstEnabledIndex := -1;
  46. for i := 0 to InstalledPythonVersions.Count - 1 do
  47. begin
  48. VersionSupported := PythonVersionSupported(InstalledPythonVersions[i]);
  49. FullName := InstalledPythonDisplayNames.Strings[i];
  50. if not VersionSupported then
  51. begin
  52. FullName := FullName + ' (unsupported)';
  53. end;
  54. FullName := FullName + #13#10 + InstalledPythonExecutables.Strings[i];
  55. Index := Page.Add(FullName);
  56. if not VersionSupported then
  57. begin
  58. Page.CheckListBox.ItemEnabled[Index] := False;
  59. end else begin
  60. if FirstEnabledIndex < 0 then FirstEnabledIndex := Index;
  61. end;
  62. if InstalledPythonVersions[i] = VersionToInstall then
  63. begin
  64. OfferToInstall := False;
  65. end;
  66. end;
  67. if OfferToInstall then
  68. begin
  69. Index := Page.Add('Install Python ' + VersionToInstall);
  70. if FirstEnabledIndex < 0 then FirstEnabledIndex := Index;
  71. end;
  72. Page.SelectedValueIndex := FirstEnabledIndex;
  73. end;
  74. procedure OnPythonSelectionChange(Sender: TObject);
  75. var
  76. Page: TInputOptionWizardPage;
  77. begin
  78. Page := TInputOptionWizardPage(Sender);
  79. Log('OnPythonSelectionChange index=' + IntToStr(Page.SelectedValueIndex));
  80. end;
  81. function OnPythonPageValidate(Sender: TWizardPage): Boolean;
  82. var
  83. Page: TInputOptionWizardPage;
  84. begin
  85. Page := TInputOptionWizardPage(Sender);
  86. Log('OnPythonPageValidate index=' + IntToStr(Page.SelectedValueIndex));
  87. if Page.SelectedValueIndex < InstalledPythonExecutables.Count then
  88. begin
  89. PythonUseExisting := True;
  90. PythonExecutablePath := InstalledPythonExecutables[Page.SelectedValueIndex];
  91. PythonPath := ExtractFilePath(PythonExecutablePath);
  92. PythonVersion := InstalledPythonVersions[Page.SelectedValueIndex];
  93. end else begin
  94. PythonUseExisting := False;
  95. PythonExecutablePath := '';
  96. PythonPath := '';
  97. PythonVersion := '{#PythonVersion}';
  98. end;
  99. Log('OnPythonPageValidate: PythonPath='+PythonPath+' PythonExecutablePath='+PythonExecutablePath);
  100. Result := True;
  101. end;
  102. procedure PythonExecutablePathUpdateAfterInstall();
  103. var
  104. Version, DisplayName, ExecutablePath, BaseDir: String;
  105. begin
  106. if not GetPythonVersionInfoFromKey(
  107. HKEY_CURRENT_USER, 'Software\Python', 'PythonCore', '{#PythonVersion}',
  108. Version, DisplayName, ExecutablePath, BaseDir) then
  109. begin
  110. Log('Failed to find ExecutablePath for the installed copy of Python');
  111. exit;
  112. end;
  113. Log('Found ExecutablePath for ' + DisplayName + ': ' + ExecutablePath);
  114. PythonExecutablePath := ExecutablePath;
  115. PythonPath := ExtractFilePath(PythonExecutablePath);
  116. Log('PythonExecutablePathUpdateAfterInstall: PythonPath='+PythonPath+' PythonExecutablePath='+PythonExecutablePath);
  117. end;
  118. <event('InitializeWizard')>
  119. procedure CreatePythonPage();
  120. begin
  121. PythonPage := ChoicePageCreate(
  122. wpLicense,
  123. 'Python choice', 'Please choose Python version',
  124. 'Available Python versions',
  125. '',
  126. False,
  127. @OnPythonPagePrepare,
  128. @OnPythonSelectionChange,
  129. @OnPythonPageValidate);
  130. end;