Skip to content

Commit f0619fe

Browse files
committed
some comments
1 parent 84cbf22 commit f0619fe

File tree

3 files changed

+19
-24
lines changed

3 files changed

+19
-24
lines changed

libs/python/detect.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,23 @@ package python
22

33
import (
44
"context"
5-
"runtime"
6-
"strings"
7-
8-
"github.com/databricks/cli/libs/process"
5+
"errors"
6+
"os/exec"
97
)
108

11-
var detectPythonBinaryName = "python3"
12-
139
func DetectExecutable(ctx context.Context) (string, error) {
1410
// TODO: add a shortcut if .python-version file is detected somewhere in
1511
// the parent directory tree.
1612
//
1713
// See https://github.com/pyenv/pyenv#understanding-python-version-selection
18-
detector := "which"
19-
if runtime.GOOS == "windows" {
20-
detector = "where.exe"
21-
}
22-
out, err := process.Background(ctx, []string{detector, detectPythonBinaryName})
14+
out, err := exec.LookPath("python3")
2315
// most of the OS'es have python3 in $PATH, but for those which don't,
2416
// we perform the latest version lookup
25-
if err != nil && !strings.Contains(err.Error(), "not found") {
17+
if err != nil && !errors.Is(err, exec.ErrNotFound) {
2618
return "", err
2719
}
2820
if out != "" {
29-
res := strings.Split(out, "\n")
30-
return strings.TrimSpace(res[0]), nil
21+
return out, nil
3122
}
3223
// otherwise, detect all interpreters and pick the least that satisfies
3324
// minimal version requirements
@@ -39,5 +30,5 @@ func DetectExecutable(ctx context.Context) (string, error) {
3930
if err != nil {
4031
return "", err
4132
}
42-
return interpreter.Binary, nil
33+
return interpreter.Path, nil
4334
}

libs/python/interpreters.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"io/fs"
78
"os"
89
"path/filepath"
910
"sort"
@@ -16,11 +17,11 @@ import (
1617

1718
type Interpreter struct {
1819
Version string
19-
Binary string
20+
Path string
2021
}
2122

2223
func (i Interpreter) String() string {
23-
return fmt.Sprintf("%s (%s)", i.Version, i.Binary)
24+
return fmt.Sprintf("%s (%s)", i.Version, i.Path)
2425
}
2526

2627
type allInterpreters []Interpreter
@@ -52,10 +53,14 @@ func DetectInterpreters(ctx context.Context) (allInterpreters, error) {
5253
seen := map[string]bool{}
5354
for _, prefix := range paths {
5455
entries, err := os.ReadDir(prefix)
55-
if os.IsNotExist(err) {
56+
if errors.Is(err, fs.ErrNotExist) {
5657
// some directories in $PATH may not exist
5758
continue
5859
}
60+
if errors.Is(err, fs.ErrPermission) {
61+
// some directories we cannot list
62+
continue
63+
}
5964
if err != nil {
6065
return nil, fmt.Errorf("listing %s: %w", prefix, err)
6166
}
@@ -92,12 +97,11 @@ func DetectInterpreters(ctx context.Context) (allInterpreters, error) {
9297
// and parsing the output.
9398
out, err := process.Background(ctx, []string{resolved, "--version"})
9499
if err != nil {
95-
// TODO: skip-and-log or return?
96100
log.Debugf(ctx, "failed to check version for %s: %s", resolved, err)
97101
continue
98102
}
99103

100-
words := strings.Split(out, " ")
104+
words := strings.Split(strings.TrimSpace(out), " ")
101105
// The Python distribution from the Windows Store is available in $PATH as `python.exe`
102106
// and `python3.exe`, even though it symlinks to a real file packaged with some versions of Windows:
103107
// /c/Program Files/WindowsApps/Microsoft.DesktopAppInstaller_.../AppInstallerPythonRedirector.exe.
@@ -126,7 +130,7 @@ func DetectInterpreters(ctx context.Context) (allInterpreters, error) {
126130
}
127131
found = append(found, Interpreter{
128132
Version: version,
129-
Binary: resolved,
133+
Path: resolved,
130134
})
131135
}
132136
}

libs/python/interpreters_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ func TestFilteringInterpreters(t *testing.T) {
3434
assert.Len(t, all, 3)
3535
assert.Equal(t, "v2.7.18", all[0].Version)
3636
assert.Equal(t, "v3.10.5", all[1].Version)
37-
assert.Equal(t, "testdata/other-binaries-filtered/python3.10", all[1].Binary)
37+
assert.Equal(t, "testdata/other-binaries-filtered/python3.10", all[1].Path)
3838
assert.Equal(t, "v3.11.4", all[2].Version)
39-
assert.Equal(t, "testdata/other-binaries-filtered/real-python3.11.4", all[2].Binary)
39+
assert.Equal(t, "testdata/other-binaries-filtered/real-python3.11.4", all[2].Path)
4040
}
4141

4242
func TestInterpretersAtLeastInvalidSemver(t *testing.T) {
@@ -59,7 +59,7 @@ func TestInterpretersAtLeast(t *testing.T) {
5959

6060
interpreter, err := all.AtLeast("3.10")
6161
assert.NoError(t, err)
62-
assert.Equal(t, "testdata/other-binaries-filtered/python3.10", interpreter.Binary)
62+
assert.Equal(t, "testdata/other-binaries-filtered/python3.10", interpreter.Path)
6363
}
6464

6565
func TestInterpretersAtLeastNotSatisfied(t *testing.T) {

0 commit comments

Comments
 (0)