-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add sgpython3 and sgpoetry tools
For Python projects managed by Poetry.
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |