|
1 | 1 | using System; |
2 | 2 | using System.IO; |
3 | | -using System.Linq; |
| 3 | +using System.Runtime.InteropServices; |
4 | 4 | using Python.Runtime; |
5 | 5 |
|
6 | 6 | namespace Bonsai.Scripting.Python |
7 | 7 | { |
8 | 8 | static class EnvironmentHelper |
9 | 9 | { |
10 | | - public static string GetPythonDLL(string path) |
| 10 | + public static string GetPythonDLL(EnvironmentConfig config) |
11 | 11 | { |
12 | | - return Directory |
13 | | - .EnumerateFiles(path, searchPattern: "python3?*.*") |
14 | | - .Select(Path.GetFileNameWithoutExtension) |
15 | | - .Where(match => match.Length > "python3".Length) |
16 | | - .Select(match => match.Replace(".", string.Empty)) |
17 | | - .FirstOrDefault(); |
| 12 | + return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) |
| 13 | + ? $"python{config.PythonVersion.Replace(".", string.Empty)}.dll" |
| 14 | + : $"libpython{config.PythonVersion}.so"; |
18 | 15 | } |
19 | 16 |
|
20 | 17 | public static void SetRuntimePath(string pythonHome) |
21 | 18 | { |
22 | | - var path = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process).TrimEnd(Path.PathSeparator); |
23 | | - path = string.IsNullOrEmpty(path) ? pythonHome : pythonHome + Path.PathSeparator + path; |
24 | | - Environment.SetEnvironmentVariable("PATH", path, EnvironmentVariableTarget.Process); |
| 19 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 20 | + { |
| 21 | + var path = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process).TrimEnd(Path.PathSeparator); |
| 22 | + path = string.IsNullOrEmpty(path) ? pythonHome : pythonHome + Path.PathSeparator + path; |
| 23 | + Environment.SetEnvironmentVariable("PATH", path, EnvironmentVariableTarget.Process); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + static string FindPythonHome() |
| 28 | + { |
| 29 | + var systemPath = Environment.GetEnvironmentVariable("PATH"); |
| 30 | + var searchPaths = systemPath.Split(Path.PathSeparator); |
| 31 | + var isRunningOnWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); |
| 32 | + var pythonExecutableName = isRunningOnWindows ? "python.exe" : "python3"; |
| 33 | + |
| 34 | + var pythonHome = Array.Find(searchPaths, path => File.Exists(Path.Combine(path, pythonExecutableName))); |
| 35 | + if (pythonHome != null && !isRunningOnWindows && MonoHelper.IsRunningOnMono) |
| 36 | + { |
| 37 | + var pythonExecutablePath = Path.Combine(pythonHome, pythonExecutableName); |
| 38 | + pythonExecutablePath = MonoHelper.GetRealPath(pythonExecutablePath); |
| 39 | + var baseDirectory = Directory.GetParent(pythonExecutablePath).Parent; |
| 40 | + if (baseDirectory != null) |
| 41 | + { |
| 42 | + pythonHome = Path.Combine(baseDirectory.FullName, "lib", Path.GetFileName(pythonExecutablePath)); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + return pythonHome; |
| 47 | + } |
| 48 | + |
| 49 | + public static string GetEnvironmentPath(string path) |
| 50 | + { |
| 51 | + if (string.IsNullOrEmpty(path)) |
| 52 | + { |
| 53 | + path = Environment.GetEnvironmentVariable("VIRTUAL_ENV", EnvironmentVariableTarget.Process); |
| 54 | + path ??= FindPythonHome(); |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + path = Path.GetFullPath(path); |
| 59 | + } |
| 60 | + |
| 61 | + return PathHelper.TrimEndingDirectorySeparator(path); |
25 | 62 | } |
26 | 63 |
|
27 | | - public static string GetPythonHome(string path) |
| 64 | + public static EnvironmentConfig GetEnvironmentConfig(string path) |
28 | 65 | { |
29 | 66 | var configFileName = Path.Combine(path, "pyvenv.cfg"); |
30 | 67 | if (File.Exists(configFileName)) |
31 | 68 | { |
32 | | - using var configReader = new StreamReader(File.OpenRead(configFileName)); |
33 | | - while (!configReader.EndOfStream) |
| 69 | + return EnvironmentConfig.FromConfigFile(configFileName); |
| 70 | + } |
| 71 | + else |
| 72 | + { |
| 73 | + var pythonHome = path; |
| 74 | + var pythonVersion = string.Empty; |
| 75 | + const string DefaultPythonName = "python"; |
| 76 | + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
34 | 77 | { |
35 | | - var line = configReader.ReadLine(); |
36 | | - if (line.StartsWith("home")) |
37 | | - { |
38 | | - var parts = line.Split('='); |
39 | | - return parts[parts.Length - 1].Trim(); |
40 | | - } |
| 78 | + var baseDirectory = Directory.GetParent(path).Parent; |
| 79 | + pythonHome = Path.Combine(baseDirectory.FullName, "bin"); |
41 | 80 | } |
42 | | - } |
43 | 81 |
|
44 | | - return path; |
| 82 | + var pythonName = Path.GetFileName(path); |
| 83 | + var pythonVersionIndex = pythonName.LastIndexOf(DefaultPythonName, StringComparison.OrdinalIgnoreCase); |
| 84 | + if (pythonVersionIndex >= 0) |
| 85 | + { |
| 86 | + pythonVersion = pythonName.Substring(pythonVersionIndex + DefaultPythonName.Length); |
| 87 | + } |
| 88 | + |
| 89 | + return new EnvironmentConfig(pythonHome, pythonVersion); |
| 90 | + } |
45 | 91 | } |
46 | 92 |
|
47 | | - public static string GetPythonPath(string pythonHome, string path) |
| 93 | + public static string GetPythonPath(EnvironmentConfig config) |
48 | 94 | { |
49 | | - var basePath = PythonEngine.PythonPath; |
50 | | - if (string.IsNullOrEmpty(basePath)) |
| 95 | + string basePath; |
| 96 | + string sitePackages; |
| 97 | + var baseDirectory = AppDomain.CurrentDomain.BaseDirectory; |
| 98 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
51 | 99 | { |
52 | | - var pythonZip = Path.Combine(pythonHome, Path.ChangeExtension(Runtime.PythonDLL, ".zip")); |
53 | | - var pythonDLLs = Path.Combine(pythonHome, "DLLs"); |
54 | | - var pythonLib = Path.Combine(pythonHome, "Lib"); |
55 | | - var baseDirectory = AppDomain.CurrentDomain.BaseDirectory; |
| 100 | + var pythonZip = Path.Combine(config.PythonHome, Path.ChangeExtension(Runtime.PythonDLL, ".zip")); |
| 101 | + var pythonDLLs = Path.Combine(config.PythonHome, "DLLs"); |
| 102 | + var pythonLib = Path.Combine(config.PythonHome, "Lib"); |
56 | 103 | basePath = string.Join(Path.PathSeparator.ToString(), pythonZip, pythonDLLs, pythonLib, baseDirectory); |
| 104 | + |
| 105 | + sitePackages = Path.Combine(config.Path, "Lib", "site-packages"); |
| 106 | + if (config.IncludeSystemSitePackages && config.Path != config.PythonHome) |
| 107 | + { |
| 108 | + var systemSitePackages = Path.Combine(config.PythonHome, "Lib", "site-packages"); |
| 109 | + sitePackages = $"{sitePackages}{Path.PathSeparator}{systemSitePackages}"; |
| 110 | + } |
| 111 | + } |
| 112 | + else |
| 113 | + { |
| 114 | + var pythonBase = Path.GetDirectoryName(config.PythonHome); |
| 115 | + pythonBase = Path.Combine(pythonBase, "lib", $"python{config.PythonVersion}"); |
| 116 | + var pythonLibDynload = Path.Combine(pythonBase, "lib-dynload"); |
| 117 | + basePath = string.Join(Path.PathSeparator.ToString(), pythonBase, pythonLibDynload, baseDirectory); |
| 118 | + |
| 119 | + sitePackages = Path.Combine(config.Path, "lib", $"python{config.PythonVersion}", "site-packages"); |
| 120 | + if (config.IncludeSystemSitePackages) |
| 121 | + { |
| 122 | + var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); |
| 123 | + var localFolder = Directory.GetParent(localAppData).FullName; |
| 124 | + var systemSitePackages = Path.Combine(localFolder, "lib", $"python{config.PythonVersion}", "site-packages"); |
| 125 | + sitePackages = $"{sitePackages}{Path.PathSeparator}{systemSitePackages}"; |
| 126 | + } |
57 | 127 | } |
58 | 128 |
|
59 | | - var sitePackages = Path.Combine(path, "Lib", "site-packages"); |
60 | | - return $"{basePath}{Path.PathSeparator}{path}{Path.PathSeparator}{sitePackages}"; |
| 129 | + return $"{basePath}{Path.PathSeparator}{config.Path}{Path.PathSeparator}{sitePackages}"; |
61 | 130 | } |
62 | 131 | } |
63 | 132 | } |
0 commit comments