99 "runtime"
1010 "strings"
1111
12- "github.com/cpunion/go-python/cmd/ internal/python "
12+ "github.com/cpunion/go-python/internal/env "
1313)
1414
1515const (
@@ -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
234234func 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
379330func 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