Skip to content

Commit

Permalink
feat: add sgpython and sgpoetry tools
Browse files Browse the repository at this point in the history
For Python projects managed by Poetry.
  • Loading branch information
odsod committed Jul 20, 2023
1 parent ffbcd0c commit b7967e4
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
48 changes: 48 additions & 0 deletions tools/sgpoetry/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package sgpoetry

import (
"context"
"os"
"os/exec"
"path/filepath"

"go.einride.tech/sage/sg"
"go.einride.tech/sage/sgtool"
"go.einride.tech/sage/tools/sgpython"
)

const (
name = "poetry"
version = "1.5.1"
)

func Command(ctx context.Context, args ...string) *exec.Cmd {
sg.Deps(ctx, PrepareCommand)
return sg.Command(ctx, sg.FromBinDir(name), args...)
}

func PrepareCommand(ctx context.Context) error {
toolDir := sg.FromToolsDir(name, version)
poetry := filepath.Join(toolDir, "bin", name)
if _, err := os.Stat(poetry); err == nil {
if _, err := sgtool.CreateSymlink(poetry); err != nil {
return err
}
return nil
}
// See: https://python-poetry.org/docs/#installing-manually
if err := sgpython.Command(ctx, "-m", "venv", toolDir).Run(); err != nil {
return err
}
pip := filepath.Join(toolDir, "bin", "pip")
if err := sg.Command(ctx, pip, "install", "-U", "pip", "setuptools").Run(); err != nil {
return err
}
if err := sg.Command(ctx, pip, "install", name+"=="+version).Run(); err != nil {
return err
}
if _, err := sgtool.CreateSymlink(poetry); err != nil {
return err
}
return nil
}
74 changes: 74 additions & 0 deletions tools/sgpython/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package sgpython

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

"go.einride.tech/sage/sg"
"go.einride.tech/sage/sgtool"
"go.einride.tech/sage/tools/sggit"
)

const (
name = "python"
version = "3.11.3"
pyenvVersion = "2.3.18"
)

func Command(ctx context.Context, args ...string) *exec.Cmd {
sg.Deps(ctx, PrepareCommand)
return sg.Command(ctx, sg.FromBinDir(name), args...)
}

func PrepareCommand(ctx context.Context) error {
toolDir := sg.FromToolsDir(name, version)
pyenvDir := filepath.Join(toolDir, "pyenv")
binDir := filepath.Join(pyenvDir, "versions", version, "bin")
pythonFromPyenv := filepath.Join(binDir, "python")
if _, err := os.Stat(pythonFromPyenv); err == nil {
if _, err := sgtool.CreateSymlink(pythonFromPyenv); err != nil {
return err
}
return nil
} else if systemPython3, err := exec.LookPath("python3"); err == nil {
// Special case: Avoid building from source if we already have Python 3 on the system.
symlink := filepath.Join(sg.FromBinDir(), name)
if _, err := os.Lstat(symlink); err == nil {
if err := os.Remove(symlink); err != nil {
return err
}
}
if err := os.Symlink(systemPython3, symlink); err != nil {
return err
}
return nil
}
if err := os.RemoveAll(pyenvDir); err != nil {
return err
}
if err := sggit.Command(
ctx,
"clone",
"--depth",
"1",
"--branch",
"v"+pyenvVersion,
"https://github.com/pyenv/pyenv.git",
pyenvDir,
).Run(); err != nil {
return err
}
cmd := sg.Command(ctx, "bin/pyenv", "install", version)
cmd.Dir = pyenvDir
cmd.Env = append(cmd.Env, fmt.Sprintf("PYENV_ROOT=%s", pyenvDir))
if err := cmd.Run(); err != nil {
return err
}
if _, err := sgtool.CreateSymlink(pythonFromPyenv); err != nil {
return err
}
return nil
}

0 comments on commit b7967e4

Please sign in to comment.