Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions pkg/ide/internal/serverutil/python.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package serverutil

import (
"fmt"

"github.com/devsy-org/devsy/pkg/command"
"github.com/devsy-org/devsy/pkg/ide"
"github.com/devsy-org/devsy/pkg/log"
)

type PythonTool struct {
Name string
Package string
Executable string
}

// InstallPythonTool installs tool for userName unless its executable already exists.
func InstallPythonTool(userName string, tool PythonTool) error {
if command.ExistsForUser(tool.Executable, userName) {
return nil
}

runCommand, err := ide.PythonInstallCommand(userName, tool.Package)
if err != nil {
return err
}

log.Infof("installing %s", tool.Name)
out, err := SuOrSh(userName, runCommand, "").CombinedOutput()
if err != nil {
return fmt.Errorf(
"error installing %s: %w",
tool.Name,
command.WrapCommandError(out, err),
)
}

log.Info("installed " + tool.Name)
return nil
}
97 changes: 97 additions & 0 deletions pkg/ide/internal/serverutil/python_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package serverutil

import (
"os"
"path/filepath"
"runtime"
"testing"

"github.com/stretchr/testify/suite"
)

const (
missingPythonExecutable = "devsy-missing-python-tool"
fakeUvArgsFile = "args"
)

type PythonToolSuite struct {
suite.Suite
}

func TestPythonToolSuite(t *testing.T) {
suite.Run(t, new(PythonToolSuite))
}

func (s *PythonToolSuite) SetupTest() {
if runtime.GOOS == goosWindows {
s.T().Skip("uses sh")
}
}

func (s *PythonToolSuite) TestInstallSkippedWhenExecutableExists() {
dir := s.useFakeUv("exit 1")

err := InstallPythonTool("", PythonTool{
Name: "shell",
Package: "unused",
Executable: "sh",
})
s.NoError(err)
s.NoFileExists(filepath.Join(dir, fakeUvArgsFile))
}

func (s *PythonToolSuite) TestInstallRunsPackageInstallWithSelectedTool() {
dir := s.useFakeUv("exit 0")

err := InstallPythonTool("", PythonTool{
Name: "jupyter notebook",
Package: "notebook",
Executable: missingPythonExecutable,
})
s.Require().NoError(err)

args, err := os.ReadFile(filepath.Clean(filepath.Join(dir, fakeUvArgsFile)))
s.Require().NoError(err)
s.Equal("tool install notebook\n", string(args))
}

func (s *PythonToolSuite) TestInstallFailureWrapsOutputWithDisplayName() {
s.useFakeUv("echo fake-uv-failure; exit 3")

err := InstallPythonTool("", PythonTool{
Name: "marimo",
Package: "marimo",
Executable: missingPythonExecutable,
})
s.Require().Error(err)
s.Contains(err.Error(), "error installing marimo: ")
s.Contains(err.Error(), "fake-uv-failure")
}

// useFakeUv puts a uv stub first on PATH. The stub records its argv in
// fakeUvArgsFile next to itself and then runs body.
func (s *PythonToolSuite) useFakeUv(body string) string {
dir := s.T().TempDir()
script := "#!/bin/sh\necho \"$@\" >> \"$(dirname \"$0\")/" + fakeUvArgsFile + "\"\n" + body + "\n"
s.Require().NoError(writeExecutable(filepath.Join(dir, "uv"), script))
s.T().Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
return dir
}

// writeExecutable creates an owner-only executable file. Permissions are set
// on the open handle so the mode is 0700 without a path-based chmod.
func writeExecutable(path, content string) error {
f, err := os.OpenFile(filepath.Clean(path), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o600)
if err != nil {
return err
}
if _, err := f.WriteString(content); err != nil {
_ = f.Close()
return err
}
if err := f.Chmod(0o700); err != nil {
_ = f.Close()
return err
}
return f.Close()
}
38 changes: 6 additions & 32 deletions pkg/ide/jupyter/jupyter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/devsy-org/devsy/pkg/command"
"github.com/devsy-org/devsy/pkg/config"
"github.com/devsy-org/devsy/pkg/ide"
"github.com/devsy-org/devsy/pkg/ide/internal/serverutil"
"github.com/devsy-org/devsy/pkg/log"
)

Expand Down Expand Up @@ -44,7 +45,11 @@ func NewJupyterNotebookServer(
}

func (o *JupyterNotbookServer) Install() error {
if err := o.installNotebook(); err != nil {
if err := serverutil.InstallPythonTool(o.userName, serverutil.PythonTool{
Name: "jupyter notebook",
Package: "notebook",
Executable: "jupyter",
}); err != nil {
return err
}
return o.Start()
Expand All @@ -71,34 +76,3 @@ func (o *JupyterNotbookServer) Start() error {
return cmd, nil
})
}

func (o *JupyterNotbookServer) installNotebook() error {
if command.ExistsForUser("jupyter", o.userName) {
return nil
}

runCommand, err := ide.PythonInstallCommand(o.userName, "notebook")
if err != nil {
return err
}

args := []string{}
if o.userName != "" {
args = append(args, "su", o.userName, "-c", runCommand)
} else {
args = append(args, "sh", "-c", runCommand)
}

log.Infof("installing jupyter notebook")
//nolint:gosec // args are constructed from trusted inputs
out, err := exec.Command(args[0], args[1:]...).CombinedOutput()
if err != nil {
return fmt.Errorf(
"error installing jupyter notebook: %w",
command.WrapCommandError(out, err),
)
}

log.Info("installed jupyter notebook")
return nil
}
40 changes: 8 additions & 32 deletions pkg/ide/marimo/marimo.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/devsy-org/devsy/pkg/command"
"github.com/devsy-org/devsy/pkg/config"
"github.com/devsy-org/devsy/pkg/ide"
"github.com/devsy-org/devsy/pkg/ide/internal/serverutil"
"github.com/devsy-org/devsy/pkg/log"
)

Expand All @@ -18,6 +19,8 @@ const (

const DefaultServerPort = 10800

const marimoTool = "marimo"

var Options = ide.Options{
ForwardPortsOption: {
Name: ForwardPortsOption,
Expand Down Expand Up @@ -51,7 +54,11 @@ func NewMarimoServer(
}

func (o *MarimoServer) Install() error {
if err := o.installMarimo(); err != nil {
if err := serverutil.InstallPythonTool(o.userName, serverutil.PythonTool{
Name: marimoTool,
Package: marimoTool,
Executable: marimoTool,
}); err != nil {
return err
}
return o.Start()
Expand All @@ -77,34 +84,3 @@ func (o *MarimoServer) Start() error {
return cmd, nil
})
}

func (o *MarimoServer) installMarimo() error {
if command.ExistsForUser("marimo", o.userName) {
return nil
}

runCommand, err := ide.PythonInstallCommand(o.userName, "marimo")
if err != nil {
return err
}

args := []string{}
if o.userName != "" {
args = append(args, "su", o.userName, "-c", runCommand)
} else {
args = append(args, "sh", "-c", runCommand)
}

log.Infof("installing marimo")
//nolint:gosec // args are constructed from trusted inputs
out, err := exec.Command(args[0], args[1:]...).CombinedOutput()
if err != nil {
return fmt.Errorf(
"error installing marimo: %w",
command.WrapCommandError(out, err),
)
}

log.Info("installed marimo")
return nil
}
Loading