@@ -393,8 +393,70 @@ function ImportCertificateUser($pfxPath, $pfxPassword) {
393393}
394394
395395function ChechFileHash ($path , $hash , $algorithm = " SHA1" ) {
396- $h = Get-Filehash - Algorithm $algorithm $path
397- if ($h .Hash.ToUpper () -ne $hash.ToUpper ()) {
398- throw " Hash comparison failed for file: $path "
396+ $actualHash = ( Get-Filehash - Algorithm $algorithm $path ).Hash.ToUpper()
397+ if ($actualHash -ne $hash.ToUpper ()) {
398+ throw " Hash comparison failed for file: $path . Expected hash: ${hash} . Actual hash: ${actualHash} "
399399 }
400400}
401+
402+
403+ function DownloadInstall-PythonMsi ($platform , $python_template_dir , $pythonVersion , $PythonMsiChecksum , $algorithm = " SHA1" ) {
404+ $platformSuffix = " "
405+ if ($platform -eq " x64" ) {
406+ $platformSuffix = " -amd64"
407+ }
408+
409+ if (Test-Path $python_template_dir ) {
410+ throw " $python_template_dir folder already exists"
411+ }
412+
413+ $pythonInstallerPath = Join-Path (Resolve-Path " ${python_template_dir} /.." ).Path " /python-${pythonVersion}${platformSuffix} .exe"
414+ $pythonVersionEscaped = $pythonVersion.replace (" _" , " ." )
415+ $PythonMsiUrl = " https://www.python.org/ftp/python/${pythonVersionEscaped} /python-${pythonVersionEscaped}${platformSuffix} .exe"
416+
417+ if ($python_template_dir -and (Test-Path $python_template_dir )) {
418+ throw " Python template directory already exists"
419+ }
420+
421+ $tmp_python_template_dir = " ${python_template_dir} _tmp"
422+ if ($tmp_python_template_dir -and (Test-Path $tmp_python_template_dir )) {
423+ throw " Python temp template directory already exists"
424+ }
425+
426+ try {
427+ ExecRetry { DownloadFile $PythonMsiUrl $pythonInstallerPath }
428+ ChechFileHash $pythonInstallerPath $PythonMsiChecksum $algorithm
429+
430+ Write-Host " Trying to uninstall Python using $pythonInstallerPath "
431+ Start-Process - FilePath " ${pythonInstallerPath} " - NoNewWindow - Wait `
432+ - ArgumentList @ (" /quiet" , " /uninstall" )
433+
434+ $package = Get-Package - Name " Python ${pythonVersionEscaped} *" - ErrorAction SilentlyContinue
435+ if ($package ) {
436+ throw " Python package was already installed"
437+ }
438+
439+ Write-Host " Installing Python using $pythonInstallerPath "
440+ Start-Process - FilePath " ${pythonInstallerPath} " - NoNewWindow - Wait `
441+ - ArgumentList @ (" /quiet" , " TargetDir=${tmp_python_template_dir} " , " Include_test=0" , " Include_tcltk=0" , " Include_launcher=0" , " Include_doc=0" )
442+
443+ Copy-Item - Recurse $tmp_python_template_dir $python_template_dir
444+
445+ } finally {
446+
447+ Start-Process - FilePath " ${pythonInstallerPath} " - NoNewWindow - Wait `
448+ - ArgumentList @ (" /quiet" , " /uninstall" )
449+
450+ if (Test-Path $pythonInstallerPath ) {
451+ Remove-Item $pythonInstallerPath
452+ }
453+
454+ if (Test-Path $tmp_python_template_dir ) {
455+ Remove-Item $tmp_python_template_dir - Recurse - Force
456+ }
457+ }
458+ if (! (Test-Path $python_template_dir )) {
459+ throw " $python_template_dir has not been created"
460+ }
461+
462+ }
0 commit comments