From 31a3a4f38739748841df9957780a3be9608a8529 Mon Sep 17 00:00:00 2001 From: Ionite Date: Thu, 8 Jun 2023 16:01:33 -0400 Subject: [PATCH] Move Python310 to be extracted to appdata --- StabilityMatrix/Python/PyRunner.cs | 37 +++++++++---------- StabilityMatrix/Python/PyVenvRunner.cs | 2 +- StabilityMatrix/StabilityMatrix.csproj | 5 +-- .../ViewModels/InstallerViewModel.cs | 3 +- .../ViewModels/OneClickInstallViewModel.cs | 3 +- 5 files changed, 22 insertions(+), 28 deletions(-) diff --git a/StabilityMatrix/Python/PyRunner.cs b/StabilityMatrix/Python/PyRunner.cs index e07e38ce..2ceefb2c 100644 --- a/StabilityMatrix/Python/PyRunner.cs +++ b/StabilityMatrix/Python/PyRunner.cs @@ -15,18 +15,15 @@ public class PyRunner : IPyRunner { private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); - private const string RelativeHomePath = @"Assets\Python310"; - private const string RelativeDllPath = @"Assets\Python310\python310.dll"; - private const string RelativeExePath = @"Assets\Python310\python.exe"; - private const string RelativePipExePath = @"Assets\Python310\Scripts\pip.exe"; - private const string RelativeGetPipPath = @"Assets\Python310\get-pip.pyc"; - private const string RelativeVenvPath = @"Assets\Python310\Scripts\virtualenv.exe"; - public static string HomePath => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, RelativeHomePath); - public static string DllPath => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, RelativeDllPath); - public static string ExePath => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, RelativeExePath); - public static string PipExePath => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, RelativePipExePath); - public static string GetPipPath => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, RelativeGetPipPath); - public static string VenvPath => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, RelativeVenvPath); + private static readonly string AppDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); + private static readonly string HomeDir = Path.Combine(AppDataDir, "StabilityMatrix"); + + public static string PythonDir => Path.Combine(HomeDir, "Python310"); + public static string PythonDllPath => Path.Combine(PythonDir, "python310.dll"); + public static string PythonExePath => Path.Combine(PythonDir, "python.exe"); + public static string GetPipPath => Path.Combine(PythonDir, "get-pip.pyc"); + public static string PipExePath => Path.Combine(PythonDir, "Scripts", "pip.exe"); + public static string VenvPath => Path.Combine(PythonDir, "Scripts", "virtualenv.exe"); public static bool PipInstalled => File.Exists(PipExePath); public static bool VenvInstalled => File.Exists(VenvPath); @@ -45,22 +42,22 @@ public class PyRunner : IPyRunner { if (PythonEngine.IsInitialized) return; - Logger.Info("Setting PYTHONHOME and PATH to {HomePath}", HomePath); - Environment.SetEnvironmentVariable("PYTHONHOME", HomePath, EnvironmentVariableTarget.Process); + Logger.Info("Setting PYTHONHOME and PATH to {HomePath}", PythonDir); + Environment.SetEnvironmentVariable("PYTHONHOME", PythonDir, EnvironmentVariableTarget.Process); // Get existing PATH var currentEnvPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process); // Append Python path to PATH - Environment.SetEnvironmentVariable("PATH", $"{HomePath};{currentEnvPath}", EnvironmentVariableTarget.Process); + Environment.SetEnvironmentVariable("PATH", $"{PythonDir};{currentEnvPath}", EnvironmentVariableTarget.Process); - Logger.Info("Initializing Python runtime with DLL: {DllPath}", DllPath); + Logger.Info("Initializing Python runtime with DLL: {DllPath}", PythonDllPath); // Check PythonDLL exists - if (!File.Exists(DllPath)) + if (!File.Exists(PythonDllPath)) { Logger.Error("Python DLL not found"); - throw new FileNotFoundException("Python DLL not found", DllPath); + throw new FileNotFoundException("Python DLL not found", PythonDllPath); } - Runtime.PythonDLL = DllPath; + Runtime.PythonDLL = PythonDllPath; PythonEngine.Initialize(); PythonEngine.BeginAllowThreads(); @@ -84,7 +81,7 @@ public class PyRunner : IPyRunner { throw new FileNotFoundException("get-pip not found", GetPipPath); } - var p = ProcessRunner.StartProcess(ExePath, "-m get-pip"); + var p = ProcessRunner.StartProcess(PythonExePath, "-m get-pip"); await ProcessRunner.WaitForExitConditionAsync(p); } diff --git a/StabilityMatrix/Python/PyVenvRunner.cs b/StabilityMatrix/Python/PyVenvRunner.cs index 8f09ebdf..d1e3eae2 100644 --- a/StabilityMatrix/Python/PyVenvRunner.cs +++ b/StabilityMatrix/Python/PyVenvRunner.cs @@ -68,7 +68,7 @@ public class PyVenvRunner : IDisposable } // Create venv - var venvProc = ProcessRunner.StartProcess(PyRunner.ExePath, $"-m virtualenv \"{RootPath}\""); + var venvProc = ProcessRunner.StartProcess(PyRunner.PythonExePath, $"-m virtualenv \"{RootPath}\""); await venvProc.WaitForExitAsync(); // Check return code diff --git a/StabilityMatrix/StabilityMatrix.csproj b/StabilityMatrix/StabilityMatrix.csproj index adc47ee5..8d8b5a05 100644 --- a/StabilityMatrix/StabilityMatrix.csproj +++ b/StabilityMatrix/StabilityMatrix.csproj @@ -10,6 +10,7 @@ Assets\Icon.ico 11 en + Assets\Python310\** @@ -81,9 +82,7 @@ - - Always - + diff --git a/StabilityMatrix/ViewModels/InstallerViewModel.cs b/StabilityMatrix/ViewModels/InstallerViewModel.cs index 9116b0e8..ba293953 100644 --- a/StabilityMatrix/ViewModels/InstallerViewModel.cs +++ b/StabilityMatrix/ViewModels/InstallerViewModel.cs @@ -409,8 +409,7 @@ public partial class InstallerViewModel : ObservableObject ProgressValue = Convert.ToInt32(progress.Percentage); }); - await prerequisiteHelper.InstallVcRedistIfNecessary(progressHandler); - await prerequisiteHelper.InstallGitIfNecessary(progressHandler); + await prerequisiteHelper.InstallAllIfNecessary(progressHandler); } private void OnPackageInstalled() => PackageInstalled?.Invoke(this, EventArgs.Empty); diff --git a/StabilityMatrix/ViewModels/OneClickInstallViewModel.cs b/StabilityMatrix/ViewModels/OneClickInstallViewModel.cs index 2b0216b7..8b4a7d43 100644 --- a/StabilityMatrix/ViewModels/OneClickInstallViewModel.cs +++ b/StabilityMatrix/ViewModels/OneClickInstallViewModel.cs @@ -93,8 +93,7 @@ public partial class OneClickInstallViewModel : ObservableObject OneClickInstallProgress = Convert.ToInt32(progress.Percentage); }); - await prerequisiteHelper.InstallGitIfNecessary(progressHandler); - await prerequisiteHelper.InstallVcRedistIfNecessary(progressHandler); + await prerequisiteHelper.InstallAllIfNecessary(progressHandler); SubHeaderText = "Installing prerequisites..."; IsIndeterminate = true;