Skip to content

Commit 7f748b3

Browse files
committed
refactor env
1 parent c953ff6 commit 7f748b3

File tree

11 files changed

+384
-317
lines changed

11 files changed

+384
-317
lines changed

cmd/internal/install/deps.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"os"
66
"os/exec"
77
"runtime"
8+
9+
"github.com/cpunion/go-python/internal/env"
810
)
911

1012
// Dependencies installs all required dependencies for the project
@@ -22,7 +24,7 @@ func Dependencies(projectPath string, goVersion, tinyPkgConfigVersion, pyVersion
2224
if err := installGo(projectPath, goVersion, verbose); err != nil {
2325
return err
2426
}
25-
SetEnv(projectPath)
27+
env.SetBuildEnv(projectPath)
2628

2729
// Install Go dependencies
2830
if err := installGoDeps(projectPath); err != nil {

cmd/internal/install/env.go

Lines changed: 0 additions & 97 deletions
This file was deleted.

cmd/internal/install/golang.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package install
22

33
import (
44
"fmt"
5-
"path/filepath"
65
"runtime"
6+
7+
"github.com/cpunion/go-python/internal/env"
78
)
89

910
const (
@@ -45,7 +46,7 @@ func getGoURL(version string) string {
4546

4647
// installGo downloads and installs Go in the project directory
4748
func installGo(projectPath, version string, verbose bool) error {
48-
goDir := filepath.Join(projectPath, DepsDir, GoDir)
49+
goDir := env.GetGoDir(projectPath)
4950
fmt.Printf("Installing Go %s in %s\n", version, goDir)
5051
// Get download URL
5152
url := getGoURL(version)

cmd/internal/install/mingw.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package install
22

33
import (
44
"fmt"
5+
6+
"github.com/cpunion/go-python/internal/env"
57
)
68

79
const (
@@ -10,7 +12,7 @@ const (
1012
)
1113

1214
func installMingw(projectPath string, verbose bool) error {
13-
root := GetMingwDir(projectPath)
15+
root := env.GetMingwDir(projectPath)
1416
fmt.Printf("Installing mingw in %v\n", root)
1517
return downloadAndExtract("mingw", mingwVersion, mingwURL, root, "", verbose)
1618
}

cmd/internal/install/python.go

Lines changed: 9 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"runtime"
1010
"strings"
1111

12-
"github.com/cpunion/go-python/cmd/internal/python"
12+
"github.com/cpunion/go-python/internal/env"
1313
)
1414

1515
const (
@@ -166,8 +166,8 @@ func generatePkgConfig(pythonPath, pkgConfigDir string) error {
166166
}
167167

168168
// Get Python version from the environment
169-
env := python.New(pythonPath)
170-
pythonBin, err := env.Python()
169+
pyEnv := env.NewPythonEnv(pythonPath)
170+
pythonBin, err := pyEnv.Python()
171171
if err != nil {
172172
return fmt.Errorf("failed to get Python executable: %v", err)
173173
}
@@ -232,8 +232,8 @@ Cflags: -I${includedir}
232232

233233
// updatePkgConfig updates the prefix in pkg-config files to use absolute path
234234
func updatePkgConfig(projectPath string) error {
235-
pythonPath := GetPythonRoot(projectPath)
236-
pkgConfigDir := GetPythonPkgConfigDir(projectPath)
235+
pythonPath := env.GetPythonRoot(projectPath)
236+
pkgConfigDir := env.GetPythonPkgConfigDir(projectPath)
237237

238238
if runtime.GOOS == "windows" {
239239
if err := generatePkgConfig(pythonPath, pkgConfigDir); err != nil {
@@ -326,59 +326,10 @@ func updatePkgConfig(projectPath string) error {
326326
return nil
327327
}
328328

329-
// writeEnvFile writes environment variables to .python/env.txt
330-
func writeEnvFile(projectPath string) error {
331-
pythonDir := GetPythonRoot(projectPath)
332-
absPath, err := filepath.Abs(pythonDir)
333-
if err != nil {
334-
return fmt.Errorf("failed to get absolute path: %v", err)
335-
}
336-
337-
// Get Python path using python executable
338-
env := python.New(absPath)
339-
pythonBin, err := env.Python()
340-
if err != nil {
341-
return fmt.Errorf("failed to get Python executable: %v", err)
342-
}
343-
344-
// Execute Python to get sys.path
345-
cmd := exec.Command(pythonBin, "-c", "import sys; print(':'.join(sys.path))")
346-
output, err := cmd.Output()
347-
if err != nil {
348-
return fmt.Errorf("failed to get Python path: %v", err)
349-
}
350-
351-
// Prepare environment variables
352-
envVars := []string{
353-
fmt.Sprintf("PKG_CONFIG_PATH=%s", filepath.Join(absPath, "lib", "pkgconfig")),
354-
fmt.Sprintf("PYTHONPATH=%s", strings.TrimSpace(string(output))),
355-
fmt.Sprintf("PYTHONHOME=%s", absPath),
356-
}
357-
358-
// Write to env.txt
359-
envFile := filepath.Join(pythonDir, "env.txt")
360-
if err := os.WriteFile(envFile, []byte(strings.Join(envVars, "\n")), 0644); err != nil {
361-
return fmt.Errorf("failed to write env file: %v", err)
362-
}
363-
364-
return nil
365-
}
366-
367-
// LoadEnvFile loads environment variables from .python/env.txt in the given directory
368-
func LoadEnvFile(dir string) ([]string, error) {
369-
envFile := filepath.Join(GetPythonRoot(dir), "env.txt")
370-
content, err := os.ReadFile(envFile)
371-
if err != nil {
372-
return nil, fmt.Errorf("failed to read env file: %v", err)
373-
}
374-
375-
return strings.Split(strings.TrimSpace(string(content)), "\n"), nil
376-
}
377-
378329
// installPythonEnv downloads and installs Python standalone build
379330
func installPythonEnv(projectPath string, version, buildDate string, freeThreaded, debug bool, verbose bool) error {
380331
fmt.Printf("Installing Python %s in %s\n", version, projectPath)
381-
pythonDir := GetPythonRoot(projectPath)
332+
pythonDir := env.GetPythonRoot(projectPath)
382333

383334
// Remove existing Python directory if it exists
384335
if err := os.RemoveAll(pythonDir); err != nil {
@@ -401,13 +352,13 @@ func installPythonEnv(projectPath string, version, buildDate string, freeThreade
401352
}
402353

403354
// Create Python environment
404-
env := python.New(pythonDir)
355+
pyEnv := env.NewPythonEnv(pythonDir)
405356

406357
if verbose {
407358
fmt.Println("Installing Python dependencies...")
408359
}
409360

410-
if err := env.RunPip("install", "--upgrade", "pip", "setuptools", "wheel"); err != nil {
361+
if err := pyEnv.RunPip("install", "--upgrade", "pip", "setuptools", "wheel"); err != nil {
411362
return fmt.Errorf("error upgrading pip, setuptools, whell")
412363
}
413364

@@ -416,7 +367,7 @@ func installPythonEnv(projectPath string, version, buildDate string, freeThreade
416367
}
417368

418369
// Write environment variables to env.txt
419-
if err := writeEnvFile(projectPath); err != nil {
370+
if err := env.WriteEnvFile(projectPath); err != nil {
420371
return fmt.Errorf("error writing environment file: %v", err)
421372
}
422373

0 commit comments

Comments
 (0)