-
Notifications
You must be signed in to change notification settings - Fork 774
Expand file tree
/
Copy pathPythonEnvironment.cs
More file actions
188 lines (149 loc) · 5.71 KB
/
PythonEnvironment.cs
File metadata and controls
188 lines (149 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using static System.FormattableString;
namespace Python.Runtime;
internal class PythonEnvironment
{
readonly static string PYDLL_ENV_VAR = "PYTHONNET_PYDLL";
readonly static string PYEXE_ENV_VAR = "PYTHONNET_PYEXE";
readonly static string PYNET_VENV_ENV_VAR = "PYTHONNET_VENV";
readonly static string VENV_ENV_VAR = "VIRTUAL_ENV";
public string? VenvPath { get; private set; }
public string? Home { get; private set; }
public Version? Version { get; private set; }
public string? ProgramName { get; set; }
public string? LibPython { get; set; }
public bool IsValid =>
!string.IsNullOrEmpty(ProgramName) && !string.IsNullOrEmpty(LibPython);
// TODO: Move the lib-guessing step to separate function, use together with
// PYTHONNET_PYEXE or a path lookup as last resort
// Initialize PythonEnvironment instance from environment variables.
//
// If PYTHONNET_PYEXE and PYTHONNET_PYDLL are set, these always have precedence.
// If PYTHONNET_VENV or VIRTUAL_ENV is set, we interpret the environment as a venv
// and set the ProgramName/LibPython accordingly. PYTHONNET_VENV takes precedence.
public static PythonEnvironment FromEnv()
{
var pydll = Environment.GetEnvironmentVariable(PYDLL_ENV_VAR);
var pydllSet = !string.IsNullOrEmpty(pydll);
var pyexe = Environment.GetEnvironmentVariable(PYEXE_ENV_VAR);
var pyexeSet = !string.IsNullOrEmpty(pyexe);
var pynetVenv = Environment.GetEnvironmentVariable(PYNET_VENV_ENV_VAR);
var pynetVenvSet = !string.IsNullOrEmpty(pynetVenv);
var venv = Environment.GetEnvironmentVariable(VENV_ENV_VAR);
var venvSet = !string.IsNullOrEmpty(venv);
PythonEnvironment? res = new();
if (pynetVenvSet)
res = FromVenv(pynetVenv) ?? res;
else if (venvSet)
res = FromVenv(venv) ?? res;
if (pyexeSet)
res.ProgramName = pyexe;
if (pydllSet)
res.LibPython = pydll;
return res;
}
public static PythonEnvironment? FromVenv(string path)
{
var env = new PythonEnvironment
{
VenvPath = path
};
string venvCfg = Path.Combine(path, "pyvenv.cfg");
if (!File.Exists(venvCfg))
return null;
var settings = TryParse(venvCfg);
if (!settings.ContainsKey("home"))
return null;
env.Home = settings["home"];
var pname = ProgramNameFromPath(path);
if (File.Exists(pname))
env.ProgramName = pname;
if (settings.TryGetValue("version", out string versionStr))
{
_ = Version.TryParse(versionStr, out Version versionObj);
env.Version = versionObj;
}
else if (settings.TryGetValue("version_info", out versionStr))
{
_ = Version.TryParse(versionStr, out Version versionObj);
env.Version = versionObj;
}
env.LibPython = FindLibPython(env.Home, env.Version);
return env;
}
private static Dictionary<string, string> TryParse(string venvCfg)
{
var settings = new Dictionary<string, string>();
string[] lines = File.ReadAllLines(venvCfg);
// The actually used format is really primitive: "<key> = <value>"
foreach (string line in lines)
{
var split = line.Split(new[] { '=' }, 2);
if (split.Length != 2)
continue;
settings[split[0].Trim()] = split[1].Trim();
}
return settings;
}
private static string? FindLibPython(string home, Version? maybeVersion)
{
// TODO: Check whether there is a .dll/.so/.dylib next to the executable
if (maybeVersion is Version version)
{
return FindLibPythonInHome(home, version);
}
return null;
}
private static string? FindLibPythonInHome(string home, Version version)
{
var libPythonName = GetDefaultDllName(version);
List<string> pathsToCheck = new();
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
var arch = RuntimeInformation.ProcessArchitecture;
if (arch == Architecture.X64 || arch == Architecture.Arm64)
{
// multilib systems
pathsToCheck.Add("../lib64");
}
pathsToCheck.Add("../lib");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
pathsToCheck.Add(".");
}
else
{
pathsToCheck.Add("../lib");
}
return pathsToCheck
.Select(path => Path.Combine(home, path, libPythonName))
.FirstOrDefault(File.Exists);
}
private static string ProgramNameFromPath(string path)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return Path.Combine(path, "Scripts", "python.exe");
}
else
{
return Path.Combine(path, "bin", "python");
}
}
internal static string GetDefaultDllName(Version version)
{
string prefix = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "" : "lib";
string suffix = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? Invariant($"{version.Major}{version.Minor}")
: Invariant($"{version.Major}.{version.Minor}");
string ext = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".dll"
: RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? ".dylib"
: ".so";
return prefix + "python" + suffix + ext;
}
}