|
| 1 | +using System.Diagnostics; |
| 2 | +using OpenCut.Installer.Models; |
| 3 | + |
| 4 | +namespace OpenCut.Installer.Services; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Installs optional Python packages via pip using system Python. |
| 8 | +/// </summary> |
| 9 | +public class DependencyInstaller |
| 10 | +{ |
| 11 | + private static readonly string[] PythonCandidates = ["python", "python3", "py"]; |
| 12 | + |
| 13 | + public void InstallDeps(InstallConfig config, IProgress<InstallProgress> progress, int step, int totalSteps) |
| 14 | + { |
| 15 | + var stepName = "Installing optional tools"; |
| 16 | + |
| 17 | + if (!config.InstallOptionalDeps || config.SelectedDeps.Count == 0) |
| 18 | + { |
| 19 | + Report(progress, step, totalSteps, stepName, "Skipped (none selected).", LogLevel.Debug); |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + // Find system Python |
| 24 | + var python = FindPython(); |
| 25 | + if (python == null) |
| 26 | + { |
| 27 | + Report(progress, step, totalSteps, stepName, |
| 28 | + "Python not found in PATH — optional tools skipped. Install from python.org and add to PATH.", |
| 29 | + LogLevel.Warning); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + Report(progress, step, totalSteps, stepName, $"Using Python: {python}"); |
| 34 | + |
| 35 | + foreach (var package in config.SelectedDeps) |
| 36 | + { |
| 37 | + Report(progress, step, totalSteps, stepName, $"Installing {package}..."); |
| 38 | + try |
| 39 | + { |
| 40 | + var psi = new ProcessStartInfo |
| 41 | + { |
| 42 | + FileName = python, |
| 43 | + Arguments = $"-m pip install {package} -q", |
| 44 | + RedirectStandardOutput = true, |
| 45 | + RedirectStandardError = true, |
| 46 | + UseShellExecute = false, |
| 47 | + CreateNoWindow = true |
| 48 | + }; |
| 49 | + |
| 50 | + using var process = Process.Start(psi); |
| 51 | + if (process == null) |
| 52 | + { |
| 53 | + Report(progress, step, totalSteps, stepName, |
| 54 | + $"Failed to start pip for {package}.", LogLevel.Error); |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + process.WaitForExit(300000); // 5 min per package |
| 59 | + |
| 60 | + if (process.ExitCode == 0) |
| 61 | + { |
| 62 | + Report(progress, step, totalSteps, stepName, |
| 63 | + $"{package} installed successfully.", LogLevel.Success); |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + var stderr = process.StandardError.ReadToEnd(); |
| 68 | + var msg = string.IsNullOrWhiteSpace(stderr) ? $"exit code {process.ExitCode}" : stderr.Trim(); |
| 69 | + Report(progress, step, totalSteps, stepName, |
| 70 | + $"Failed to install {package}: {msg}", LogLevel.Warning); |
| 71 | + } |
| 72 | + } |
| 73 | + catch (Exception ex) |
| 74 | + { |
| 75 | + Report(progress, step, totalSteps, stepName, |
| 76 | + $"Error installing {package}: {ex.Message}", LogLevel.Warning); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private static string? FindPython() |
| 82 | + { |
| 83 | + foreach (var name in PythonCandidates) |
| 84 | + { |
| 85 | + try |
| 86 | + { |
| 87 | + var psi = new ProcessStartInfo |
| 88 | + { |
| 89 | + FileName = name, |
| 90 | + Arguments = "--version", |
| 91 | + RedirectStandardOutput = true, |
| 92 | + RedirectStandardError = true, |
| 93 | + UseShellExecute = false, |
| 94 | + CreateNoWindow = true |
| 95 | + }; |
| 96 | + |
| 97 | + using var process = Process.Start(psi); |
| 98 | + if (process == null) continue; |
| 99 | + process.WaitForExit(10000); |
| 100 | + |
| 101 | + if (process.ExitCode == 0) |
| 102 | + return name; |
| 103 | + } |
| 104 | + catch |
| 105 | + { |
| 106 | + // Not found, try next |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return null; |
| 111 | + } |
| 112 | + |
| 113 | + private static void Report(IProgress<InstallProgress> progress, int step, int total, |
| 114 | + string stepName, string message, LogLevel level = LogLevel.Info) |
| 115 | + { |
| 116 | + progress.Report(new InstallProgress |
| 117 | + { |
| 118 | + StepNumber = step, |
| 119 | + TotalSteps = total, |
| 120 | + StepName = stepName, |
| 121 | + Message = message, |
| 122 | + Level = level |
| 123 | + }); |
| 124 | + } |
| 125 | +} |
0 commit comments