Skip to content

Commit

Permalink
feat: add sgpython3 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 18389d6
Show file tree
Hide file tree
Showing 2 changed files with 77 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/sgpython3"
)

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 := sgpython3.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+"poetry=="+version).Run(); err != nil {
return err
}
if _, err := sgtool.CreateSymlink(poetry); err != nil {
return err
}
return nil
}
29 changes: 29 additions & 0 deletions tools/sgpython3/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package sgpython3

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

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

const name = "python3"

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

func PrepareCommand(context.Context) error {
// Make sure python3 is available.
if binary, err := exec.LookPath("python3"); err == nil {
if _, err := sgtool.CreateSymlink(binary); err != nil {
return err
}
return nil
}
// TODO: If needed, check if `python` is Python 3.
return fmt.Errorf("python3 is not available in $PATH")
}

0 comments on commit 18389d6

Please sign in to comment.