Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python on path #258

Merged
merged 9 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
30 changes: 25 additions & 5 deletions internal/environment/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package environment
import (
"bytes"
"fmt"
"io/fs"
"os/exec"
"strings"

Expand Down Expand Up @@ -54,18 +55,34 @@ func NewPythonInspector(projectDir util.Path, pythonPath util.Path, log logging.
}
}

func (i *defaultPythonInspector) getPythonExecutable() string {
func (i *defaultPythonInspector) getPythonExecutable(exec util.PathLooker) (string, error) {
if i.pythonPath.Path() != "" {
// User-provided python executable
return i.pythonPath.Path()
exists, err := i.pythonPath.Exists()
if err != nil {
return "", err
}
if exists {
return i.pythonPath.Path(), nil
}
return "", fmt.Errorf(
"cannot find the specified Python executable %s: %w",
i.pythonPath, fs.ErrNotExist)
} else {
// Use whatever is on PATH
return "python3"
path, err := exec.LookPath("python3")
if err != nil {
return exec.LookPath("python")
}
return path, err
}
}

func (i *defaultPythonInspector) GetPythonVersion() (string, error) {
pythonExecutable := i.getPythonExecutable()
pythonExecutable, err := i.getPythonExecutable(util.NewPathLooker())
if err != nil {
return "", err
}
args := []string{
`-E`, // ignore python-specific environment variables
`-c`, // execute the next argument as python code
Expand All @@ -90,7 +107,10 @@ func (i *defaultPythonInspector) GetPythonRequirements() ([]byte, error) {
i.log.Info("Using Python packages", "source", requirementsFilename)
return requirementsFilename.ReadFile()
}
pythonExecutable := i.getPythonExecutable()
pythonExecutable, err := i.getPythonExecutable(util.NewPathLooker())
if err != nil {
return nil, err
}
source := fmt.Sprintf("'%s -m pip freeze'", pythonExecutable)
i.log.Info("Using Python packages", "source", source)
args := []string{"-m", "pip", "freeze"}
Expand Down
21 changes: 18 additions & 3 deletions internal/environment/python_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (s *PythonSuite) TestGetPythonVersionFromPATH() {
log := logging.New()
inspector := NewPythonInspector(util.Path{}, util.Path{}, log)
executor := NewMockPythonExecutor()
executor.On("runPythonCommand", "python3", mock.Anything).Return([]byte("3.10.4"), nil)
executor.On("runPythonCommand", mock.Anything, mock.Anything).Return([]byte("3.10.4"), nil)
inspector.executor = executor
version, err := inspector.GetPythonVersion()
s.Nil(err)
Expand All @@ -93,10 +93,13 @@ func (s *PythonSuite) TestGetPythonVersionFromPATH() {
}

func (s *PythonSuite) TestGetPythonVersionFromRealDefaultPython() {
// This test can only run if python3 is on the PATH.
// This test can only run if python3 or python is on the PATH.
_, err := exec.LookPath("python3")
if err != nil {
s.T().Skip("python3 isn't available on PATH")
_, err := exec.LookPath("python")
if err != nil {
s.T().Skip("This test requires python or python3 to be available on PATH")
}
}
log := logging.New()
inspector := NewPythonInspector(util.Path{}, util.Path{}, log)
Expand All @@ -105,6 +108,18 @@ func (s *PythonSuite) TestGetPythonVersionFromRealDefaultPython() {
s.True(strings.HasPrefix(version, "3."))
}

func (s *PythonSuite) TestGetPythonExecutableFallbackPython() {
log := logging.New()
inspector := NewPythonInspector(util.Path{}, util.Path{}, log)

exec := util.NewMockPathLooker()
exec.On("LookPath", "python3").Return("", os.ErrNotExist)
exec.On("LookPath", "python").Return("/usr/bin/python", nil)
executable, err := inspector.getPythonExecutable(exec)
s.NoError(err)
s.Equal("/usr/bin/python", executable)
}

func (s *PythonSuite) TestGetRequirementsFromFile() {
baseDir, err := util.Getwd(afero.NewMemMapFs())
s.Nil(err)
Expand Down
36 changes: 36 additions & 0 deletions internal/util/process.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package util

// Copyright (C) 2023 by Posit Software, PBC.

import (
"os/exec"

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

type PathLooker interface {
LookPath(name string) (string, error)
}

type defaultPathLooker struct{}

func NewPathLooker() PathLooker {
return &defaultPathLooker{}
}

func (p *defaultPathLooker) LookPath(name string) (string, error) {
return exec.LookPath(name)
}

type mockPathLooker struct {
mock.Mock
}

func NewMockPathLooker() *mockPathLooker {
return &mockPathLooker{}
}

func (m *mockPathLooker) LookPath(name string) (string, error) {
args := m.Called(name)
return args.String(0), args.Error(1)
}