Skip to content

Commit 989ce2f

Browse files
committed
fix windows compatible
1 parent 59991d1 commit 989ce2f

File tree

7 files changed

+54
-74
lines changed

7 files changed

+54
-74
lines changed

cmd/internal/install/deps.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ func Dependencies(projectPath string, goVersion, tinyPkgConfigVersion, pyVersion
3636
return err
3737
}
3838

39+
if runtime.GOOS == "windows" {
40+
pythonPath := env.GetPythonRoot(projectPath)
41+
pkgConfigDir := env.GetPythonPkgConfigDir(projectPath)
42+
if err := generatePkgConfig(pythonPath, pkgConfigDir); err != nil {
43+
return err
44+
}
45+
}
46+
3947
// Update pkg-config files
4048
if err := updatePkgConfig(projectPath); err != nil {
4149
return err

cmd/internal/install/python.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,6 @@ func updatePkgConfig(projectPath string) error {
279279
pythonPath := env.GetPythonRoot(projectPath)
280280
pkgConfigDir := env.GetPythonPkgConfigDir(projectPath)
281281

282-
if runtime.GOOS == "windows" {
283-
if err := generatePkgConfig(pythonPath, pkgConfigDir); err != nil {
284-
return err
285-
}
286-
}
287-
288282
entries, err := os.ReadDir(pkgConfigDir)
289283
if err != nil {
290284
return fmt.Errorf("failed to read pkgconfig directory: %v", err)
@@ -410,8 +404,13 @@ func installPythonEnv(projectPath string, version, buildDate string, freeThreade
410404
return fmt.Errorf("error updating pkg-config: %v", err)
411405
}
412406

407+
pythonHome := env.GetPythonRoot(projectPath)
408+
pythonPath, err := pyEnv.GetPythonPath()
409+
if err != nil {
410+
return fmt.Errorf("failed to get Python path: %v", err)
411+
}
413412
// Write environment variables to env.txt
414-
if err := env.WriteEnvFile(projectPath); err != nil {
413+
if err := env.WriteEnvFile(projectPath, pythonHome, pythonPath); err != nil {
415414
return fmt.Errorf("error writing environment file: %v", err)
416415
}
417416

cmd/internal/install/python_test.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -154,20 +154,6 @@ func TestGetCacheDir(t *testing.T) {
154154
t.Errorf("getCacheDir() did not create cache directory")
155155
}
156156
})
157-
158-
t.Run("invalid home directory", func(t *testing.T) {
159-
// Set HOME to a non-existent directory
160-
if runtime.GOOS == "windows" {
161-
os.Setenv("USERPROFILE", "/nonexistent/path")
162-
} else {
163-
os.Setenv("HOME", "/nonexistent/path")
164-
}
165-
166-
_, err := getCacheDir()
167-
if err == nil {
168-
t.Error("getCacheDir() error = nil, want error for invalid home directory")
169-
}
170-
})
171157
}
172158

173159
func TestUpdatePkgConfig(t *testing.T) {

convert.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,25 @@ func From(from any) Object {
2424
case int64:
2525
return newObject(C.PyLong_FromLongLong(C.longlong(v)))
2626
case int:
27-
return newObject(C.PyLong_FromLong(C.long(v)))
27+
if unsafe.Sizeof(v) == unsafe.Sizeof(int64(0)) {
28+
return newObject(C.PyLong_FromLongLong(C.longlong(v)))
29+
} else {
30+
return newObject(C.PyLong_FromLong(C.long(v)))
31+
}
2832
case uint8:
29-
return newObject(C.PyLong_FromLong(C.long(v)))
33+
return newObject(C.PyLong_FromUnsignedLong(C.ulong(v)))
3034
case uint16:
31-
return newObject(C.PyLong_FromLong(C.long(v)))
35+
return newObject(C.PyLong_FromUnsignedLong(C.ulong(v)))
3236
case uint32:
33-
return newObject(C.PyLong_FromLong(C.long(v)))
37+
return newObject(C.PyLong_FromUnsignedLong(C.ulong(v)))
3438
case uint64:
3539
return newObject(C.PyLong_FromUnsignedLongLong(C.ulonglong(v)))
3640
case uint:
37-
return newObject(C.PyLong_FromUnsignedLong(C.ulong(v)))
41+
if unsafe.Sizeof(v) == unsafe.Sizeof(uint64(0)) {
42+
return newObject(C.PyLong_FromUnsignedLongLong(C.ulonglong(v)))
43+
} else {
44+
return newObject(C.PyLong_FromUnsignedLong(C.ulong(v)))
45+
}
3846
case float64:
3947
return newObject(C.PyFloat_FromDouble(C.double(v)))
4048
case float32:

internal/env/env.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package env
33
import (
44
"fmt"
55
"os"
6-
"os/exec"
76
"path/filepath"
87
"runtime"
98
"strings"
@@ -112,26 +111,11 @@ func pathSeparator() string {
112111
}
113112

114113
// WriteEnvFile writes environment variables to .python/env.txt
115-
func WriteEnvFile(projectPath string) error {
116-
pythonHome := GetPythonRoot(projectPath)
117-
// Get Python path using python executable
118-
env := NewPythonEnv(pythonHome)
119-
pythonBin, err := env.Python()
120-
if err != nil {
121-
return fmt.Errorf("failed to get Python executable: %v", err)
122-
}
123-
124-
// Execute Python to get sys.path
125-
cmd := exec.Command(pythonBin, "-c", "import sys; print(':'.join(sys.path))")
126-
output, err := cmd.Output()
127-
if err != nil {
128-
return fmt.Errorf("failed to get Python path: %v", err)
129-
}
130-
114+
func WriteEnvFile(projectPath, pythonHome, pythonPath string) error {
131115
// Prepare environment variables
132116
envVars := []string{
133117
fmt.Sprintf("PKG_CONFIG_PATH=%s", filepath.Join(pythonHome, "lib", "pkgconfig")),
134-
fmt.Sprintf("PYTHONPATH=%s", strings.TrimSpace(string(output))),
118+
fmt.Sprintf("PYTHONPATH=%s", strings.TrimSpace(pythonPath)),
135119
fmt.Sprintf("PYTHONHOME=%s", pythonHome),
136120
}
137121

internal/env/env_test.go

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,13 @@ func TestWriteEnvFile(t *testing.T) {
7272
// Create mock Python executable
7373
var pythonPath string
7474
if runtime.GOOS == "windows" {
75-
pythonPath = filepath.Join(binDir, "python.exe")
76-
pythonScript := `@echo off
77-
echo /mock/path1;/mock/path2
78-
`
79-
if err := os.WriteFile(pythonPath, []byte(pythonScript), 0644); err != nil {
80-
t.Fatal(err)
81-
}
75+
pythonPath = "/mock/path1;/mock/path2"
8276
} else {
83-
pythonPath = filepath.Join(binDir, "python")
84-
pythonScript := `#!/bin/sh
85-
echo "/mock/path1:/mock/path2"
86-
`
87-
if err := os.WriteFile(pythonPath, []byte(pythonScript), 0755); err != nil {
88-
t.Fatal(err)
89-
}
77+
pythonPath = "/mock/path1:/mock/path2"
9078
}
9179

9280
// Test writing env file
93-
if err := WriteEnvFile(projectDir); err != nil {
81+
if err := WriteEnvFile(projectDir, pythonDir, pythonPath); err != nil {
9482
t.Errorf("writeEnvFile() error = %v, want nil", err)
9583
return
9684
}
@@ -129,16 +117,4 @@ echo "/mock/path1:/mock/path2"
129117
}
130118
}
131119
})
132-
133-
t.Run("missing python executable", func(t *testing.T) {
134-
tmpDir := t.TempDir()
135-
if err := os.MkdirAll(filepath.Join(tmpDir, ".deps/python"), 0755); err != nil {
136-
t.Fatal(err)
137-
}
138-
139-
err := WriteEnvFile(tmpDir)
140-
if err == nil {
141-
t.Error("writeEnvFile() error = nil, want error for missing python executable")
142-
}
143-
})
144120
}

internal/env/pyenv.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package env
22

33
import (
44
"fmt"
5+
"io"
56
"os"
67
"os/exec"
78
"path/filepath"
89
"regexp"
910
"runtime"
11+
"strings"
1012
)
1113

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

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

5759
// RunPython executes python with the given arguments
58-
func (e *PythonEnv) RunPython(args ...string) error {
60+
func (e *PythonEnv) RunPython(args ...string) (string, error) {
61+
var buf strings.Builder
62+
err := e.RunPythonWithOutput(&buf, args...)
63+
if err != nil {
64+
return "", err
65+
}
66+
return strings.TrimSpace(buf.String()), nil
67+
}
68+
69+
func (e *PythonEnv) RunPythonWithOutput(writer io.Writer, args ...string) error {
5970
pythonPath, err := e.Python()
6071
if err != nil {
6172
return err
6273
}
6374

6475
cmd := exec.Command(pythonPath, args...)
65-
cmd.Stdout = os.Stdout
76+
if writer != nil {
77+
cmd.Stdout = io.MultiWriter(writer, os.Stdout)
78+
} else {
79+
cmd.Stdout = os.Stdout
80+
}
6681
cmd.Stderr = os.Stderr
6782
return cmd.Run()
6883
}
84+
85+
func (e *PythonEnv) GetPythonPath() (string, error) {
86+
return e.RunPython("-c", `import sys; print(':'.join(sys.path))`)
87+
}

0 commit comments

Comments
 (0)