-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathwin-python-builder.psm1
152 lines (119 loc) · 4.93 KB
/
win-python-builder.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using module "./python-builder.psm1"
class WinPythonBuilder : PythonBuilder {
<#
.SYNOPSIS
Base Python builder class for Windows systems.
.DESCRIPTION
Contains methods required for build Windows Python artifact. Inherited from base PythonBuilder class.
.PARAMETER version
The version of Python that should be built.
.PARAMETER architecture
The architecture with which Python should be built.
.PARAMETER WithPyDebug
The flag that indicates whether Python should be installed with debug symbols.
.PARAMETER InstallationTemplateName
The name of installation script template that will be used in generated artifact.
.PARAMETER InstallationScriptName
The name of generated installation script.
#>
[string] $InstallationTemplateName
[string] $InstallationScriptName
[string] $OutputArtifactName
WinPythonBuilder(
[semver] $version,
[string] $architecture,
[string] $platform
) : Base($version, $architecture, $platform) {
$this.InstallationTemplateName = "win-setup-template.ps1"
$this.InstallationScriptName = "setup.ps1"
$this.OutputArtifactName = "python-$Version-$Platform-$Architecture.zip"
}
[string] GetPythonExtension() {
<#
.SYNOPSIS
Return extension for required version of Python executable.
#>
$extension = if ($this.Version -lt "3.5" -and $this.Version -ge "2.5") { ".msi" } else { ".exe" }
return $extension
}
[string] GetArchitectureExtension() {
<#
.SYNOPSIS
Return architecture suffix for Python executable.
#>
$ArchitectureExtension = ""
if ($this.Architecture -eq "x64") {
if ($this.Version -ge "3.5") {
$ArchitectureExtension = "-amd64"
} else {
$ArchitectureExtension = ".amd64"
}
} elseif ($this.Architecture -eq "arm64") {
$ArchitectureExtension = "-arm64"
}
return $ArchitectureExtension
}
[uri] GetSourceUri() {
<#
.SYNOPSIS
Get base Python URI and return complete URI for Python installation executable.
#>
$base = $this.GetBaseUri()
$versionName = $this.GetBaseVersion()
$nativeVersion = Convert-Version -version $this.Version
$architecture = $this.GetArchitectureExtension()
$extension = $this.GetPythonExtension()
$uri = "${base}/${versionName}/python-${nativeVersion}${architecture}${extension}"
return $uri
}
[string] Download() {
<#
.SYNOPSIS
Download Python installation executable into artifact location.
#>
$sourceUri = $this.GetSourceUri()
Write-Host "Sources URI: $sourceUri"
$sourcesLocation = Download-File -Uri $sourceUri -OutputFolder $this.WorkFolderLocation
Write-Debug "Done; Sources location: $sourcesLocation"
return $sourcesLocation
}
[void] CreateInstallationScript() {
<#
.SYNOPSIS
Create Python artifact installation script based on specified template.
#>
$sourceUri = $this.GetSourceUri()
$pythonExecName = [IO.path]::GetFileName($sourceUri.AbsoluteUri)
$installationTemplateLocation = Join-Path -Path $this.InstallationTemplatesLocation -ChildPath $this.InstallationTemplateName
$installationTemplateContent = Get-Content -Path $installationTemplateLocation -Raw
$installationScriptLocation = New-Item -Path $this.WorkFolderLocation -Name $this.InstallationScriptName -ItemType File
$variablesToReplace = @{
"{{__ARCHITECTURE__}}" = $this.Architecture;
"{{__VERSION__}}" = $this.Version;
"{{__PYTHON_EXEC_NAME__}}" = $pythonExecName;
"{{__PYTHON_EXTRA_PARAMS__}}" = "";
}
if ($this.WithPyDebug) {
$variablesToReplace["{{__PYTHON_EXTRA_PARAMS__}}"] " Include_debug=1 Include_symbols=1"
}
$variablesToReplace.keys | ForEach-Object { $installationTemplateContent = $installationTemplateContent.Replace($_, $variablesToReplace[$_]) }
$installationTemplateContent | Out-File -FilePath $installationScriptLocation
Write-Debug "Done; Installation script location: $installationScriptLocation)"
}
[void] ArchiveArtifact() {
$OutputPath = Join-Path $this.ArtifactFolderLocation $this.OutputArtifactName
Create-SevenZipArchive -SourceFolder $this.WorkFolderLocation -ArchivePath $OutputPath
}
[void] Build() {
<#
.SYNOPSIS
Generates Python artifact from downloaded Python installation executable.
#>
Write-Host "Download Python $($this.Version) [$($this.Architecture)] executable..."
$this.Download()
Write-Host "Create installation script..."
$this.CreateInstallationScript()
Write-Host "Archive artifact"
$this.ArchiveArtifact()
}
}