|
| 1 | +package python |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "sort" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/databricks/cli/libs/log" |
| 13 | + "github.com/databricks/cli/libs/process" |
| 14 | + "golang.org/x/mod/semver" |
| 15 | +) |
| 16 | + |
| 17 | +type Interpreter struct { |
| 18 | + Version string |
| 19 | + Binary string |
| 20 | +} |
| 21 | + |
| 22 | +func (i Interpreter) String() string { |
| 23 | + return fmt.Sprintf("%s (%s)", i.Version, i.Binary) |
| 24 | +} |
| 25 | + |
| 26 | +type allInterpreters []Interpreter |
| 27 | + |
| 28 | +func (a allInterpreters) Latest() Interpreter { |
| 29 | + return a[len(a)-1] |
| 30 | +} |
| 31 | + |
| 32 | +func (a allInterpreters) AtLeast(minimalVersion string) (*Interpreter, error) { |
| 33 | + canonicalMinimalVersion := semver.Canonical("v" + strings.TrimPrefix(minimalVersion, "v")) |
| 34 | + if canonicalMinimalVersion == "" { |
| 35 | + return nil, fmt.Errorf("invalid SemVer: %s", minimalVersion) |
| 36 | + } |
| 37 | + for _, interpreter := range a { |
| 38 | + cmp := semver.Compare(interpreter.Version, canonicalMinimalVersion) |
| 39 | + if cmp < 0 { |
| 40 | + continue |
| 41 | + } |
| 42 | + return &interpreter, nil |
| 43 | + } |
| 44 | + return nil, fmt.Errorf("cannot find Python greater or equal to %s", canonicalMinimalVersion) |
| 45 | +} |
| 46 | + |
| 47 | +var ErrNoPythonInterpreters = errors.New("no python3 interpreters found") |
| 48 | + |
| 49 | +func DetectInterpreters(ctx context.Context) (allInterpreters, error) { |
| 50 | + found := allInterpreters{} |
| 51 | + paths := strings.Split(os.Getenv("PATH"), string(os.PathListSeparator)) |
| 52 | + seen := map[string]bool{} |
| 53 | + for _, prefix := range paths { |
| 54 | + entries, err := os.ReadDir(prefix) |
| 55 | + if os.IsNotExist(err) { |
| 56 | + // some directories in $PATH may not exist |
| 57 | + continue |
| 58 | + } |
| 59 | + if err != nil { |
| 60 | + return nil, fmt.Errorf("listing %s: %w", prefix, err) |
| 61 | + } |
| 62 | + for _, v := range entries { |
| 63 | + if v.IsDir() { |
| 64 | + continue |
| 65 | + } |
| 66 | + if strings.Contains(v.Name(), "-") { |
| 67 | + // skip python3-config, python3.10-config, etc |
| 68 | + continue |
| 69 | + } |
| 70 | + |
| 71 | + // If Python3 is installed on Windows through GUI installer app that was |
| 72 | + // downloaded from https://python.org/downloads/windows, it may appear |
| 73 | + // in $PATH as `python`, even though it means Python 2.7 in all other |
| 74 | + // operating systems (macOS, Linux). |
| 75 | + // |
| 76 | + // See https://github.com/databrickslabs/ucx/issues/281 |
| 77 | + if !strings.HasPrefix(v.Name(), "python") { |
| 78 | + continue |
| 79 | + } |
| 80 | + bin := filepath.Join(prefix, v.Name()) |
| 81 | + resolved, err := filepath.EvalSymlinks(bin) |
| 82 | + if err != nil { |
| 83 | + log.Debugf(ctx, "cannot resolve symlink for %s: %s", bin, resolved) |
| 84 | + continue |
| 85 | + } |
| 86 | + if seen[resolved] { |
| 87 | + continue |
| 88 | + } |
| 89 | + seen[resolved] = true |
| 90 | + |
| 91 | + // probe the binary version by executing it, like `python --version` |
| 92 | + // and parsing the output. |
| 93 | + out, err := process.Background(ctx, []string{resolved, "--version"}) |
| 94 | + if err != nil { |
| 95 | + // TODO: skip-and-log or return? |
| 96 | + log.Debugf(ctx, "failed to check version for %s: %s", resolved, err) |
| 97 | + continue |
| 98 | + } |
| 99 | + |
| 100 | + words := strings.Split(out, " ") |
| 101 | + // The Python distribution from the Windows Store is available in $PATH as `python.exe` |
| 102 | + // and `python3.exe`, even though it symlinks to a real file packaged with some versions of Windows: |
| 103 | + // /c/Program Files/WindowsApps/Microsoft.DesktopAppInstaller_.../AppInstallerPythonRedirector.exe. |
| 104 | + // Executing the `python` command from this distribution opens the Windows Store, allowing users to |
| 105 | + // download and install Python. Once installed, it replaces the `python.exe` and `python3.exe`` stub |
| 106 | + // with the genuine Python executable. Additionally, once user installs from the main installer at |
| 107 | + // https://python.org/downloads/windows, it does not replace this stub. |
| 108 | + // |
| 109 | + // However, a drawback is that if this initial stub is run with any command line arguments, it quietly |
| 110 | + // fails to execute. According to https://github.com/databrickslabs/ucx/issues/281, it can be |
| 111 | + // detected by seeing just the "Python" output without any version info from the `python --version` |
| 112 | + // command execution. |
| 113 | + // |
| 114 | + // See https://github.com/pypa/packaging-problems/issues/379 |
| 115 | + // See https://bugs.python.org/issue41327 |
| 116 | + if len(words) < 2 { |
| 117 | + continue |
| 118 | + } |
| 119 | + if words[0] != "Python" { |
| 120 | + continue |
| 121 | + } |
| 122 | + lastWord := words[len(words)-1] |
| 123 | + version := semver.Canonical("v" + lastWord) |
| 124 | + if version == "" { |
| 125 | + continue |
| 126 | + } |
| 127 | + found = append(found, Interpreter{ |
| 128 | + Version: version, |
| 129 | + Binary: resolved, |
| 130 | + }) |
| 131 | + } |
| 132 | + } |
| 133 | + if len(found) == 0 { |
| 134 | + return nil, ErrNoPythonInterpreters |
| 135 | + } |
| 136 | + sort.Slice(found, func(i, j int) bool { |
| 137 | + a := found[i].Version |
| 138 | + b := found[j].Version |
| 139 | + cmp := semver.Compare(a, b) |
| 140 | + if cmp != 0 { |
| 141 | + return cmp < 0 |
| 142 | + } |
| 143 | + return a < b |
| 144 | + }) |
| 145 | + return found, nil |
| 146 | +} |
0 commit comments