From 4e532588d235273753af8f48c057488d7908158f Mon Sep 17 00:00:00 2001 From: Tristan Gerrish Date: Mon, 27 Nov 2023 20:23:38 +0000 Subject: [PATCH 1/2] Added version check to existing environment query and updated Pollination version method --- .../Compute/InstallPythonEnv_LBT.cs | 42 +++++++------------ .../LadybugTools_Engine.csproj | 4 +- .../Query/IsPollinationInstalled.cs | 21 ++-------- LadybugTools_oM/LadybugTools_oM.csproj | 2 +- 4 files changed, 22 insertions(+), 47 deletions(-) diff --git a/LadybugTools_Engine/Compute/InstallPythonEnv_LBT.cs b/LadybugTools_Engine/Compute/InstallPythonEnv_LBT.cs index 1b75693e..9ca4fd9d 100644 --- a/LadybugTools_Engine/Compute/InstallPythonEnv_LBT.cs +++ b/LadybugTools_Engine/Compute/InstallPythonEnv_LBT.cs @@ -37,43 +37,31 @@ public static partial class Compute [Output("env", "The LadybugTools_Toolkit Python Environment, with BHoM code accessible.")] public static PythonEnvironment InstallPythonEnv_LBT(bool run = false, bool reinstall = false) { - // check if referenced Python is installed - string referencedExecutable = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) + @"\ladybug_tools\python\python.exe"; + if (!run) + return null; + // check if referenced Python is installed, and get executable and version if it is if (!Query.IsPollinationInstalled()) { return null; } - - if (!run) - return null; - - // find out whether this environment already exists - bool exists = Python.Query.VirtualEnvironmentExists(Query.ToolkitName()); - - if (reinstall) - Python.Compute.RemoveVirtualEnvironment(Query.ToolkitName()); - - // obtain python version + string referencedExecutable = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) + @"\ladybug_tools\python\python.exe"; PythonVersion pythonVersion = Python.Query.Version(referencedExecutable); - // create virtualenvironment - PythonEnvironment env = Python.Compute.VirtualEnvironment(version: pythonVersion, name: Query.ToolkitName(), reload: true); + // check if environment already exists. If it does, and no reinstall requested, load it + bool exists = Python.Query.VirtualEnvironmentExists(envName: Query.ToolkitName(), pythonVersion: pythonVersion); + if (run && exists && !reinstall) + return Python.Compute.VirtualEnvironment(version: pythonVersion, name: Query.ToolkitName(), reload: true); - // return null if environment could not be created/loaded - if (env == null) - return null; + // create environment from scratch + PythonEnvironment env = Python.Compute.VirtualEnvironment(version: pythonVersion, name: Query.ToolkitName(), reload: false); - // install packages if this is a reinstall, or the environment did not originally exist - if (reinstall || !exists) - { - // install local package - env.InstallPackageLocal(Path.Combine(Python.Query.DirectoryCode(), Query.ToolkitName())); + // install local package + env.InstallPackageLocal(Path.Combine(Python.Query.DirectoryCode(), Query.ToolkitName())); - // create requiremetns from referenced executable - string requirementsTxt = Python.Compute.RequirementsTxt(referencedExecutable, Path.Combine(Python.Query.DirectoryEnvironments(), $"requirements_{Query.ToolkitName()}.txt")); - env.InstallRequirements(requirementsTxt); - } + // create requirements from referenced executable + string requirementsTxt = Python.Compute.RequirementsTxt(referencedExecutable, Path.Combine(Python.Query.DirectoryEnvironments(), $"requirements_{Query.ToolkitName()}.txt")); + env.InstallRequirements(requirementsTxt); return env; } diff --git a/LadybugTools_Engine/LadybugTools_Engine.csproj b/LadybugTools_Engine/LadybugTools_Engine.csproj index 2ca83f00..3ab642cb 100644 --- a/LadybugTools_Engine/LadybugTools_Engine.csproj +++ b/LadybugTools_Engine/LadybugTools_Engine.csproj @@ -85,11 +85,11 @@ - + - + \ No newline at end of file diff --git a/LadybugTools_Engine/Query/IsPollinationInstalled.cs b/LadybugTools_Engine/Query/IsPollinationInstalled.cs index b10442c5..d9fa3f7d 100644 --- a/LadybugTools_Engine/Query/IsPollinationInstalled.cs +++ b/LadybugTools_Engine/Query/IsPollinationInstalled.cs @@ -35,10 +35,9 @@ namespace BH.Engine.LadybugTools public static partial class Query { [Description("Return True if Pollination is installed to the currently supported version.")] - [Input("targetPollinationVersion", "The target Pollination version that BHoM currently supports. Default is 1.35.14 which has an uninstaller ProductVersion of 1.38.104, which is the number that is checked against on the uninstaller that comes bundled with Pollincation.")] - [Input("includeBuildNumber", "If true, the build number (the third number in the X.X.X ProductVersion) will be included in the comparison.")] + [Input("targetPollinationVersion", "The target Pollination version that BHoM currently supports.")] [Output("bool", "True if Pollination is installed to the currently supported version.")] - public static bool IsPollinationInstalled(string targetPollinationVersion = "1.38.104", bool includeBuildNumber = false) + public static bool IsPollinationInstalled(string targetPollinationVersion = "1.35.14") { // check if referenced Python is installed string referencedExecutable = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) + @"\ladybug_tools\python\python.exe"; @@ -49,26 +48,14 @@ public static bool IsPollinationInstalled(string targetPollinationVersion = "1.3 } // obtain version of pollination installed - string referencedUninstaller = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) + @"\ladybug_tools\uninstall.exe"; + string referencedUninstaller = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) + @"\pollination\uninstall.exe"; FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(referencedUninstaller); - if (includeBuildNumber && (versionInfo.ProductVersion != targetPollinationVersion)) + if (versionInfo.ProductVersion != targetPollinationVersion) { Base.Compute.RecordError($"Pollination version installed ({versionInfo.ProductVersion}) is not the same as the version required for this code to function correctly ({targetPollinationVersion})."); return false; } - // check that version matches the target version - int major = int.Parse(targetPollinationVersion.Split('.')[0]); - int minor = int.Parse(targetPollinationVersion.Split('.')[1]); - if (versionInfo.ProductMajorPart != major || versionInfo.ProductMinorPart != minor) - { - if (versionInfo.ProductVersion != targetPollinationVersion) - { - Base.Compute.RecordError($"Pollination version installed ({versionInfo.ProductVersion}) is not the same as the version required for this code to function correctly ({targetPollinationVersion})."); - return false; - } - } - return true; } } diff --git a/LadybugTools_oM/LadybugTools_oM.csproj b/LadybugTools_oM/LadybugTools_oM.csproj index a3d3c750..ee402bf8 100644 --- a/LadybugTools_oM/LadybugTools_oM.csproj +++ b/LadybugTools_oM/LadybugTools_oM.csproj @@ -41,7 +41,7 @@ - + From 3b409831e5523b776f62d6935a33c911b8bc5ee6 Mon Sep 17 00:00:00 2001 From: Tristan Gerrish <10939984+tg359@users.noreply.github.com> Date: Mon, 27 Nov 2023 20:37:24 +0000 Subject: [PATCH 2/2] Update InstallPythonEnv_LBT.cs --- LadybugTools_Engine/Compute/InstallPythonEnv_LBT.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/LadybugTools_Engine/Compute/InstallPythonEnv_LBT.cs b/LadybugTools_Engine/Compute/InstallPythonEnv_LBT.cs index 9ca4fd9d..ee0de93a 100644 --- a/LadybugTools_Engine/Compute/InstallPythonEnv_LBT.cs +++ b/LadybugTools_Engine/Compute/InstallPythonEnv_LBT.cs @@ -42,9 +42,7 @@ public static PythonEnvironment InstallPythonEnv_LBT(bool run = false, bool rein // check if referenced Python is installed, and get executable and version if it is if (!Query.IsPollinationInstalled()) - { return null; - } string referencedExecutable = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) + @"\ladybug_tools\python\python.exe"; PythonVersion pythonVersion = Python.Query.Version(referencedExecutable);