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