Skip to content

Commit

Permalink
Update PythonDataProvider.cs (#81)
Browse files Browse the repository at this point in the history
* Add a little more error checking/reporting when running Python
* Fix bug when Python path is overridden
  • Loading branch information
jas88 committed Feb 13, 2024
1 parent 58e2e70 commit 6342008
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void PythonScript_Timeout()
[Test]
public void PythonScript_OverrideExecutablePath_DodgyFileType()
{
var MyPythonScript = @"s = raw_input ('==>')";
const string MyPythonScript = "s = raw_input ('==>')";

var py = Path.Combine(TestContext.CurrentContext.WorkDirectory, "Myscript.py");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using Rdmp.Core.DataLoad;
using Rdmp.Core.DataLoad.Engine.DataProvider;
using Rdmp.Core.DataLoad.Engine.Job;
using Rdmp.Core.ReusableLibraryCode.Annotations;
using Rdmp.Core.ReusableLibraryCode.Checks;
using Rdmp.Core.ReusableLibraryCode.Progress;

Expand Down Expand Up @@ -58,6 +57,18 @@ 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
{
Expand Down Expand Up @@ -91,18 +102,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 @@ -146,6 +145,14 @@ public void Initialize(ILoadDirectory hicProjectDirectory, DiscoveredDatabase db

public ExitCodeType Fetch(IDataLoadJob job, GracefulCancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(FullPathToPythonScriptToRun))
{
job.OnNotify(this,
new NotifyEventArgs(ProgressEventType.Error,
"No Python script provided"));
return ExitCodeType.Error;
}

int exitCode;
try
{
Expand Down Expand Up @@ -291,15 +298,15 @@ 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 (!path.Contains("DUMMY",StringComparison.Ordinal))
yield return (minor,fullVersion.ToString()??"0.0.0", path.ToString()??"none");
if (File.Exists(path))
yield return (minor,fullVersion.ToString()??"0.0.0", path);
}
}

Expand Down

0 comments on commit 6342008

Please sign in to comment.