|
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(string pythonVersion) |
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{pythonVersion.Replace(".", string.Empty)}.dll" |
| 14 | + : $"libpython{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 | + public static string GetVirtualEnvironmentPath(string path) |
| 28 | + { |
| 29 | + if (string.IsNullOrEmpty(path)) |
| 30 | + { |
| 31 | + path = Environment.GetEnvironmentVariable("VIRTUAL_ENV", EnvironmentVariableTarget.Process); |
| 32 | + if (path == null) return path; |
| 33 | + } |
| 34 | + |
| 35 | + return Path.GetFullPath(path); |
25 | 36 | }
|
26 | 37 |
|
27 |
| - public static string GetPythonHome(string path) |
| 38 | + public static string GetPythonHome(string path, out string pythonVersion) |
28 | 39 | {
|
| 40 | + var pythonHome = path; |
| 41 | + pythonVersion = string.Empty; |
29 | 42 | var configFileName = Path.Combine(path, "pyvenv.cfg");
|
30 | 43 | if (File.Exists(configFileName))
|
31 | 44 | {
|
32 | 45 | using var configReader = new StreamReader(File.OpenRead(configFileName));
|
33 | 46 | while (!configReader.EndOfStream)
|
34 | 47 | {
|
35 | 48 | var line = configReader.ReadLine();
|
36 |
| - if (line.StartsWith("home")) |
| 49 | + static string GetConfigValue(string line) |
37 | 50 | {
|
38 | 51 | var parts = line.Split('=');
|
39 |
| - return parts[parts.Length - 1].Trim(); |
| 52 | + return parts.Length > 1 ? parts[1].Trim() : string.Empty; |
| 53 | + } |
| 54 | + |
| 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 | + } |
40 | 66 | }
|
41 | 67 | }
|
42 | 68 | }
|
43 | 69 |
|
44 |
| - return path; |
| 70 | + return pythonHome; |
45 | 71 | }
|
46 | 72 |
|
47 |
| - public static string GetPythonPath(string pythonHome, string path) |
| 73 | + public static string GetPythonPath(string pythonHome, string pythonVersion, string path) |
48 | 74 | {
|
| 75 | + string sitePackages; |
49 | 76 | var basePath = PythonEngine.PythonPath;
|
50 |
| - if (string.IsNullOrEmpty(basePath)) |
| 77 | + var baseDirectory = AppDomain.CurrentDomain.BaseDirectory; |
| 78 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
51 | 79 | {
|
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; |
56 |
| - basePath = string.Join(Path.PathSeparator.ToString(), pythonZip, pythonDLLs, pythonLib, baseDirectory); |
| 80 | + if (string.IsNullOrEmpty(basePath)) |
| 81 | + { |
| 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"); |
| 85 | + basePath = string.Join(Path.PathSeparator.ToString(), pythonZip, pythonDLLs, pythonLib, baseDirectory); |
| 86 | + } |
| 87 | + |
| 88 | + sitePackages = Path.Combine(path, "Lib", "site-packages"); |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + if (string.IsNullOrEmpty(basePath)) |
| 93 | + { |
| 94 | + var pythonBase = Path.GetDirectoryName(pythonHome); |
| 95 | + pythonBase = Path.Combine(pythonBase, "lib", $"python{pythonVersion}"); |
| 96 | + var pythonLibDynload = Path.Combine(pythonBase, "lib-dynload"); |
| 97 | + basePath = string.Join(Path.PathSeparator.ToString(), pythonBase, pythonLibDynload, baseDirectory); |
| 98 | + } |
| 99 | + |
| 100 | + sitePackages = Path.Combine(path, "lib", $"python{pythonVersion}", "site-packages"); |
57 | 101 | }
|
58 | 102 |
|
59 |
| - var sitePackages = Path.Combine(path, "Lib", "site-packages"); |
60 | 103 | return $"{basePath}{Path.PathSeparator}{path}{Path.PathSeparator}{sitePackages}";
|
61 | 104 | }
|
62 | 105 | }
|
|
0 commit comments