diff --git a/pkg/ide/internal/serverutil/python.go b/pkg/ide/internal/serverutil/python.go new file mode 100644 index 0000000000..fed5e9f618 --- /dev/null +++ b/pkg/ide/internal/serverutil/python.go @@ -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 +} diff --git a/pkg/ide/internal/serverutil/python_test.go b/pkg/ide/internal/serverutil/python_test.go new file mode 100644 index 0000000000..f70ca4d2c9 --- /dev/null +++ b/pkg/ide/internal/serverutil/python_test.go @@ -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() +} diff --git a/pkg/ide/jupyter/jupyter.go b/pkg/ide/jupyter/jupyter.go index 0c721c039e..caa147a26c 100644 --- a/pkg/ide/jupyter/jupyter.go +++ b/pkg/ide/jupyter/jupyter.go @@ -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" ) @@ -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() @@ -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 -} diff --git a/pkg/ide/marimo/marimo.go b/pkg/ide/marimo/marimo.go index 87fadd5037..569039cf57 100644 --- a/pkg/ide/marimo/marimo.go +++ b/pkg/ide/marimo/marimo.go @@ -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" ) @@ -18,6 +19,8 @@ const ( const DefaultServerPort = 10800 +const marimoTool = "marimo" + var Options = ide.Options{ ForwardPortsOption: { Name: ForwardPortsOption, @@ -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() @@ -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 -}