diff --git a/Frends.Python.ExecuteScript/CHANGELOG.md b/Frends.Python.ExecuteScript/CHANGELOG.md index c9a0f33..6e0f89f 100644 --- a/Frends.Python.ExecuteScript/CHANGELOG.md +++ b/Frends.Python.ExecuteScript/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## [1.1.0] - 2025-12-05 + +### Changed + +- Add more specific error message when python is not installed or not found in PATH. + + ## [1.0.0] - 2025-09-11 ### Changed diff --git a/Frends.Python.ExecuteScript/Frends.Python.ExecuteScript.Tests/UnitTests.cs b/Frends.Python.ExecuteScript/Frends.Python.ExecuteScript.Tests/UnitTests.cs index a953289..ba2e19d 100644 --- a/Frends.Python.ExecuteScript/Frends.Python.ExecuteScript.Tests/UnitTests.cs +++ b/Frends.Python.ExecuteScript/Frends.Python.ExecuteScript.Tests/UnitTests.cs @@ -152,4 +152,4 @@ public async Task ResultContainsStandardError() }; private static Options DefaultOptions() => new() { ThrowErrorOnFailure = true, ErrorMessageOnFailure = null }; -} \ No newline at end of file +} diff --git a/Frends.Python.ExecuteScript/Frends.Python.ExecuteScript/Frends.Python.ExecuteScript.cs b/Frends.Python.ExecuteScript/Frends.Python.ExecuteScript/Frends.Python.ExecuteScript.cs index 419f68b..42d4fbe 100644 --- a/Frends.Python.ExecuteScript/Frends.Python.ExecuteScript/Frends.Python.ExecuteScript.cs +++ b/Frends.Python.ExecuteScript/Frends.Python.ExecuteScript/Frends.Python.ExecuteScript.cs @@ -33,16 +33,16 @@ public static async Task ExecuteScript( var stdError = string.Empty; var stdOutput = string.Empty; using var process = new Process(); + try { - var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); + await ValidatePythonAvailability(cancellationToken); - var command = GenerateCommand(isWindows, input); - var argumentsPrefix = isWindows ? "/C" : "-c"; + var command = GenerateCommand(IsWindows(), input); var psi = new ProcessStartInfo { - FileName = isWindows ? "cmd" : "/bin/bash", - Arguments = $"{argumentsPrefix} {command}", + FileName = IsWindows() ? "cmd" : "/bin/bash", + Arguments = $"{GetArgumentsPrefix()} {command}", RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, @@ -104,6 +104,7 @@ private static string GenerateCommand(bool isWindows, Input input) }; string fullCommand; + if (input.IsPreparationNeeded) { fullCommand = isWindows @@ -117,4 +118,36 @@ private static string GenerateCommand(bool isWindows, Input input) return fullCommand; } + + private static async Task ValidatePythonAvailability(CancellationToken cancellationToken) + { + using var process = new Process(); + var psi = new ProcessStartInfo + { + FileName = IsWindows() ? "cmd" : "/bin/bash", + Arguments = $"{GetArgumentsPrefix()} python --version", + UseShellExecute = true, + CreateNoWindow = true, + }; + process.StartInfo = psi; + + try + { + process.Start(); + await process.WaitForExitAsync(cancellationToken); + } + finally + { + if (!process.HasExited) process.Kill(true); + } + + var exitCode = process.ExitCode; + + if (exitCode != 0) + throw new Exception($"Python is not installed or not added to PATH. Exit code: {exitCode}."); + } + + private static string GetArgumentsPrefix() => IsWindows() ? "/C" : "-c"; + + private static bool IsWindows() => RuntimeInformation.IsOSPlatform(OSPlatform.Windows); } \ No newline at end of file diff --git a/Frends.Python.ExecuteScript/Frends.Python.ExecuteScript/Frends.Python.ExecuteScript.csproj b/Frends.Python.ExecuteScript/Frends.Python.ExecuteScript/Frends.Python.ExecuteScript.csproj index 16a7ca5..9ed3349 100644 --- a/Frends.Python.ExecuteScript/Frends.Python.ExecuteScript/Frends.Python.ExecuteScript.csproj +++ b/Frends.Python.ExecuteScript/Frends.Python.ExecuteScript/Frends.Python.ExecuteScript.csproj @@ -3,7 +3,7 @@ net8.0 latest - 1.0.0 + 1.1.0 Frends Copyright (c) 2025 Frends EiPaaS Frends