|
22 | 22 |
|
23 | 23 | using BH.oM.Base.Attributes; |
24 | 24 | using BH.oM.Python; |
| 25 | +using BH.oM.Python.Enums; |
25 | 26 | using System.ComponentModel; |
26 | | -using System.Collections.Generic; |
27 | 27 | using System.IO; |
| 28 | +using BH.Engine.Python; |
28 | 29 |
|
29 | 30 | namespace BH.Engine.LadybugTools |
30 | 31 | { |
31 | 32 | public static partial class Compute |
32 | 33 | { |
33 | 34 | [Description("Create the BHoM Python environment for LadybugTools_Toolkit. This creates a replica of what is found in the Pollination installed python environment, for extension using BHoM.")] |
34 | 35 | [Input("run", "Run the installation process.")] |
| 36 | + [Input("reinstall", "Reinstall the environment if it already exists.")] |
35 | 37 | [Output("env", "The LadybugTools_Toolkit Python Environment, with BHoM code accessible.")] |
36 | | - public static PythonEnvironment InstallPythonEnv_LBT(bool run = false) |
| 38 | + [PreviousVersion("6.3", "BH.Engine.LadybugTools.Compute.InstallPythonEnv_LBT(System.Boolean)")] |
| 39 | + public static PythonEnvironment InstallPythonEnv_LBT(bool run = false, bool reinstall = false) |
37 | 40 | { |
| 41 | + // check if referenced Python is installed |
38 | 42 | string referencedExecutable = @"C:\Program Files\ladybug_tools\python\python.exe"; |
| 43 | + if (!File.Exists(referencedExecutable)) |
| 44 | + { |
| 45 | + Base.Compute.RecordError($"Could not find referenced python executable at {referencedExecutable}. Please install Pollination try again."); |
| 46 | + return null; |
| 47 | + } |
| 48 | + |
| 49 | + if (!run) |
| 50 | + return null; |
39 | 51 |
|
40 | | - PythonEnvironment referencedEnvironment = Python.Compute.InstallReferencedVirtualenv( |
41 | | - name: Query.ToolkitName(), |
42 | | - executable: referencedExecutable, |
43 | | - localPackage: Path.Combine(Python.Query.CodeDirectory(), Query.ToolkitName()), |
44 | | - run: run |
45 | | - ); |
| 52 | + // find out whether this environment already exists |
| 53 | + bool exists = Python.Query.VirtualEnvironmentExists(Query.ToolkitName()); |
46 | 54 |
|
47 | | - // reload environment to establish the new executable path |
48 | | - PythonEnvironment localEnvironment = Python.Query.VirtualEnv(Query.ToolkitName()); |
| 55 | + if (reinstall) |
| 56 | + Python.Compute.RemoveVirtualEnvironment(Query.ToolkitName()); |
| 57 | + |
| 58 | + // obtain python version |
| 59 | + PythonVersion pythonVersion = Python.Query.Version(referencedExecutable); |
49 | 60 |
|
50 | | - // check here to ensure that referenced executable is using same version as local BHoM environment executable |
51 | | - List<string> packagesToCheck = new List<string>() { "lbt-ladybug", "lbt-dragonfly", "lbt-honeybee", "lbt-recipes" }; |
52 | | - foreach ( string package in packagesToCheck ) |
| 61 | + // create virtualenvironment |
| 62 | + PythonEnvironment env = Python.Compute.VirtualEnvironment(version: pythonVersion, name: Query.ToolkitName(), reload: true); |
| 63 | + |
| 64 | + // return null if environment could not be created/loaded |
| 65 | + if (env == null) |
| 66 | + return null; |
| 67 | + |
| 68 | + // install packages if this is a reinstall, or the environment did not originally exist |
| 69 | + if (reinstall || !exists) |
53 | 70 | { |
54 | | - string installed = Python.Compute.RunCommandStdout($"{Python.Modify.AddQuotesIfRequired(localEnvironment.Executable)} -m pip freeze | FindStr {package}"); |
55 | | - string referenced = Python.Compute.RunCommandStdout($"{Python.Modify.AddQuotesIfRequired(referencedEnvironment.Executable)} -m pip freeze | FindStr {package}"); |
56 | | - if (installed != referenced) |
57 | | - { |
58 | | - Base.Compute.RecordWarning($"BHoM environment {package} does not match referenced package version ({installed} != {referenced}). " + |
59 | | - $"This can be caused by the BHoM version and installed version becoming out of sync. " + |
60 | | - $"Try deleting the {Python.Query.VirtualEnvDirectory("LadybugTools_Toolkit")} directory and re-running the " + |
61 | | - $"{System.Reflection.MethodBase.GetCurrentMethod().Name} method again to fix this."); |
62 | | - } |
| 71 | + // install local package |
| 72 | + env.InstallPackageLocal(Path.Combine(Python.Query.DirectoryCode(), Query.ToolkitName())); |
| 73 | + |
| 74 | + // create requiremetns from referenced executable |
| 75 | + string requirementsTxt = Python.Compute.RequirementsTxt(referencedExecutable, Path.Combine(Python.Query.DirectoryEnvironments(), $"requirements_{Query.ToolkitName()}.txt")); |
| 76 | + env.InstallRequirements(requirementsTxt); |
| 77 | + File.Delete(requirementsTxt); |
63 | 78 | } |
64 | 79 |
|
65 | | - return localEnvironment; |
| 80 | + return env; |
66 | 81 | } |
67 | 82 | } |
68 | 83 | } |
|
0 commit comments