47 lines
1.0 KiB
C#
47 lines
1.0 KiB
C#
using System.Diagnostics;
|
|
|
|
namespace Sdt.Core;
|
|
|
|
internal static class PythonResolver
|
|
{
|
|
public static string ResolveExecutable()
|
|
{
|
|
var candidates = OperatingSystem.IsWindows()
|
|
? new[] { "python", "py" }
|
|
: new[] { "python3", "python" };
|
|
|
|
foreach (var candidate in candidates)
|
|
{
|
|
if (CanRun(candidate))
|
|
return candidate;
|
|
}
|
|
|
|
return "python";
|
|
}
|
|
|
|
private static bool CanRun(string exe)
|
|
{
|
|
try
|
|
{
|
|
var psi = new ProcessStartInfo
|
|
{
|
|
FileName = exe,
|
|
RedirectStandardOutput = true,
|
|
RedirectStandardError = true,
|
|
UseShellExecute = false,
|
|
CreateNoWindow = true,
|
|
};
|
|
psi.ArgumentList.Add("--version");
|
|
|
|
using var p = new Process { StartInfo = psi };
|
|
p.Start();
|
|
p.WaitForExit(2000);
|
|
return p.ExitCode == 0;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|