-
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 sgpython and sgpoetry tools
For Python projects managed by Poetry.
- Loading branch information
Showing
2 changed files
with
122 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/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 | ||
} |
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,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 | ||
} |