@@ -7,11 +7,11 @@ namespace Bonsai.Scripting.Python
7
7
{
8
8
static class EnvironmentHelper
9
9
{
10
- public static string GetPythonDLL ( string pythonVersion )
10
+ public static string GetPythonDLL ( EnvironmentConfig config )
11
11
{
12
12
return RuntimeInformation . IsOSPlatform ( OSPlatform . Windows )
13
- ? $ "python{ pythonVersion . Replace ( "." , string . Empty ) } .dll"
14
- : $ "libpython{ pythonVersion } .so";
13
+ ? $ "python{ config . PythonVersion . Replace ( "." , string . Empty ) } .dll"
14
+ : $ "libpython{ config . PythonVersion } .so";
15
15
}
16
16
17
17
public static void SetRuntimePath ( string pythonHome )
@@ -24,7 +24,7 @@ public static void SetRuntimePath(string pythonHome)
24
24
}
25
25
}
26
26
27
- public static string GetVirtualEnvironmentPath ( string path )
27
+ public static string GetEnvironmentPath ( string path )
28
28
{
29
29
if ( string . IsNullOrEmpty ( path ) )
30
30
{
@@ -35,42 +35,36 @@ public static string GetVirtualEnvironmentPath(string path)
35
35
return Path . GetFullPath ( path ) ;
36
36
}
37
37
38
- public static string GetPythonHome ( string path , out string pythonVersion )
38
+ public static EnvironmentConfig GetEnvironmentConfig ( string path )
39
39
{
40
- var pythonHome = path ;
41
- pythonVersion = string . Empty ;
42
40
var configFileName = Path . Combine ( path , "pyvenv.cfg" ) ;
43
41
if ( File . Exists ( configFileName ) )
44
42
{
45
- using var configReader = new StreamReader ( File . OpenRead ( configFileName ) ) ;
46
- while ( ! configReader . EndOfStream )
43
+ return EnvironmentConfig . FromConfigFile ( configFileName ) ;
44
+ }
45
+ else
46
+ {
47
+ var pythonHome = path ;
48
+ var pythonVersion = string . Empty ;
49
+ const string DefaultPythonName = "python" ;
50
+ if ( ! RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
47
51
{
48
- var line = configReader . ReadLine ( ) ;
49
- static string GetConfigValue ( string line )
50
- {
51
- var parts = line . Split ( '=' ) ;
52
- return parts . Length > 1 ? parts [ 1 ] . Trim ( ) : string . Empty ;
53
- }
52
+ var baseDirectory = Directory . GetParent ( path ) . Parent ;
53
+ pythonHome = Path . Combine ( baseDirectory . FullName , "bin" ) ;
54
+ }
54
55
55
- if ( line . StartsWith ( "home" ) )
56
- {
57
- pythonHome = GetConfigValue ( line ) ;
58
- }
59
- else if ( line . StartsWith ( "version" ) )
60
- {
61
- pythonVersion = GetConfigValue ( line ) ;
62
- if ( ! string . IsNullOrEmpty ( pythonVersion ) )
63
- {
64
- pythonVersion = pythonVersion . Substring ( 0 , pythonVersion . LastIndexOf ( '.' ) ) ;
65
- }
66
- }
56
+ var pythonName = Path . GetFileName ( path ) ;
57
+ var pythonVersionIndex = pythonName . LastIndexOf ( DefaultPythonName , StringComparison . OrdinalIgnoreCase ) ;
58
+ if ( pythonVersionIndex >= 0 )
59
+ {
60
+ pythonVersion = pythonName . Substring ( pythonVersionIndex + DefaultPythonName . Length ) ;
67
61
}
68
- }
69
62
70
- return pythonHome ;
63
+ return new EnvironmentConfig ( pythonHome , pythonVersion ) ;
64
+ }
71
65
}
72
66
73
- public static string GetPythonPath ( string pythonHome , string pythonVersion , string path )
67
+ public static string GetPythonPath ( EnvironmentConfig config )
74
68
{
75
69
string sitePackages ;
76
70
var basePath = PythonEngine . PythonPath ;
@@ -79,28 +73,28 @@ public static string GetPythonPath(string pythonHome, string pythonVersion, stri
79
73
{
80
74
if ( string . IsNullOrEmpty ( basePath ) )
81
75
{
82
- var pythonZip = Path . Combine ( pythonHome , Path . ChangeExtension ( Runtime . PythonDLL , ".zip" ) ) ;
83
- var pythonDLLs = Path . Combine ( pythonHome , "DLLs" ) ;
84
- var pythonLib = Path . Combine ( pythonHome , "Lib" ) ;
76
+ var pythonZip = Path . Combine ( config . PythonHome , Path . ChangeExtension ( Runtime . PythonDLL , ".zip" ) ) ;
77
+ var pythonDLLs = Path . Combine ( config . PythonHome , "DLLs" ) ;
78
+ var pythonLib = Path . Combine ( config . PythonHome , "Lib" ) ;
85
79
basePath = string . Join ( Path . PathSeparator . ToString ( ) , pythonZip , pythonDLLs , pythonLib , baseDirectory ) ;
86
80
}
87
81
88
- sitePackages = Path . Combine ( path , "Lib" , "site-packages" ) ;
82
+ sitePackages = Path . Combine ( config . Path , "Lib" , "site-packages" ) ;
89
83
}
90
84
else
91
85
{
92
86
if ( string . IsNullOrEmpty ( basePath ) )
93
87
{
94
- var pythonBase = Path . GetDirectoryName ( pythonHome ) ;
95
- pythonBase = Path . Combine ( pythonBase , "lib" , $ "python{ pythonVersion } ") ;
88
+ var pythonBase = Path . GetDirectoryName ( config . PythonHome ) ;
89
+ pythonBase = Path . Combine ( pythonBase , "lib" , $ "python{ config . PythonVersion } ") ;
96
90
var pythonLibDynload = Path . Combine ( pythonBase , "lib-dynload" ) ;
97
91
basePath = string . Join ( Path . PathSeparator . ToString ( ) , pythonBase , pythonLibDynload , baseDirectory ) ;
98
92
}
99
93
100
- sitePackages = Path . Combine ( path , "lib" , $ "python{ pythonVersion } ", "site-packages" ) ;
94
+ sitePackages = Path . Combine ( config . Path , "lib" , $ "python{ config . PythonVersion } ", "site-packages" ) ;
101
95
}
102
96
103
- return $ "{ basePath } { Path . PathSeparator } { path } { Path . PathSeparator } { sitePackages } ";
97
+ return $ "{ basePath } { Path . PathSeparator } { config . Path } { Path . PathSeparator } { sitePackages } ";
104
98
}
105
99
}
106
100
}
0 commit comments