Skip to content

Commit 3963515

Browse files
committed
added FromInterpreterChecked and an app command for that
1 parent 85d198d commit 3963515

6 files changed

Lines changed: 99 additions & 9 deletions

File tree

CI/Build+Test.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,12 @@ steps:
5050
targetType: 'inline'
5151
script: |
5252
Write-Host "$(GetPython.pythonLocation)"
53+
$interpreter = if ($IsWindows) { "python.exe" } else { "python" }
54+
$interpreter = Join-Path "$(GetPython.pythonLocation)" $interpreter
55+
5356
Get-ChildItem -Recurse "$(GetPython.pythonLocation)"
5457
55-
$pyEnvs = (dotnet run --project app/WhichPython.App.csproj) | Out-String
58+
$pyEnvs = (dotnet run --project app/WhichPython.App.csproj interpreter "$interpreter") | Out-String
5659
if (-Not $pyEnvs.Contains("$(GetPython.pythonLocation)")) {
5760
Write-Host "$env:PATH"
5861
Write-Host "$pyEnvs"

app/InterpreterInfoCommand.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace LostTech.WhichPython {
2+
using System;
3+
using System.IO;
4+
using ManyConsole.CommandLineUtils;
5+
6+
class InterpreterInfoCommand : ConsoleCommand {
7+
public override int Run(string[] remainingArguments) {
8+
this.CheckRequiredArguments();
9+
if (remainingArguments.Length < 1)
10+
throw new ArgumentNullException("python-executable");
11+
12+
var interpreter = new FileInfo(remainingArguments[0]);
13+
var environment = PythonEnvironment.FromInterpreterChecked(interpreter);
14+
15+
Console.WriteLine("ver: " + environment.LanguageVersion);
16+
Console.WriteLine("exe: " + environment.InterpreterPath.FullName);
17+
Console.WriteLine("home: " + environment.Home?.FullName);
18+
Console.WriteLine("dll: " + environment.DynamicLibraryPath?.FullName);
19+
20+
return 0;
21+
}
22+
23+
public InterpreterInfoCommand() {
24+
this.IsCommand("interpreter");
25+
this.HasAdditionalArguments(1, "<python-executable>");
26+
}
27+
}
28+
}

app/ListCommand.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace LostTech.WhichPython {
2+
using System;
3+
using System.Linq;
4+
5+
using ManyConsole.CommandLineUtils;
6+
7+
class ListCommand : ConsoleCommand {
8+
public override int Run(string[] remainingArguments) {
9+
foreach (var environment in PythonEnvironment.EnumerateEnvironments()
10+
.Concat(CondaEnvironment.EnumerateCondaEnvironments())) {
11+
Console.WriteLine($"{environment.LanguageVersion?.ToString(2) ?? "??"}-{environment.Architecture?.ToString() ?? "???"} @ {environment.Home}");
12+
}
13+
14+
return 0;
15+
}
16+
17+
public ListCommand() {
18+
this.IsCommand("list");
19+
}
20+
}
21+
}

app/WhichPython.App.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@
1212
<ProjectReference Include="..\src\LostTech.WhichPython.csproj" />
1313
</ItemGroup>
1414

15+
<ItemGroup>
16+
<PackageReference Include="ManyConsole.CommandLineUtils" Version="1.0.3-alpha" />
17+
</ItemGroup>
18+
1519
</Project>

app/WhichPythonProgram.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
namespace WhichPython {
1+
namespace LostTech.WhichPython {
22
using System;
33
using System.Linq;
4-
using LostTech.WhichPython;
4+
using ManyConsole.CommandLineUtils;
55

66
static class WhichPythonProgram {
7-
static void Main() {
8-
foreach (var environment in PythonEnvironment.EnumerateEnvironments()
9-
.Concat(CondaEnvironment.EnumerateCondaEnvironments())) {
10-
Console.WriteLine($"{environment.LanguageVersion?.ToString(2) ?? "??"}-{environment.Architecture?.ToString() ?? "???"} @ {environment.Home}");
11-
}
7+
static int Main(string[] args) {
8+
return ConsoleCommandDispatcher.DispatchCommand(
9+
ConsoleCommandDispatcher.FindCommandsInSameAssemblyAs(typeof(WhichPythonProgram)),
10+
args, Console.Out);
1211
}
1312
}
1413
}

src/PythonEnvironment.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,40 @@ IEnumerable<PythonEnvironment> Impl() {
109109
}
110110
}
111111

112+
public static PythonEnvironment FromInterpreterChecked(FileInfo interpreter) {
113+
if (interpreter is null) throw new ArgumentNullException(nameof(interpreter));
114+
115+
if (!interpreter.Exists)
116+
throw new FileNotFoundException("Interpreter file not found", interpreter.FullName);
117+
118+
var version = TryGetVersion(interpreter);
119+
if (version is null)
120+
throw new NotSupportedException("Unable to get interpreter version");
121+
122+
DirectoryInfo? home = null;
123+
string? dllPath = null;
124+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
125+
home = new DirectoryInfo(interpreter.DirectoryName);
126+
dllPath = GetDll(home, version);
127+
if (dllPath != null && !File.Exists(dllPath))
128+
throw new NotSupportedException("Unable to find dynamic library");
129+
}
130+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
131+
|| RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
132+
dllPath = TryLocateSo(interpreter);
133+
if (dllPath != null) {
134+
home = TryGuessUnixHome(interpreter, version, dllPath: dllPath);
135+
} else {
136+
throw new NotSupportedException("Unable to find dynamic library");
137+
}
138+
}
139+
140+
return new PythonEnvironment(interpreter, home,
141+
dll: dllPath is null ? null : new FileInfo(dllPath),
142+
languageVersion: version,
143+
architecture: null);
144+
}
145+
112146
/// <summary>
113147
/// Enumerate Python environments, listed in the PATH environment variable.
114148
/// </summary>
@@ -260,7 +294,8 @@ internal static string GetDll(DirectoryInfo home, Version version)
260294
if (match.Success)
261295
return match.Groups["ver"].Value;
262296

263-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && interpreter.Name == "python")
297+
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && interpreter.Name == "python"
298+
|| RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && interpreter.Name == "python.exe")
264299
return TryRunScript(interpreter, PrintVersionScript);
265300

266301
return null;

0 commit comments

Comments
 (0)