Skip to content

Commit c85ad90

Browse files
tg359Fraser Greenroyd
authored andcommitted
added Venv query method and checks for existence of files/directories
1 parent 6a2917a commit c85ad90

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

Python_Engine/Python_Engine.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
<Compile Include="Convert\ToPython.cs" />
7979
<Compile Include="Modify\AddQuotesIfRequired.cs" />
8080
<Compile Include="Create\VirtualEnvironment.cs" />
81+
<Compile Include="Query\VirtualEnv.cs" />
8182
<Compile Include="Query\VirtualEnvPythonExePath.cs" />
8283
<Compile Include="Query\VirtualEnvDirectory.cs" />
8384
<Compile Include="Query\ToString.cs" />

Python_Engine/Query/VirtualEnv.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* This file is part of the Buildings and Habitats object Model (BHoM)
3+
* Copyright (c) 2015 - 2022, the respective contributors. All rights reserved.
4+
*
5+
* Each contributor holds copyright over their respective contributions.
6+
* The project versioning (Git) records all such contribution source information.
7+
*
8+
*
9+
* The BHoM is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3.0 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* The BHoM is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public License
20+
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
21+
*/
22+
23+
using BH.oM.Base.Attributes;
24+
using BH.oM.Python;
25+
using System.ComponentModel;
26+
using System.IO;
27+
using System.Xml.Linq;
28+
29+
namespace BH.Engine.Python
30+
{
31+
public static partial class Query
32+
{
33+
[Description("Get an installed environment without the overhead of attempting to install that environment.")]
34+
[Input("envName","The virtual environment name to request.")]
35+
[Output("The virtual environment, if it exists.")]
36+
public static PythonEnvironment VirtualEnv(string envName)
37+
{
38+
return new PythonEnvironment() { Executable = VirtualEnvPythonExePath(envName), Name = envName };
39+
}
40+
}
41+
}
42+

Python_Engine/Query/VirtualEnvDirectory.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,15 @@ public static partial class Query
3535
[Output("The location where any BHoM generated Python environments reside.")]
3636
public static string VirtualEnvDirectory(string envName)
3737
{
38-
return Path.Combine(Query.EnvironmentsDirectory(), envName);
38+
string directory = Path.Combine(Query.EnvironmentsDirectory(), envName);
39+
40+
if (Directory.Exists(directory))
41+
{
42+
return directory;
43+
}
44+
45+
BH.Engine.Base.Compute.RecordError($"The virtual environment directory for \"{envName}\" does not exist at \"{directory}\".");
46+
return null;
3947
}
4048
}
4149
}

Python_Engine/Query/VirtualEnvPythonExePath.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,15 @@ public static partial class Query
3535
[Output("The location where a Python virtual environment would reside.")]
3636
public static string VirtualEnvPythonExePath(string envName)
3737
{
38-
return Path.Combine(VirtualEnvDirectory(envName), "Scripts", "python.exe");
38+
string exePath = Path.Combine(VirtualEnvDirectory(envName), "Scripts", "python.exe");
39+
40+
if (File.Exists(exePath))
41+
{
42+
return exePath;
43+
}
44+
45+
BH.Engine.Base.Compute.RecordError($"The executable for \"{envName}\" does not exist at \"{exePath}\".");
46+
return null;
3947
}
4048
}
4149
}

0 commit comments

Comments
 (0)