-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathEnvironmentHelper.cs
134 lines (120 loc) · 6 KB
/
EnvironmentHelper.cs
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
using System;
using System.IO;
using System.Runtime.InteropServices;
using Python.Runtime;
namespace Bonsai.Scripting.Python
{
static class EnvironmentHelper
{
public static string GetPythonDLL(EnvironmentConfig config)
{
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? $"python{config.PythonVersion.Replace(".", string.Empty)}.dll"
: $"libpython{config.PythonVersion}.so";
}
public static void SetRuntimePath(string pythonHome)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
var path = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process).TrimEnd(Path.PathSeparator);
path = string.IsNullOrEmpty(path) ? pythonHome : pythonHome + Path.PathSeparator + path;
Environment.SetEnvironmentVariable("PATH", path, EnvironmentVariableTarget.Process);
}
}
static string FindPythonHome()
{
var systemPath = Environment.GetEnvironmentVariable("PATH");
var searchPaths = systemPath.Split(Path.PathSeparator);
var isRunningOnWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
var pythonExecutableName = isRunningOnWindows ? "python.exe" : "python3";
var pythonHome = Array.Find(searchPaths, path => File.Exists(Path.Combine(path, pythonExecutableName)));
if (pythonHome != null && !isRunningOnWindows && MonoHelper.IsRunningOnMono)
{
var pythonExecutablePath = Path.Combine(pythonHome, pythonExecutableName);
pythonExecutablePath = MonoHelper.GetRealPath(pythonExecutablePath);
var baseDirectory = Directory.GetParent(pythonExecutablePath).Parent;
if (baseDirectory != null)
{
pythonHome = Path.Combine(baseDirectory.FullName, "lib", Path.GetFileName(pythonExecutablePath));
}
}
return pythonHome;
}
public static string GetEnvironmentPath(string path)
{
if (string.IsNullOrEmpty(path))
{
path = Environment.GetEnvironmentVariable("VIRTUAL_ENV", EnvironmentVariableTarget.Process);
if (path == null) return FindPythonHome();
}
return Path.GetFullPath(path);
}
public static EnvironmentConfig GetEnvironmentConfig(string path)
{
var configFileName = Path.Combine(path, "pyvenv.cfg");
if (File.Exists(configFileName))
{
return EnvironmentConfig.FromConfigFile(configFileName);
}
else
{
var pythonHome = path;
var pythonVersion = string.Empty;
const string DefaultPythonName = "python";
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
var baseDirectory = Directory.GetParent(path).Parent;
pythonHome = Path.Combine(baseDirectory.FullName, "bin");
}
var pythonName = Path.GetFileName(path);
var pythonVersionIndex = pythonName.LastIndexOf(DefaultPythonName, StringComparison.OrdinalIgnoreCase);
if (pythonVersionIndex >= 0)
{
pythonVersion = pythonName.Substring(pythonVersionIndex + DefaultPythonName.Length);
}
return new EnvironmentConfig(pythonHome, pythonVersion);
}
}
public static string GetPythonPath(EnvironmentConfig config)
{
string sitePackages;
var basePath = PythonEngine.PythonPath;
var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
if (string.IsNullOrEmpty(basePath))
{
var pythonZip = Path.Combine(config.PythonHome, Path.ChangeExtension(Runtime.PythonDLL, ".zip"));
var pythonDLLs = Path.Combine(config.PythonHome, "DLLs");
var pythonLib = Path.Combine(config.PythonHome, "Lib");
basePath = string.Join(Path.PathSeparator.ToString(), pythonZip, pythonDLLs, pythonLib, baseDirectory);
}
sitePackages = Path.Combine(config.Path, "Lib", "site-packages");
if (config.IncludeSystemSitePackages && config.Path != config.PythonHome)
{
var systemSitePackages = Path.Combine(config.PythonHome, "Lib", "site-packages");
sitePackages = $"{sitePackages}{Path.PathSeparator}{systemSitePackages}";
}
}
else
{
if (string.IsNullOrEmpty(basePath))
{
var pythonBase = Path.GetDirectoryName(config.PythonHome);
pythonBase = Path.Combine(pythonBase, "lib", $"python{config.PythonVersion}");
var pythonLibDynload = Path.Combine(pythonBase, "lib-dynload");
basePath = string.Join(Path.PathSeparator.ToString(), pythonBase, pythonLibDynload, baseDirectory);
}
sitePackages = Path.Combine(config.Path, "lib", $"python{config.PythonVersion}", "site-packages");
if (config.IncludeSystemSitePackages)
{
var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var localFolder = Directory.GetParent(localAppData).FullName;
var systemSitePackages = Path.Combine(localFolder, "lib", $"python{config.PythonVersion}", "site-packages");
sitePackages = $"{sitePackages}{Path.PathSeparator}{systemSitePackages}";
}
}
return $"{basePath}{Path.PathSeparator}{config.Path}{Path.PathSeparator}{sitePackages}";
}
}
}