forked from kluctl/go-embed-python
/
main.go
202 lines (170 loc) · 4.57 KB
/
main.go
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"sync"
"github.com/an0nym05/go-embed-python/embed_util"
"github.com/an0nym05/go-embed-python/internal"
"github.com/gobwas/glob"
"github.com/klauspost/compress/zstd"
log "github.com/sirupsen/logrus"
)
// versions taken from https://github.com/indygreg/python-build-standalone/releases/
const (
pythonVersionBase = "3.10"
pythonVersionFull = "3.10.9"
pythonStandaloneVersion = "20230116"
)
var archMapping = map[string]string{
"amd64": "x86_64",
"386": "i686",
"arm64": "aarch64",
}
var removeLibs = []string{
"distutils",
"ensurepip",
"idlelib",
"lib2to3",
"multiprocessing",
"pydoc_data",
"site-packages",
"sqlite3",
"test",
"turtledemo",
"venv",
"bin", // not really a library, but erroneously installed by jsonpath_ng
}
var keepNixPatterns = []glob.Glob{
glob.MustCompile("bin/**"),
glob.MustCompile("lib/*.so*"),
glob.MustCompile("lib/*.dylib"),
glob.MustCompile("lib/python3.*/**"),
}
var keepWinPatterns = []glob.Glob{
glob.MustCompile("Lib/**"),
glob.MustCompile("DLLs/**"),
glob.MustCompile("*.dll"),
glob.MustCompile("*.exe"),
}
var downloadLock sync.Mutex
func main() {
targetPath := "./python/internal/data"
var wg sync.WaitGroup
type job struct {
os string
arch string
dist string
keepPatterns []glob.Glob
}
jobs := []job{
{"linux", "amd64", "unknown-linux-musl-lto-full", keepNixPatterns},
{"linux", "arm64", "unknown-linux-gnu-lto-full", keepNixPatterns},
{"darwin", "amd64", "apple-darwin-lto-full", keepNixPatterns},
{"darwin", "arm64", "apple-darwin-lto-full", keepNixPatterns},
{"windows", "amd64", "pc-windows-msvc-shared-pgo-full", keepWinPatterns},
}
for _, j := range jobs {
j := j
wg.Add(1)
go func() {
downloadAndCopy(j.os, j.arch, j.dist, j.keepPatterns, targetPath)
wg.Done()
}()
}
wg.Wait()
}
func downloadAndCopy(osName string, arch string, dist string, keepPatterns []glob.Glob, targetPath string) {
downloadPath := download(osName, arch, dist)
extractPath := downloadPath + ".extracted"
err := os.RemoveAll(extractPath)
if err != nil {
log.Panic(err)
}
extract(downloadPath, extractPath)
installPath := filepath.Join(extractPath, "python", "install")
var libPath string
if osName == "windows" {
libPath = filepath.Join(installPath, "Lib")
} else {
libPath = filepath.Join(installPath, "lib", fmt.Sprintf("python%s", pythonVersionBase))
}
for _, lib := range removeLibs {
_ = os.RemoveAll(filepath.Join(libPath, lib))
}
err = internal.CleanupPythonDir(installPath, keepPatterns)
if err != nil {
panic(err)
}
err = embed_util.CopyForEmbed(filepath.Join(targetPath, fmt.Sprintf("%s-%s", osName, arch)), installPath)
if err != nil {
panic(err)
}
err = embed_util.WriteEmbedGoFile(targetPath, osName, arch)
if err != nil {
panic(err)
}
}
func download(osName, arch, dist string) string {
downloadLock.Lock()
defer downloadLock.Unlock()
pythonArch, ok := archMapping[arch]
if !ok {
log.Errorf("arch %s not supported", arch)
os.Exit(1)
}
fname := fmt.Sprintf("cpython-%s+%s-%s-%s.tar.zst", pythonVersionFull, pythonStandaloneVersion, pythonArch, dist)
downloadPath := filepath.Join(os.TempDir(), "python-download", fname)
downloadUrl := fmt.Sprintf("https://github.com/indygreg/python-build-standalone/releases/download/%s/%s", pythonStandaloneVersion, fname)
if _, err := os.Stat(downloadPath); err == nil {
log.Infof("skipping download of %s", downloadUrl)
return downloadPath
}
err := os.MkdirAll(filepath.Dir(downloadPath), 0o755)
if err != nil {
log.Errorf("mkdirs failed: %v", err)
os.Exit(1)
}
log.Infof("downloading %s", downloadUrl)
r, err := http.Get(downloadUrl)
if err != nil {
log.Errorf("download failed: %v", err)
os.Exit(1)
}
if r.StatusCode == http.StatusNotFound {
log.Errorf("404 not found")
os.Exit(1)
}
defer r.Body.Close()
fileData, err := io.ReadAll(r.Body)
err = os.WriteFile(downloadPath, fileData, 0o640)
if err != nil {
log.Errorf("writing file failed: %v", err)
os.Remove(downloadPath)
os.Exit(1)
}
return downloadPath
}
func extract(archivePath string, targetPath string) string {
f, err := os.Open(archivePath)
if err != nil {
log.Errorf("opening file failed: %v", err)
os.Exit(1)
}
defer f.Close()
z, err := zstd.NewReader(f)
if err != nil {
log.Errorf("decompression failed: %v", err)
os.Exit(1)
}
defer z.Close()
log.Infof("decompressing %s", archivePath)
err = internal.ExtractTarStream(z, targetPath)
if err != nil {
log.Errorf("decompression failed: %v", err)
os.Exit(1)
}
return targetPath
}