Skip to content

Commit

Permalink
fix windows compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
cpunion committed Nov 20, 2024
1 parent 59991d1 commit f6afabb
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 68 deletions.
7 changes: 6 additions & 1 deletion cmd/internal/install/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,13 @@ func installPythonEnv(projectPath string, version, buildDate string, freeThreade
return fmt.Errorf("error updating pkg-config: %v", err)
}

Check warning on line 411 in cmd/internal/install/python.go

View check run for this annotation

Codecov / codecov/patch

cmd/internal/install/python.go#L409-L411

Added lines #L409 - L411 were not covered by tests

pythonHome := env.GetPythonRoot(projectPath)
pythonPath, err := pyEnv.GetPythonPath()
if err != nil {
return fmt.Errorf("failed to get Python path: %v", err)
}

Check warning on line 417 in cmd/internal/install/python.go

View check run for this annotation

Codecov / codecov/patch

cmd/internal/install/python.go#L413-L417

Added lines #L413 - L417 were not covered by tests
// Write environment variables to env.txt
if err := env.WriteEnvFile(projectPath); err != nil {
if err := env.WriteEnvFile(projectPath, pythonHome, pythonPath); err != nil {
return fmt.Errorf("error writing environment file: %v", err)
}

Check warning on line 421 in cmd/internal/install/python.go

View check run for this annotation

Codecov / codecov/patch

cmd/internal/install/python.go#L419-L421

Added lines #L419 - L421 were not covered by tests

Expand Down
14 changes: 0 additions & 14 deletions cmd/internal/install/python_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,6 @@ func TestGetCacheDir(t *testing.T) {
t.Errorf("getCacheDir() did not create cache directory")
}
})

t.Run("invalid home directory", func(t *testing.T) {
// Set HOME to a non-existent directory
if runtime.GOOS == "windows" {
os.Setenv("USERPROFILE", "/nonexistent/path")
} else {
os.Setenv("HOME", "/nonexistent/path")
}

_, err := getCacheDir()
if err == nil {
t.Error("getCacheDir() error = nil, want error for invalid home directory")
}
})
}

func TestUpdatePkgConfig(t *testing.T) {
Expand Down
18 changes: 13 additions & 5 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,25 @@ func From(from any) Object {
case int64:
return newObject(C.PyLong_FromLongLong(C.longlong(v)))
case int:
return newObject(C.PyLong_FromLong(C.long(v)))
if unsafe.Sizeof(v) == unsafe.Sizeof(int64(0)) {
return newObject(C.PyLong_FromLongLong(C.longlong(v)))
} else {
return newObject(C.PyLong_FromLong(C.long(v)))
}

Check warning on line 31 in convert.go

View check run for this annotation

Codecov / codecov/patch

convert.go#L30-L31

Added lines #L30 - L31 were not covered by tests
case uint8:
return newObject(C.PyLong_FromLong(C.long(v)))
return newObject(C.PyLong_FromUnsignedLong(C.ulong(v)))
case uint16:
return newObject(C.PyLong_FromLong(C.long(v)))
return newObject(C.PyLong_FromUnsignedLong(C.ulong(v)))
case uint32:
return newObject(C.PyLong_FromLong(C.long(v)))
return newObject(C.PyLong_FromUnsignedLong(C.ulong(v)))
case uint64:
return newObject(C.PyLong_FromUnsignedLongLong(C.ulonglong(v)))
case uint:
return newObject(C.PyLong_FromUnsignedLong(C.ulong(v)))
if unsafe.Sizeof(v) == unsafe.Sizeof(uint64(0)) {
return newObject(C.PyLong_FromUnsignedLongLong(C.ulonglong(v)))
} else {
return newObject(C.PyLong_FromUnsignedLong(C.ulong(v)))
}

Check warning on line 45 in convert.go

View check run for this annotation

Codecov / codecov/patch

convert.go#L44-L45

Added lines #L44 - L45 were not covered by tests
case float64:
return newObject(C.PyFloat_FromDouble(C.double(v)))
case float32:
Expand Down
20 changes: 2 additions & 18 deletions internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package env
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
Expand Down Expand Up @@ -112,26 +111,11 @@ func pathSeparator() string {
}

// WriteEnvFile writes environment variables to .python/env.txt
func WriteEnvFile(projectPath string) error {
pythonHome := GetPythonRoot(projectPath)
// Get Python path using python executable
env := NewPythonEnv(pythonHome)
pythonBin, err := env.Python()
if err != nil {
return fmt.Errorf("failed to get Python executable: %v", err)
}

// Execute Python to get sys.path
cmd := exec.Command(pythonBin, "-c", "import sys; print(':'.join(sys.path))")
output, err := cmd.Output()
if err != nil {
return fmt.Errorf("failed to get Python path: %v", err)
}

func WriteEnvFile(projectPath, pythonHome, pythonPath string) error {
// Prepare environment variables
envVars := []string{
fmt.Sprintf("PKG_CONFIG_PATH=%s", filepath.Join(pythonHome, "lib", "pkgconfig")),
fmt.Sprintf("PYTHONPATH=%s", strings.TrimSpace(string(output))),
fmt.Sprintf("PYTHONPATH=%s", strings.TrimSpace(pythonPath)),
fmt.Sprintf("PYTHONHOME=%s", pythonHome),
}

Expand Down
30 changes: 3 additions & 27 deletions internal/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,13 @@ func TestWriteEnvFile(t *testing.T) {
// Create mock Python executable
var pythonPath string
if runtime.GOOS == "windows" {
pythonPath = filepath.Join(binDir, "python.exe")
pythonScript := `@echo off
echo /mock/path1;/mock/path2
`
if err := os.WriteFile(pythonPath, []byte(pythonScript), 0644); err != nil {
t.Fatal(err)
}
pythonPath = "/mock/path1;/mock/path2"
} else {
pythonPath = filepath.Join(binDir, "python")
pythonScript := `#!/bin/sh
echo "/mock/path1:/mock/path2"
`
if err := os.WriteFile(pythonPath, []byte(pythonScript), 0755); err != nil {
t.Fatal(err)
}
pythonPath = "/mock/path1:/mock/path2"
}

// Test writing env file
if err := WriteEnvFile(projectDir); err != nil {
if err := WriteEnvFile(projectDir, pythonDir, pythonPath); err != nil {
t.Errorf("writeEnvFile() error = %v, want nil", err)
return
}
Expand Down Expand Up @@ -129,16 +117,4 @@ echo "/mock/path1:/mock/path2"
}
}
})

t.Run("missing python executable", func(t *testing.T) {
tmpDir := t.TempDir()
if err := os.MkdirAll(filepath.Join(tmpDir, ".deps/python"), 0755); err != nil {
t.Fatal(err)
}

err := WriteEnvFile(tmpDir)
if err == nil {
t.Error("writeEnvFile() error = nil, want error for missing python executable")
}
})
}
25 changes: 22 additions & 3 deletions internal/env/pyenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package env

import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
)

