From 99223ded985549781361da2e11ff50c2159e9e4f Mon Sep 17 00:00:00 2001 From: MatteoDelOmbra Date: Fri, 5 Dec 2025 15:28:51 +0100 Subject: [PATCH 1/3] add missing python error message --- Frends.Python.ExecuteScript/CHANGELOG.md | 7 ++++ .../UnitTests.cs | 2 +- .../Frends.Python.ExecuteScript.cs | 35 +++++++++++++++---- .../Frends.Python.ExecuteScript.csproj | 2 +- 4 files changed, 38 insertions(+), 8 deletions(-) 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..ecbbcc6 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(process, 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, @@ -117,4 +117,27 @@ private static string GenerateCommand(bool isWindows, Input input) return fullCommand; } -} \ No newline at end of file + + private static async Task ValidatePythonAvailability(Process process, CancellationToken cancellationToken) + { + var psi = new ProcessStartInfo + { + FileName = IsWindows() ? "cmd" : "/bin/bash", + Arguments = $"{GetArgumentsPrefix()} python --version", + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true, + }; + process.StartInfo = psi; + process.Start(); + + await process.WaitForExitAsync(cancellationToken); + var exitCode = process.ExitCode; + if (exitCode != 0) throw new Exception($"Python is not installed nor added to PATH. Exit code: {exitCode}."); + } + + private static string GetArgumentsPrefix() => IsWindows() ? "/C" : "-c"; + + private static bool IsWindows() => RuntimeInformation.IsOSPlatform(OSPlatform.Windows); +} 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 From be4d7315e329d03cacfa2c629dd20e059107a914 Mon Sep 17 00:00:00 2001 From: MatteoDelOmbra Date: Fri, 5 Dec 2025 15:41:43 +0100 Subject: [PATCH 2/3] fix process run --- .../Frends.Python.ExecuteScript.cs | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) 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 ecbbcc6..feedac2 100644 --- a/Frends.Python.ExecuteScript/Frends.Python.ExecuteScript/Frends.Python.ExecuteScript.cs +++ b/Frends.Python.ExecuteScript/Frends.Python.ExecuteScript/Frends.Python.ExecuteScript.cs @@ -36,7 +36,7 @@ public static async Task ExecuteScript( try { - await ValidatePythonAvailability(process, cancellationToken); + await ValidatePythonAvailability(cancellationToken); var command = GenerateCommand(IsWindows(), input); var psi = new ProcessStartInfo @@ -118,8 +118,9 @@ private static string GenerateCommand(bool isWindows, Input input) return fullCommand; } - private static async Task ValidatePythonAvailability(Process process, CancellationToken cancellationToken) + private static async Task ValidatePythonAvailability(CancellationToken cancellationToken) { + using var process = new Process(); var psi = new ProcessStartInfo { FileName = IsWindows() ? "cmd" : "/bin/bash", @@ -130,11 +131,20 @@ private static async Task ValidatePythonAvailability(Process process, Cancellati CreateNoWindow = true, }; process.StartInfo = psi; - process.Start(); - await process.WaitForExitAsync(cancellationToken); + 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 nor added to PATH. Exit code: {exitCode}."); + if (exitCode != 0) + throw new Exception($"Python is not installed nor added to PATH. Exit code: {exitCode}."); } private static string GetArgumentsPrefix() => IsWindows() ? "/C" : "-c"; From 48538c03e9afcd319630525b2bf4008de100effd Mon Sep 17 00:00:00 2001 From: Mateusz Noga-Wojtania Date: Thu, 11 Dec 2025 14:01:11 +0100 Subject: [PATCH 3/3] cr changes --- .../Frends.Python.ExecuteScript.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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 feedac2..42d4fbe 100644 --- a/Frends.Python.ExecuteScript/Frends.Python.ExecuteScript/Frends.Python.ExecuteScript.cs +++ b/Frends.Python.ExecuteScript/Frends.Python.ExecuteScript/Frends.Python.ExecuteScript.cs @@ -104,6 +104,7 @@ private static string GenerateCommand(bool isWindows, Input input) }; string fullCommand; + if (input.IsPreparationNeeded) { fullCommand = isWindows @@ -125,9 +126,7 @@ private static async Task ValidatePythonAvailability(CancellationToken cancellat { FileName = IsWindows() ? "cmd" : "/bin/bash", Arguments = $"{GetArgumentsPrefix()} python --version", - RedirectStandardOutput = true, - RedirectStandardError = true, - UseShellExecute = false, + UseShellExecute = true, CreateNoWindow = true, }; process.StartInfo = psi; @@ -143,11 +142,12 @@ private static async Task ValidatePythonAvailability(CancellationToken cancellat } var exitCode = process.ExitCode; + if (exitCode != 0) - throw new Exception($"Python is not installed nor added to PATH. Exit code: {exitCode}."); + 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