Skip to content

Get a venv PythonEnvironment object from an environment name #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Python_Engine/Compute/InstallReferencedVirtualenv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
13 changes: 11 additions & 2 deletions Python_Engine/Python_Engine.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
Expand Down Expand Up @@ -77,6 +77,9 @@
<Compile Include="Compute\Logging.cs" />
<Compile Include="Convert\ToPython.cs" />
<Compile Include="Modify\AddQuotesIfRequired.cs" />
<Compile Include="Query\VirtualEnv.cs" />
<Compile Include="Query\VirtualEnvPythonExePath.cs" />
<Compile Include="Query\VirtualEnvDirectory.cs" />
<Compile Include="Query\ToString.cs" />
<Compile Include="Query\CodeDirectory.cs" />
<Compile Include="Query\EnvironmentExists.cs" />
Expand All @@ -89,6 +92,7 @@
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="Versioning_53.json" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Python_oM\Python_oM.csproj">
Expand Down Expand Up @@ -120,18 +124,23 @@
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>

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__\" &gt; output.log

:: remove temporary log file
del output.log

</PostBuildEvent>
</PropertyGroup>
</Project>
</Project>
52 changes: 52 additions & 0 deletions Python_Engine/Query/VirtualEnv.cs
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

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;
}
}
}
}


51 changes: 51 additions & 0 deletions Python_Engine/Query/VirtualEnvDirectory.cs
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

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;
}
}
}


51 changes: 51 additions & 0 deletions Python_Engine/Query/VirtualEnvPythonExePath.cs
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

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;
}
}
}