Skip to content

Commit

Permalink
Update PythonDataProvider.cs
Browse files Browse the repository at this point in the history
Fix bug when Python path is overridden
  • Loading branch information
jas88 committed Nov 13, 2023
1 parent 8a593c0 commit 2e142c3
Showing 1 changed file with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,24 @@ public void Check(ICheckNotifier notifier)
return;
}

if (FullPathToPythonScriptToRun?.Contains(' ') == true && FullPathToPythonScriptToRun?.Contains('"') == false)
notifier.OnCheckPerformed(
new CheckEventArgs(
"FullPathToPythonScriptToRun contains spaces but is not wrapped by quotes which will likely fail when we assemble the python execute command",
CheckResult.Fail));

if (!File.Exists(FullPathToPythonScriptToRun?.Trim('\"', '\'')))
notifier.OnCheckPerformed(
new CheckEventArgs(
$"File {FullPathToPythonScriptToRun} does not exist (FullPathToPythonScriptToRun)",
CheckResult.Warning));

//make sure Python is installed
try
{
// If the override exe exists, trust it.
if (OverridePythonExecutablePath?.Exists == true) return;

var version = GetPythonVersion();

if (version?.StartsWith(GetExpectedPythonVersion(), StringComparison.Ordinal)==true)
Expand Down Expand Up @@ -91,18 +106,6 @@ public void Check(ICheckNotifier notifier)
? new CheckEventArgs("Python is not installed on the host", CheckResult.Fail, e)
: new CheckEventArgs(e.Message, CheckResult.Fail, e));
}

if (FullPathToPythonScriptToRun?.Contains(' ') ==true && FullPathToPythonScriptToRun?.Contains('"') ==false)
notifier.OnCheckPerformed(
new CheckEventArgs(
"FullPathToPythonScriptToRun contains spaces but is not wrapped by quotes which will likely fail when we assemble the python execute command",
CheckResult.Fail));

if (!File.Exists(FullPathToPythonScriptToRun?.Trim('\"', '\'')))
notifier.OnCheckPerformed(
new CheckEventArgs(
$"File {FullPathToPythonScriptToRun} does not exist (FullPathToPythonScriptToRun)",
CheckResult.Warning));
}

public string? GetPythonVersion()
Expand Down Expand Up @@ -167,7 +170,7 @@ private int ExecuteProcess(IDataLoadEventListener listener, string script, int m
{
var processStartInfo = new ProcessStartInfo
{
FileName = GetPythonCommand() ?? throw new Exception("No Python executable found"),
FileName = GetPythonCommand(),
Arguments = script,
RedirectStandardOutput = true,
RedirectStandardError = true,
Expand Down Expand Up @@ -291,12 +294,12 @@ public string GetFullPythonPath()
using var details = k.OpenSubKey(v);
if (details is null) continue;

var fullVersion = details?.GetValue("Version") ?? v;
var fullVersion = details.GetValue("Version") ?? v;

using var pathKey = details?.OpenSubKey("InstallPath");
using var pathKey = details.OpenSubKey("InstallPath");
if (pathKey is null) continue;

var path = pathKey.GetValue("ExecutablePath")?.ToString() ?? Path.Combine(pathKey?.GetValue(null)?.ToString() ?? "DUMMY","python.exe");
var path = pathKey.GetValue("ExecutablePath")?.ToString() ?? Path.Combine(pathKey.GetValue(null)?.ToString() ?? "DUMMY","python.exe");

if (File.Exists(path))
yield return (minor,fullVersion.ToString()??"0.0.0", path);
Expand Down

0 comments on commit 2e142c3

Please sign in to comment.