// PythonEnv represents a Python environment
Expand Down Expand Up @@ -51,18 +53,35 @@ func (e *PythonEnv) Python() (string, error) {

// RunPip executes pip with the given arguments
func (e *PythonEnv) RunPip(args ...string) error {
return e.RunPython(append([]string{"-m", "pip"}, args...)...)
return e.RunPythonWithOutput(nil, append([]string{"-m", "pip"}, args...)...)

Check warning on line 56 in internal/env/pyenv.go

View check run for this annotation

Codecov / codecov/patch

internal/env/pyenv.go#L55-L56

Added lines #L55 - L56 were not covered by tests
}

// RunPython executes python with the given arguments
func (e *PythonEnv) RunPython(args ...string) error {
func (e *PythonEnv) RunPython(args ...string) (string, error) {
var buf strings.Builder
err := e.RunPythonWithOutput(&buf, args...)
if err != nil {
return "", err
}
return strings.TrimSpace(buf.String()), nil

Check warning on line 66 in internal/env/pyenv.go

View check run for this annotation

Codecov / codecov/patch

internal/env/pyenv.go#L60-L66

Added lines #L60 - L66 were not covered by tests
}

func (e *PythonEnv) RunPythonWithOutput(writer io.Writer, args ...string) error {
pythonPath, err := e.Python()
if err != nil {
return err
}

Check warning on line 73 in internal/env/pyenv.go

View check run for this annotation

Codecov / codecov/patch

internal/env/pyenv.go#L69-L73

Added lines #L69 - L73 were not covered by tests

cmd := exec.Command(pythonPath, args...)
cmd.Stdout = os.Stdout
if writer != nil {
cmd.Stdout = io.MultiWriter(writer, os.Stdout)
} else {
cmd.Stdout = os.Stdout
}
cmd.Stderr = os.Stderr
return cmd.Run()

Check warning on line 82 in internal/env/pyenv.go

View check run for this annotation

Codecov / codecov/patch

internal/env/pyenv.go#L75-L82

Added lines #L75 - L82 were not covered by tests
}

func (e *PythonEnv) GetPythonPath() (string, error) {
return e.RunPython("-c", `import sys; print(':'.join(sys.path))`)

Check warning on line 86 in internal/env/pyenv.go

View check run for this annotation

Codecov / codecov/patch

internal/env/pyenv.go#L85-L86

Added lines #L85 - L86 were not covered by tests
}

0 comments on commit f6afabb

Please sign in to comment.