diff --git a/Python_Engine/Compute/InstallReferencedVirtualenv.cs b/Python_Engine/Compute/InstallReferencedVirtualenv.cs index 7a07576..5556675 100644 --- a/Python_Engine/Compute/InstallReferencedVirtualenv.cs +++ b/Python_Engine/Compute/InstallReferencedVirtualenv.cs @@ -67,12 +67,12 @@ public static oM.Python.PythonEnvironment InstallReferencedVirtualenv( string bhomPythonExecutable = baseEnv.Executable; // set location where virtual env will be created - string targetDirectory = Path.Combine(Query.EnvironmentsDirectory(), name); + string targetDirectory = Query.VirtualEnvDirectory(name); if (!Directory.Exists(targetDirectory)) Directory.CreateDirectory(targetDirectory); // return existing env if it already exists - oM.Python.PythonEnvironment env = new oM.Python.PythonEnvironment() { Name = name, Executable = Path.Combine(targetDirectory, "Scripts", "python.exe") }; + oM.Python.PythonEnvironment env = new oM.Python.PythonEnvironment() { Name = name, Executable = Query.VirtualEnvPythonExePath(name) }; if (env.EnvironmentExists()) { BH.Engine.Base.Compute.RecordNote($"The {name} environment already exists and is being returned here instead of installing it again. To install a fresh version of this environment, remove this environment first."); diff --git a/Python_Engine/Python_Engine.csproj b/Python_Engine/Python_Engine.csproj index 50624d1..dc2b735 100644 --- a/Python_Engine/Python_Engine.csproj +++ b/Python_Engine/Python_Engine.csproj @@ -1,4 +1,4 @@ - + @@ -77,6 +77,9 @@ + + + @@ -89,6 +92,7 @@ + @@ -120,18 +124,23 @@ + xcopy "$(TargetDir)$(TargetFileName)" "C:\\ProgramData\\BHoM\\Assemblies" /Y xcopy "$(TargetDir)Python.Runtime.dll" "C:\\ProgramData\\BHoM\\Assemblies" /Y + :: create infrastructure necessary to support Python environments if not exist "C:\ProgramData\BHoM\Extensions\PythonEnvironments" mkdir "C:\ProgramData\BHoM\Extensions\PythonEnvironments" if not exist "C:\ProgramData\BHoM\Extensions\PythonCode" mkdir "C:\ProgramData\BHoM\Extensions\PythonCode" + :: remove any old versions within target directory for current toolkits Python code if exist "C:\ProgramData\BHoM\Extensions\PythonCode\$(SolutionName)" rmdir "C:\ProgramData\BHoM\Extensions\PythonCode\$(SolutionName)" /S /Q + :: move Python code over to toolkit extensions folder, redirecting output to temp log file to silence it robocopy "$(ProjectDir)Python" "C:\ProgramData\BHoM\Extensions\PythonCode\$(SolutionName)" /s /purge /xf "*.pyc" /xd "\__pycache__\" > output.log + :: remove temporary log file del output.log - + \ No newline at end of file diff --git a/Python_Engine/Query/VirtualEnv.cs b/Python_Engine/Query/VirtualEnv.cs new file mode 100644 index 0000000..36502cd --- /dev/null +++ b/Python_Engine/Query/VirtualEnv.cs @@ -0,0 +1,52 @@ +/* + * This file is part of the Buildings and Habitats object Model (BHoM) + * Copyright (c) 2015 - 2023, the respective contributors. All rights reserved. + * + * Each contributor holds copyright over their respective contributions. + * The project versioning (Git) records all such contribution source information. + * + * + * The BHoM is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3.0 of the License, or + * (at your option) any later version. + * + * The BHoM is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this code. If not, see . + */ + +using BH.oM.Base.Attributes; +using BH.oM.Python; +using System.ComponentModel; +using System.IO; +using System.Xml.Linq; + +namespace BH.Engine.Python +{ + public static partial class Query + { + [Description("Get an installed environment without the overhead of attempting to install that environment.")] + [Input("envName","The virtual environment name to request.")] + [Output("The virtual environment, if it exists.")] + public static PythonEnvironment VirtualEnv(string envName) + { + string exePath = Query.VirtualEnvPythonExePath(envName); + if (File.Exists(exePath)) + { + return new PythonEnvironment() { Name = envName, Executable = exePath }; + } + else + { + BH.Engine.Base.Compute.RecordError($"No environment could be found for {envName}. Use the appropriate InstallPythonEnv to install this environment."); + return null; + } + } + } +} + + diff --git a/Python_Engine/Query/VirtualEnvDirectory.cs b/Python_Engine/Query/VirtualEnvDirectory.cs new file mode 100644 index 0000000..dbc954b --- /dev/null +++ b/Python_Engine/Query/VirtualEnvDirectory.cs @@ -0,0 +1,51 @@ +/* + * This file is part of the Buildings and Habitats object Model (BHoM) + * Copyright (c) 2015 - 2023, the respective contributors. All rights reserved. + * + * Each contributor holds copyright over their respective contributions. + * The project versioning (Git) records all such contribution source information. + * + * + * The BHoM is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3.0 of the License, or + * (at your option) any later version. + * + * The BHoM is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this code. If not, see . + */ + +using BH.oM.Base.Attributes; + +using System.ComponentModel; +using System.IO; +using System.Xml.Linq; + +namespace BH.Engine.Python +{ + public static partial class Query + { + [Description("The location where any BHoM generated Python environments reside.")] + [Input("envName","The virtual environment name.")] + [Output("The location where any BHoM generated Python environments reside.")] + public static string VirtualEnvDirectory(string envName) + { + string directory = Path.Combine(Query.EnvironmentsDirectory(), envName); + + if (Directory.Exists(directory)) + { + return directory; + } + + BH.Engine.Base.Compute.RecordError($"The virtual environment directory for \"{envName}\" does not exist at \"{directory}\"."); + return null; + } + } +} + + diff --git a/Python_Engine/Query/VirtualEnvPythonExePath.cs b/Python_Engine/Query/VirtualEnvPythonExePath.cs new file mode 100644 index 0000000..43d6a20 --- /dev/null +++ b/Python_Engine/Query/VirtualEnvPythonExePath.cs @@ -0,0 +1,51 @@ +/* + * This file is part of the Buildings and Habitats object Model (BHoM) + * Copyright (c) 2015 - 2023, the respective contributors. All rights reserved. + * + * Each contributor holds copyright over their respective contributions. + * The project versioning (Git) records all such contribution source information. + * + * + * The BHoM is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3.0 of the License, or + * (at your option) any later version. + * + * The BHoM is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this code. If not, see . + */ + +using BH.oM.Base.Attributes; + +using System.ComponentModel; +using System.IO; +using System.Xml.Linq; + +namespace BH.Engine.Python +{ + public static partial class Query + { + [Description("The location where Python.exe for a named virtual environment would reside. This method does not check if that environment actually exists or not.")] + [Input("envName","The virtual environment name.")] + [Output("The location where a Python virtual environment would reside.")] + public static string VirtualEnvPythonExePath(string envName) + { + string exePath = Path.Combine(VirtualEnvDirectory(envName), "Scripts", "python.exe"); + + if (File.Exists(exePath)) + { + return exePath; + } + + BH.Engine.Base.Compute.RecordError($"The executable for \"{envName}\" does not exist at \"{exePath}\"."); + return null; + } + } +} + +