Skip to content

Commit

Permalink
Merge pull request #32 from netthier/split-phases
Browse files Browse the repository at this point in the history
Split Python executable generation into two phases to facilitate patching
  • Loading branch information
codablock authored Feb 6, 2024
2 parents 227d033 + a7e2f03 commit 320a663
Showing 1 changed file with 36 additions and 10 deletions.
46 changes: 36 additions & 10 deletions python/generate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import (
var (
pythonStandaloneVersion = flag.String("python-standalone-version", "", "specify the python-standalone version. Check https://github.com/indygreg/python-build-standalone/releases/ for available options.")
pythonVersion = flag.String("python-version", "", "specify the python version.")

pythonVersionBase string
preparePath = flag.String("prepare-path", filepath.Join(os.TempDir(), "python-download"), "specify the path where the python executables are downloaded and prepared. automatically creates a temporary directory if unset")
runPrepare = flag.Bool("prepare", true, "if set, python executables will be downloaded and prepared for packing at the configured path")
runPack = flag.Bool("pack", true, "if set, previously prepared python executables will be packed into their redistributable form")
pythonVersionBase string
)

var archMapping = map[string]string{
Expand Down Expand Up @@ -89,14 +91,19 @@ func main() {
j := j
wg.Add(1)
go func() {
downloadAndCopy(j.os, j.arch, j.dist, j.keepPatterns, targetPath)
if *runPrepare {
downloadAndPrepare(j.os, j.arch, j.dist, j.keepPatterns)
}
if *runPack {
packPrepared(j.os, j.arch, j.dist, targetPath)
}
wg.Done()
}()
}
wg.Wait()
}

func downloadAndCopy(osName string, arch string, dist string, keepPatterns []glob.Glob, targetPath string) {
func downloadAndPrepare(osName string, arch string, dist string, keepPatterns []glob.Glob) {
downloadPath := download(osName, arch, dist)

extractPath := downloadPath + ".extracted"
Expand Down Expand Up @@ -124,8 +131,12 @@ func downloadAndCopy(osName string, arch string, dist string, keepPatterns []glo
if err != nil {
panic(err)
}
}

err = embed_util.CopyForEmbed(filepath.Join(targetPath, fmt.Sprintf("%s-%s", osName, arch)), installPath)
func packPrepared(osName string, arch string, dist string, targetPath string) {
extractPath := generateDownloadPath(arch, dist) + ".extracted"
installPath := filepath.Join(extractPath, "python", "install")
err := embed_util.CopyForEmbed(filepath.Join(targetPath, fmt.Sprintf("%s-%s", osName, arch)), installPath)
if err != nil {
panic(err)
}
Expand All @@ -134,19 +145,34 @@ func downloadAndCopy(osName string, arch string, dist string, keepPatterns []glo
if err != nil {
panic(err)
}
}

func download(osName, arch, dist string) string {
downloadLock.Lock()
defer downloadLock.Unlock()
f, err := os.Create(filepath.Join(targetPath, "PYTHON_VERSION"))
if err != nil {
panic(err)
}
defer f.Close()
_, err = fmt.Fprintf(f, "PYTHON_VERSION=%q\nPYTHON_STANDALONE_VERSION=%q\n", *pythonVersion, *pythonStandaloneVersion)
if err != nil {
panic(err)
}
}

func generateDownloadPath(arch string, dist string) string {
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", *pythonVersion, *pythonStandaloneVersion, pythonArch, dist)
downloadPath := filepath.Join(os.TempDir(), "python-download", fname)
return filepath.Join(*preparePath, fname)
}

func download(osName string, arch string, dist string) string {
downloadLock.Lock()
defer downloadLock.Unlock()

downloadPath := generateDownloadPath(arch, dist)
fname := filepath.Base(downloadPath)
downloadUrl := fmt.Sprintf("https://github.com/indygreg/python-build-standalone/releases/download/%s/%s", *pythonStandaloneVersion, fname)

if _, err := os.Stat(downloadPath); err == nil {
Expand Down

0 comments on commit 320a663

Please sign in to comment.