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 9d451c9
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
47 changes: 47 additions & 0 deletions tools/sgpoetry/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package sgpoetry

import (
"context"
"go.einride.tech/sage/sg"
"go.einride.tech/sage/sgtool"
"go.einride.tech/sage/tools/sgpython3"
"os"
"os/exec"
"path/filepath"
)

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
}
28 changes: 28 additions & 0 deletions tools/sgpython3/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package sgpython3

import (
"context"
"fmt"
"go.einride.tech/sage/sg"
"go.einride.tech/sage/sgtool"
"os/exec"
)

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(ctx 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 9d451c9

Please sign in to comment.