FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

enable detecting Python from PATH on MacOS · losttech/WhichPython@0ef7ad9 · GitHub

Repository navigation

Commit 0ef7ad9

Browse files
committed
enable detecting Python from PATH on MacOS
1 parent ba4cb25 commit 0ef7ad9

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

‎src/PythonEnvironment.cs‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ static PythonEnvironment TryDetectEnvironmentFromInterpreter(string potentialInt
167167
if (dll != null && !File.Exists(dll))
168168
dll = null;
169169
}
170-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)){
170+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
171+
|| RuntimeInformation.IsOSPlatform(OSPlatform.OSX)){
171172
dll = TryLocateSo(potentialInterpreter);
172173
}
173174
return home != null || dll != null
@@ -178,23 +179,31 @@ static PythonEnvironment TryDetectEnvironmentFromInterpreter(string potentialInt
178179
}
179180

180181
static string TryLocateSo(string interpreterPath) {
181-
var getMemMapStartInfo = new ProcessStartInfo(interpreterPath, $"-c \"{GetMemMapScript}\""){
182+
string getLibPathsScript = RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
183+
? LinuxGetLibPathsScript
184+
: MacGetLibPathsScript;
185+
var getLibPathsStartInfo = new ProcessStartInfo(interpreterPath, $"-c \"{getLibPathsScript}\""){
182186
CreateNoWindow = true,
183187
RedirectStandardOutput = true,
184188
UseShellExecute = false,
185189
};
186190

187191
try{
188-
var getMemMap = Process.Start(getMemMapStartInfo);
189-
if (!getMemMap.WaitForExit(5000)){
190-
getMemMap.Kill();
192+
var getLibPaths = Process.Start(getLibPathsStartInfo);
193+
if (!getLibPaths.WaitForExit(5000)){
194+
getLibPaths.Kill();
191195
return null;
192196
}
193-
string stdout = getMemMap.StandardOutput.ReadToEnd();
197+
string stdout = getLibPaths.StandardOutput.ReadToEnd();
194198
string[] paths = stdout
195199
.Split(new []{Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
196200
.ToArray();
197-
if (paths.Length == 1) return paths[0];
201+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
202+
if (paths.Length == 1) return paths[0];
203+
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
204+
return paths.Select(libPath => Path.ChangeExtension(libPath, ".dylib"))
205+
.FirstOrDefault(File.Exists);
206+
}
198207
return null;
199208
} catch(PlatformNotSupportedException) {
200209
return null;
@@ -203,6 +212,7 @@ static string TryLocateSo(string interpreterPath) {
203212
}
204213
}
205214

206-
const string GetMemMapScript = @"from distutils import sysconfig; import os.path as op; v = sysconfig.get_config_vars(); fpaths = [op.join(v[pv], v['LDLIBRARY']) for pv in ('LIBDIR', 'LIBPL')]; print(list(filter(op.exists, fpaths))[0])";
215+
const string LinuxGetLibPathsScript = @"from distutils import sysconfig; import os.path as op; v = sysconfig.get_config_vars(); fpaths = [op.join(v[pv], v['LDLIBRARY']) for pv in ('LIBDIR', 'LIBPL')]; print(list(filter(op.exists, fpaths))[0])";
216+
const string MacGetLibPathsScript = @"from distutils import sysconfig; import os.path as op; v = sysconfig.get_config_vars(); fpaths = [op.join(v[pv], v['LIBRARY']) for pv in ('LIBDIR', 'LIBPL')]; print(list(filter(op.exists, fpaths))[0])";
207217
}
208218
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL