Compare commits

..

No commits in common. "3bf36aaedd4a4f6eb166adc300a1f11ed6c65638" and "3c3c172622b22c7868cf1b82bb02caeb748e2875" have entirely different histories.

8 changed files with 93 additions and 141 deletions

View File

@ -16,6 +16,12 @@ type infer struct {
func (m *infer) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
artifact := b.Config.Artifacts[m.name]
// TODO use python.DetectVEnvExecutable once bundle has a way to specify venv path
py, err := python.DetectExecutable(ctx)
if err != nil {
return diag.FromErr(err)
}
// Note: using --build-number (build tag) flag does not help with re-installing
// libraries on all-purpose clusters. The reason is that `pip` ignoring build tag
// when upgrading the library and only look at wheel version.
@ -30,9 +36,7 @@ func (m *infer) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
// version=datetime.datetime.utcnow().strftime("%Y%m%d.%H%M%S"),
// ...
//)
py := python.GetExecutable()
artifact.BuildCommand = fmt.Sprintf(`%s setup.py bdist_wheel`, py)
artifact.BuildCommand = fmt.Sprintf(`"%s" setup.py bdist_wheel`, py)
return nil
}

View File

@ -541,7 +541,7 @@ func TestLoadDiagnosticsFile_nonExistent(t *testing.T) {
func TestInterpreterPath(t *testing.T) {
if runtime.GOOS == "windows" {
assert.Equal(t, "venv\\Scripts\\python.exe", interpreterPath("venv"))
assert.Equal(t, "venv\\Scripts\\python3.exe", interpreterPath("venv"))
} else {
assert.Equal(t, "venv/bin/python3", interpreterPath("venv"))
}
@ -673,7 +673,7 @@ func withFakeVEnv(t *testing.T, venvPath string) {
func interpreterPath(venvPath string) string {
if runtime.GOOS == "windows" {
return filepath.Join(venvPath, "Scripts", "python.exe")
return filepath.Join(venvPath, "Scripts", "python3.exe")
} else {
return filepath.Join(venvPath, "bin", "python3")
}

26
internal/testutil/cmd.go Normal file
View File

@ -0,0 +1,26 @@
package testutil
import (
"bytes"
"os"
"os/exec"
"github.com/stretchr/testify/require"
)
func RunCommand(t TestingT, name string, args ...string) {
cmd := exec.Command(name, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
require.NoError(t, cmd.Run())
}
func CaptureCommandOutput(t TestingT, name string, args ...string) string {
cmd := exec.Command(name, args...)
var stdout bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
require.NoError(t, err)
return stdout.String()
}

View File

@ -61,3 +61,25 @@ func Chdir(t TestingT, dir string) string {
return wd
}
func InsertPathEntry(t TestingT, path string) {
var separator string
if runtime.GOOS == "windows" {
separator = ";"
} else {
separator = ":"
}
t.Setenv("PATH", path+separator+os.Getenv("PATH"))
}
func InsertVirtualenvInPath(t TestingT, venvPath string) {
if runtime.GOOS == "windows" {
// https://github.com/pypa/virtualenv/commit/993ba1316a83b760370f5a3872b3f5ef4dd904c1
venvPath = filepath.Join(venvPath, "Scripts")
} else {
venvPath = filepath.Join(venvPath, "bin")
}
InsertPathEntry(t, venvPath)
}

View File

@ -11,19 +11,6 @@ import (
"runtime"
)
// GetExecutable gets appropriate python binary name for the platform
func GetExecutable() string {
// On Windows when virtualenv is created, the <env>/Scripts directory
// contains python.exe but no python3.exe.
// Most installers (e.g. the ones from python.org) only install python.exe and not python3.exe
if runtime.GOOS == "windows" {
return "python"
} else {
return "python3"
}
}
// DetectExecutable looks up the path to the python3 executable from the PATH
// environment variable.
//
@ -39,16 +26,31 @@ func DetectExecutable(ctx context.Context) (string, error) {
//
// See https://github.com/pyenv/pyenv#understanding-python-version-selection
out, err := exec.LookPath(GetExecutable())
// On Windows when virtualenv is created, the <env>/Scripts directory
// contains python.exe but no python3.exe. However, system python does have python3 entry
// and it is also added to PATH, so it is found first.
if runtime.GOOS == "windows" {
out, err := exec.LookPath("python.exe")
if err == nil && out != "" {
return out, nil
}
if err != nil && !errors.Is(err, exec.ErrNotFound) {
return "", err
}
}
out, err := exec.LookPath("python3")
// most of the OS'es have python3 in $PATH, but for those which don't,
// we perform the latest version lookup
if err != nil && !errors.Is(err, exec.ErrNotFound) {
return "", err
}
if out != "" {
return out, nil
}
// otherwise, detect all interpreters and pick the least that satisfies
// minimal version requirements
all, err := DetectInterpreters(ctx)
@ -69,7 +71,7 @@ func DetectExecutable(ctx context.Context) (string, error) {
func DetectVEnvExecutable(venvPath string) (string, error) {
interpreterPath := filepath.Join(venvPath, "bin", "python3")
if runtime.GOOS == "windows" {
interpreterPath = filepath.Join(venvPath, "Scripts", "python.exe")
interpreterPath = filepath.Join(venvPath, "Scripts", "python3.exe")
}
if _, err := os.Stat(interpreterPath); err != nil {

View File

@ -39,7 +39,7 @@ func TestDetectVEnvExecutable_badLayout(t *testing.T) {
func interpreterPath(venvPath string) string {
if runtime.GOOS == "windows" {
return filepath.Join(venvPath, "Scripts", "python.exe")
return filepath.Join(venvPath, "Scripts", "python3.exe")
} else {
return filepath.Join(venvPath, "bin", "python3")
}

View File

@ -2,106 +2,31 @@ package pythontest
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/databricks/cli/internal/testutil"
"github.com/databricks/cli/libs/python"
"github.com/stretchr/testify/require"
)
type VenvOpts struct {
// input
PythonVersion string
skipVersionCheck bool
func RequirePythonVENV(t testutil.TestingT, ctx context.Context, pythonVersion string, checkVersion bool) string {
tmpDir := t.TempDir()
testutil.Chdir(t, tmpDir)
// input/output
Dir string
Name string
venvName := testutil.RandomName("test-venv-")
testutil.RunCommand(t, "uv", "venv", venvName, "--python", pythonVersion, "--seed")
testutil.InsertVirtualenvInPath(t, filepath.Join(tmpDir, venvName))
// output:
// Absolute path to venv
EnvPath string
// Absolute path to venv/bin or venv/Scripts, depending on OS
BinPath string
// Absolute path to python binary
PythonExe string
}
func CreatePythonEnv(opts *VenvOpts) error {
if opts == nil || opts.PythonVersion == "" {
return errors.New("PythonVersion must be provided")
}
if opts.Name == "" {
opts.Name = testutil.RandomName("test-venv-")
}
cmd := exec.Command("uv", "venv", opts.Name, "--python", opts.PythonVersion, "--seed", "-q")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = opts.Dir
err := cmd.Run()
if err != nil {
return err
}
opts.EnvPath, err = filepath.Abs(filepath.Join(opts.Dir, opts.Name))
if err != nil {
return err
}
_, err = os.Stat(opts.EnvPath)
if err != nil {
return fmt.Errorf("cannot stat EnvPath %s: %s", opts.EnvPath, err)
}
if runtime.GOOS == "windows" {
// https://github.com/pypa/virtualenv/commit/993ba1316a83b760370f5a3872b3f5ef4dd904c1
opts.BinPath = filepath.Join(opts.EnvPath, "Scripts")
opts.PythonExe = filepath.Join(opts.BinPath, "python.exe")
} else {
opts.BinPath = filepath.Join(opts.EnvPath, "bin")
opts.PythonExe = filepath.Join(opts.BinPath, "python3")
}
_, err = os.Stat(opts.BinPath)
if err != nil {
return fmt.Errorf("cannot stat BinPath %s: %s", opts.BinPath, err)
}
_, err = os.Stat(opts.PythonExe)
if err != nil {
return fmt.Errorf("cannot stat PythonExe %s: %s", opts.PythonExe, err)
}
if !opts.skipVersionCheck {
cmd := exec.Command(opts.PythonExe, "--version")
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("Failed to run %s --version: %s", opts.PythonExe, err)
}
outString := string(out)
expectVersion := "Python " + opts.PythonVersion
if !strings.HasPrefix(outString, expectVersion) {
return fmt.Errorf("Unexpected output from %s --version: %v (expected %v)", opts.PythonExe, outString, expectVersion)
}
}
return nil
}
func RequireActivatedPythonEnv(t *testing.T, ctx context.Context, opts *VenvOpts) {
err := CreatePythonEnv(opts)
pythonExe, err := python.DetectExecutable(ctx)
require.NoError(t, err)
require.DirExists(t, opts.BinPath)
require.Contains(t, pythonExe, venvName)
newPath := fmt.Sprintf("%s%c%s", opts.BinPath, os.PathListSeparator, os.Getenv("PATH"))
t.Setenv("PATH", newPath)
if checkVersion {
actualVersion := testutil.CaptureCommandOutput(t, pythonExe, "--version")
expectVersion := "Python " + pythonVersion
require.True(t, strings.HasPrefix(actualVersion, expectVersion), "Running %s --version: Expected %v, got %v", pythonExe, expectVersion, actualVersion)
}
return tmpDir
}

View File

@ -2,42 +2,15 @@ package pythontest
import (
"context"
"os/exec"
"path/filepath"
"testing"
"github.com/databricks/cli/libs/python"
"github.com/stretchr/testify/require"
)
func TestVenvSuccess(t *testing.T) {
func TestVenv(t *testing.T) {
// Test at least two version to ensure we capture a case where venv version does not match system one
for _, pythonVersion := range []string{"3.11", "3.12"} {
t.Run(pythonVersion, func(t *testing.T) {
ctx := context.Background()
dir := t.TempDir()
opts := VenvOpts{
PythonVersion: pythonVersion,
Dir: dir,
}
RequireActivatedPythonEnv(t, ctx, &opts)
require.DirExists(t, opts.EnvPath)
require.DirExists(t, opts.BinPath)
require.FileExists(t, opts.PythonExe)
pythonExe, err := exec.LookPath(python.GetExecutable())
require.NoError(t, err)
require.Equal(t, filepath.Dir(pythonExe), filepath.Dir(opts.PythonExe))
require.FileExists(t, pythonExe)
RequirePythonVENV(t, ctx, pythonVersion, true)
})
}
}
func TestWrongVersion(t *testing.T) {
require.Error(t, CreatePythonEnv(&VenvOpts{PythonVersion: "4.0"}))
}
func TestMissingVersion(t *testing.T) {
require.Error(t, CreatePythonEnv(nil))
require.Error(t, CreatePythonEnv(&VenvOpts{}))
}