11package main
22
33import (
4+ "compress/gzip"
45 "flag"
56 "fmt"
67 "io"
@@ -78,44 +79,58 @@ func main() {
7879 os string
7980 arch string
8081 dist string
82+ ext string
8183 keepPatterns []glob.Glob
84+ strip bool
8285 }
8386
8487 jobs := []job {
85- {"linux" , "amd64" , "unknown-linux-gnu-pgo+lto-full" , keepNixPatterns },
86- {"linux" , "arm64" , "unknown-linux-gnu-pgo+lto-full" , keepNixPatterns },
87- {"darwin" , "amd64" , "apple-darwin-pgo+lto-full" , keepNixPatterns },
88- {"darwin" , "arm64" , "apple-darwin-pgo+lto-full" , keepNixPatterns },
89- {"windows" , "amd64" , "pc-windows-msvc-pgo-full" , keepWinPatterns },
88+ {"linux" , "amd64" , "unknown-linux-gnu-pgo+lto-full" , "tar.zst" , keepNixPatterns , true },
89+ {"linux" , "arm64" , "unknown-linux-gnu-pgo+lto-full" , "tar.zst" , keepNixPatterns , true },
90+ {"darwin" , "amd64" , "apple-darwin-pgo+lto-full" , "tar.zst" , keepNixPatterns , false },
91+ {"darwin" , "arm64" , "apple-darwin-pgo+lto-full" , "tar.zst" , keepNixPatterns , false },
92+ {"windows" , "amd64" , "pc-windows-msvc-pgo-full" , "tar.zst" , keepWinPatterns , false },
9093 }
9194 for _ , j := range jobs {
9295 j := j
9396 wg .Add (1 )
9497 go func () {
9598 if * runPrepare {
96- downloadAndPrepare (j .os , j .arch , j .dist , j .keepPatterns )
99+ downloadAndPrepare (j .os , j .arch , j .dist , j .ext , j . keepPatterns , j . strip )
97100 }
98101 if * runPack {
99- packPrepared (j .os , j .arch , j .dist , targetPath )
102+ packPrepared (j .os , j .arch , j .dist , j . ext , targetPath )
100103 }
101104 wg .Done ()
102105 }()
103106 }
104107 wg .Wait ()
105108}
106109
107- func downloadAndPrepare (osName string , arch string , dist string , keepPatterns []glob.Glob ) {
108- downloadPath := download (osName , arch , dist )
110+ func calcInstallPath (extractPath string , dist string ) string {
111+ installPath := filepath .Join (extractPath , "python" )
112+ if ! strings .Contains (dist , "install_only" ) {
113+ installPath = filepath .Join (installPath , "install" )
114+ }
115+ return installPath
116+ }
117+
118+ func downloadAndPrepare (osName string , arch string , dist string , ext string , keepPatterns []glob.Glob , strip bool ) {
119+ downloadPath := download (osName , arch , dist , ext )
109120
110121 extractPath := downloadPath + ".extracted"
111122 err := os .RemoveAll (extractPath )
112123 if err != nil {
113124 log .Panic (err )
114125 }
115126
116- extract (downloadPath , extractPath )
127+ _ , err = extract (downloadPath , extractPath , ext )
128+ if err != nil {
129+ log .Errorf ("extract failed: %v" , err )
130+ os .Exit (1 )
131+ }
117132
118- installPath := filepath . Join (extractPath , "python" , "install" )
133+ installPath := calcInstallPath (extractPath , dist )
119134
120135 var libPath string
121136 if osName == "windows" {
@@ -132,11 +147,18 @@ func downloadAndPrepare(osName string, arch string, dist string, keepPatterns []
132147 if err != nil {
133148 panic (err )
134149 }
150+
151+ if strip {
152+ err = internal .StripBinaries (installPath , arch )
153+ if err != nil {
154+ panic (err )
155+ }
156+ }
135157}
136158
137- func packPrepared (osName string , arch string , dist string , targetPath string ) {
138- extractPath := generateDownloadPath (arch , dist ) + ".extracted"
139- installPath := filepath . Join (extractPath , "python" , "install" )
159+ func packPrepared (osName string , arch string , dist string , ext string , targetPath string ) {
160+ extractPath := generateDownloadPath (arch , dist , ext ) + ".extracted"
161+ installPath := calcInstallPath (extractPath , dist )
140162 err := embed_util .CopyForEmbed (filepath .Join (targetPath , fmt .Sprintf ("%s-%s" , osName , arch )), installPath )
141163 if err != nil {
142164 panic (err )
@@ -158,21 +180,21 @@ func packPrepared(osName string, arch string, dist string, targetPath string) {
158180 }
159181}
160182
161- func generateDownloadPath (arch string , dist string ) string {
183+ func generateDownloadPath (arch string , dist string , ext string ) string {
162184 pythonArch , ok := archMapping [arch ]
163185 if ! ok {
164186 log .Errorf ("arch %s not supported" , arch )
165187 os .Exit (1 )
166188 }
167- fname := fmt .Sprintf ("cpython-%s+%s-%s-%s.tar.zst " , * pythonVersion , * pythonStandaloneVersion , pythonArch , dist )
189+ fname := fmt .Sprintf ("cpython-%s+%s-%s-%s.%s " , * pythonVersion , * pythonStandaloneVersion , pythonArch , dist , ext )
168190 return filepath .Join (* preparePath , fname )
169191}
170192
171- func download (osName string , arch string , dist string ) string {
193+ func download (osName string , arch string , dist string , ext string ) string {
172194 downloadLock .Lock ()
173195 defer downloadLock .Unlock ()
174196
175- downloadPath := generateDownloadPath (arch , dist )
197+ downloadPath := generateDownloadPath (arch , dist , ext )
176198 fname := filepath .Base (downloadPath )
177199 downloadUrl := fmt .Sprintf ("https://github.com/astral-sh/python-build-standalone/releases/download/%s/%s" , * pythonStandaloneVersion , fname )
178200
@@ -211,27 +233,38 @@ func download(osName string, arch string, dist string) string {
211233 return downloadPath
212234}
213235
214- func extract (archivePath string , targetPath string ) string {
236+ func extract (archivePath string , targetPath string , ext string ) ( string , error ) {
215237 f , err := os .Open (archivePath )
216238 if err != nil {
217- log .Errorf ("opening file failed: %v" , err )
218- os .Exit (1 )
239+ return "" , err
219240 }
220241 defer f .Close ()
221242
222- z , err := zstd .NewReader (f )
223- if err != nil {
224- log .Errorf ("decompression failed: %v" , err )
225- os .Exit (1 )
243+ var dc io.Reader
244+ if ext == "tar.gz" {
245+ gz , err := gzip .NewReader (f )
246+ if err != nil {
247+ return "" , err
248+ }
249+ defer gz .Close ()
250+ dc = gz
251+ } else if ext == "tar.zst" {
252+ z , err := zstd .NewReader (f )
253+ if err != nil {
254+ return "" , err
255+ }
256+ defer z .Close ()
257+ dc = z
258+ } else {
259+ return "" , fmt .Errorf ("unknown extension: %s" , ext )
226260 }
227- defer z .Close ()
228261
229262 log .Infof ("decompressing %s" , archivePath )
230- err = internal .ExtractTarStream (z , targetPath )
263+ err = internal .ExtractTarStream (dc , targetPath )
231264 if err != nil {
232265 log .Errorf ("decompression failed: %v" , err )
233266 os .Exit (1 )
234267 }
235268
236- return targetPath
269+ return targetPath , nil
237270}
0 commit comments