Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Python installer from the https://www.python.org/ #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion BuildAutomation/BuildCloudbaseInitSetup.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Param(
[string]$platform = "x64",
[string]$pythonversion = "3.11_9",
[string]$pythonversion = "3.12.3",
[string]$pythonversionPrelease = "",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume Prelease stands for pre-release (e.g. b1, a6, etc), we could add a comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix the name, it s a typo

[string]$SignX509Thumbprint = $null,
[string]$release = $null,
# Cloudbase-Init repo details
Expand All @@ -18,6 +19,13 @@ $ErrorActionPreference = "Stop"
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
. "$scriptPath\BuildUtils.ps1"

$PythonInstallerSha1Hash = @{
"python-installer-3.12.3-x64.exe" = "B1207FBA545A75841E2DBCA2AD4F17B26414E0C1";
"python-installer-3.12.3-x86.exe" = "FF180F8EA0B126E5A0FAF0A22EC50E96E5B9C5AB";
"python-installer-3.13.0b1-x64.exe" = "46ADF56A03D91D39EA4E8B6F5FFB080C824BDFDA";
"python-installer-3.13.0b1-x86.exe" = "62E6FE0D5C9267275ABDD36302F327EB4E68C794";
}

$platformVCVarsRequired = "x86_amd64"
# On Visual Studio 2019, the mixed x86_amd64 VC variables
# make compilation for x86 use the x64 functions
Expand Down Expand Up @@ -76,6 +84,54 @@ try

$python_template_dir = join-path $cloudbaseInitInstallerDir "Python$($pythonversion.replace('.', ''))_${platform}_Template"

ExecRetry -maxRetryCount 3 {
$pythonVersionInstaller = $pythonversion.Replace("-",".").Replace("_",".").Trim()
$pythonVersionInstallerSuffix = $pythonVersionInstaller
if ($pythonversionPrelease) {
$pythonVersionInstallerSuffix = $pythonversionPrelease
}
$pythonArchInstaller = ""
if ($platform -eq "x64") {
$pythonArchInstaller = "-amd64"
}
$pythonInstallerName = "python-installer-${pythonVersionInstallerSuffix}-${platform}.exe"
$pythonInstallerPath = (Join-Path $pwd $pythonInstallerName)
$PythonInstallerUrl = "https://www.python.org/ftp/python/${pythonVersionInstaller}/python-${pythonVersionInstallerSuffix}${pythonArchInstaller}.exe"
DownloadFile $PythonInstallerUrl $pythonInstallerPath
$expectedSha1Hash = $PythonInstallerSha1Hash[$pythonInstallerName]
if (!$expectedSha1Hash) {
throw "expected Sha1 Hash for ${pythonInstallerPath} is not configured"
}
$sha1Hash = (Get-FileHash -Algorithm SHA1 $pythonInstallerPath).Hash
if ($sha1Hash -ne $expectedSha1Hash) {
throw "$pythonInstallerPath SHA1 hash is: ${sha1Hash}. Expected hash: ${expectedSha1Hash}"
}

try {
Write-Host "Trying to uninstall Python ${pythonVersionInstaller} to ${python_template_dir}"
$installProcess = Start-Process -PassThru -Wait $pythonInstallerPath `
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit concerned about removing existing Python installations, it might mess someone's dev environment.

Instead of uninstalling/installing Python, can we use the "embeddable" zip archives?

e.g. https://www.python.org/ftp/python/3.12.3/python-3.12.3-embed-amd64.zip

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about the embed version too, but it is not that straightforward. The embeded Python is a subset of Python with less features, and we will need to navigate the dependency graph to install the required parts (pip, setuptools and maybe others). You will end up making the required parts of an installer, but with extra steps and without any SHA1 verifiable start point.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have also made a branch with the Embedded version. There are a few things to be aware of the embedded version though as it is not suitable to build CPYTHON extensions:

  • Include folder does not exist
  • Python.lib files do not exist
  • pyconfig.h does not exist

The include folder can be downloaded from the github cpython repository.
Python.lib files can be built from cpython source code or gotten from the MSI installer installation directory, same with pyconfig.h.

If the wheels are already available from pypi, the Embedded version will work without these requirements.

Copy link
Member

@petrutlucian94 petrutlucian94 Jun 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So by getting rid of netifaces, we should be able to easily use the embeddable version. We'd have to do that anyway, since it's no longer maintained.

We can't use wmi because the provider is not available at that stage. What we could do would be to implemented an utils module that calls the following using ctypes: https://github.com/al45tair/netifaces/blob/53fcdb6e5dccc84f6734939cfee1a95d3f470d7b/netifaces.c#L1304

-ArgumentList "/silent /uninstall"
$installProcess.WaitForExit()
if ($installProcess.ExitCode -ne 0) {
Write-Host "Failed to uninstall ${pythonVersionInstaller} from ${python_template_dir}. Exit code: $($installProcess.ExitCode)"
} else {
Write-Host "Uninstalled Python ${pythonVersionInstaller} from ${python_template_dir}"
}
if ($python_template_dir -and (Test-Path $python_template_dir)) {
Remove-Item -Force -Recurse "${python_template_dir}"
}
} catch {Write-Host $_}

Write-Host "Trying to install Python ${pythonVersionInstaller} to ${python_template_dir}"
$installProcess = Start-Process -PassThru -Wait $pythonInstallerPath `
-ArgumentList "/silent TargetDir=${python_template_dir} Include_test=0 Include_tcltk=0 Include_launcher=0 Include_doc=0"
$installProcess.WaitForExit()
if ($installProcess.ExitCode -ne 0) {
throw "Failed to install ${pythonVersionInstaller} to ${python_template_dir}. Exit code: $($installProcess.ExitCode)"
}
Write-Host "Installed Python ${pythonVersionInstaller} to ${python_template_dir}"
}

CheckCopyDir $python_template_dir $python_dir

# Make sure that we don't have temp files from a previous build
Expand Down