Files
HawkeyeVision/Hawkeye.VisionBuilder.Workflow/DataTransfer/UvicornScriptRunner.cs
meelstorm 07002dd875 c# script support
python server support
2025-08-25 17:02:33 +02:00

63 lines
1.7 KiB
C#

using System.Diagnostics;
namespace Hawkeye.VisionBuilder.Workflow.DataTransfer;
public class UvicornScriptRunner
{
private Process process;
private readonly string _workingDirectory;
public UvicornScriptRunner(string workingDirectory)
{
_workingDirectory = workingDirectory;
}
public static string ConvertToWslPath(string windowsPath)
{
if (string.IsNullOrWhiteSpace(windowsPath))
throw new ArgumentException("Path cannot be null or empty.", nameof(windowsPath));
// Replace backslashes with forward slashes
string wslPath = windowsPath.Replace('\\', '/');
// Extract the drive letter and convert it to WSL format
if (wslPath.Length > 1 && wslPath[1] == ':')
{
char driveLetter = char.ToLower(wslPath[0]);
wslPath = $"/mnt/{driveLetter}{wslPath.Substring(2)}";
}
return wslPath;
}
public void Start()
{
//startInfo.CreateNoWindow = true;
// create and start the process
process = new Process();
var convertedWd = ConvertToWslPath(_workingDirectory);
var arg =
$@"--distribution ubuntu --user root --cd ""{convertedWd}"" -- uvicorn PythonModelAPI:app";
process.StartInfo = new ProcessStartInfo("wsl", arg)
{
UseShellExecute = false
};
process.Start();
// on windows only
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
ChildProcessTracker.AddProcess(process);
}
public void Stop()
{
if (process != null && !process.HasExited)
{
process.Kill();
process.Dispose();
process = null;
}
}
}