Skip to content

Commit

Permalink
anaconda buildpack for Python3 (#12)
Browse files Browse the repository at this point in the history
* anaconda buildpack for Python3

* Support Anaconda 2, for Python 2, changes syntax
  • Loading branch information
Ridai Govinda Pombo authored and johnewart committed Jul 23, 2019
1 parent edd5eb9 commit 795b0cc
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 0 deletions.
140 changes: 140 additions & 0 deletions buildpacks/anaconda.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package buildpacks

import (
"fmt"
"os"
"path/filepath"

. "github.com/yourbase/yb/plumbing"
. "github.com/yourbase/yb/types"
)

type AnacondaBuildTool struct {
BuildTool
version string
spec BuildToolSpec
pyCompatibleNum int
}

var ANACONDA_DIST_MIRROR = "https://repo.continuum.io/miniconda/Miniconda{{.PyNum}}-{{.Version}}-{{.OS}}-{{.Arch}}.{{.Extension}}"

func NewAnaconda2BuildTool(toolSpec BuildToolSpec) AnacondaBuildTool {
tool := AnacondaBuildTool{
version: toolSpec.Version,
spec: toolSpec,
pyCompatibleNum: 2,
}

return tool
}

func NewAnaconda3BuildTool(toolSpec BuildToolSpec) AnacondaBuildTool {
tool := AnacondaBuildTool{
version: toolSpec.Version,
spec: toolSpec,
pyCompatibleNum: 3,
}

return tool
}

func (bt AnacondaBuildTool) Version() string {
return bt.version
}

func (bt AnacondaBuildTool) InstallDir() string {
return filepath.Join(bt.spec.PackageCacheDir, "miniconda", fmt.Sprintf("miniconda-%s", bt.Version()))
}

func (bt AnacondaBuildTool) Install() error {
anacondaDir := bt.InstallDir()
setupDir := bt.spec.PackageDir

if _, err := os.Stat(anacondaDir); err == nil {
fmt.Printf("anaconda installed in %s\n", anacondaDir)
} else {
fmt.Printf("Installing anaconda\n")

downloadUrl := bt.DownloadUrl()

fmt.Printf("Downloading Miniconda from URL %s...\n", downloadUrl)
localFile, err := DownloadFileWithCache(downloadUrl)
if err != nil {
fmt.Printf("Unable to download: %v\n", err)
return err
}
for _, cmd := range []string{
fmt.Sprintf("chmod +x %s", localFile),
fmt.Sprintf("bash %s -b -p %s", localFile, anacondaDir),
} {
fmt.Printf("Running: '%v' ", cmd)
ExecToStdout(cmd, setupDir)
}

}

return nil
}

func (bt AnacondaBuildTool) DownloadUrl() string {
opsys := OS()
arch := Arch()
extension := "sh"
version := bt.Version()

if version == "" {
version = "latest"
}

if arch == "amd64" {
arch = "x64"
}

if opsys == "darwin" {
opsys = "MacOSX"
}

if opsys == "linux" {
opsys = "Linux"
}

if opsys == "windows" {
opsys = "Windows"
extension = "exe"
}

data := struct {
PyNum int
OS string
Arch string
Version string
Extension string
}{
bt.pyCompatibleNum,
opsys,
arch,
version,
extension,
}

url, _ := TemplateToString(ANACONDA_DIST_MIRROR, data)

return url
}

func (bt AnacondaBuildTool) Setup() error {
installDir := bt.InstallDir()
PrependToPath(filepath.Join(installDir, "bin"))
setupDir := bt.spec.PackageDir

for _, cmd := range []string{
fmt.Sprintf("conda config --set always_yes yes --set changeps1 no"),
fmt.Sprintf("conda update -q conda"),
} {
fmt.Printf("Running: '%v' ", cmd)
ExecToStdout(cmd, setupDir)
}

return nil

}
4 changes: 4 additions & 0 deletions buildpacks/buildpack_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func LoadBuildPacks(dependencies []string, pkgCacheDir string, pkgDir string) ([
fmt.Printf("Configuring build tool: %s\n", toolSpec)

switch buildpackName {
case "anaconda2":
bt = NewAnaconda2BuildTool(spec)
case "anaconda3":
bt = NewAnaconda3BuildTool(spec)
case "ant":
bt = NewAntBuildTool(spec)
case "r":
Expand Down

0 comments on commit 795b0cc

Please sign in to comment.