Files
2025-07-14 12:03:59 +02:00

66 lines
1.9 KiB
C#

using System.Diagnostics;
namespace Hawkeye.VisionBuilder.Workflow.DataTransfer;
public class WslScriptRunner
{
private Process process;
private readonly string _pythonPath;
private string scriptPath;
private readonly string _workingDirectory;
public WslScriptRunner(string pythonPath, string scriptPath,string workingDirectory)
{
_pythonPath = pythonPath;
this.scriptPath = scriptPath;
_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 arg =
$@"--distribution ubuntu --user root --cd ""{ConvertToWslPath(_workingDirectory)}"" -- /root/ai3/bin/python3 ""{ConvertToWslPath(scriptPath)}""";
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;
}
}
}