Skip to content

Commit 6be7b0b

Browse files
author
Fraser Greenroyd
authored
6.3 Deployment (#116)
2 parents 0b1ec0e + 803ecf5 commit 6be7b0b

70 files changed

Lines changed: 8646 additions & 7777 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

LadybugTools_Adapter/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,5 @@
5555
// by using the '*' as shown below:
5656
// [assembly: AssemblyVersion("1.0.*")]
5757
[assembly: AssemblyVersion("6.0.0.0")]
58-
[assembly: AssemblyFileVersion("6.2.0.0")]
58+
[assembly: AssemblyFileVersion("6.3.0.0")]
5959

LadybugTools_Engine/Compute/EPWtoCSV.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ public static partial class Compute
3535
[Input("epwFile", "An EPW file.")]
3636
[Input("includeAdditional", "Add sun position and psychrometric properties to the resultant CSV.")]
3737
[Output("csv", "The generated CSV file.")]
38-
[PreviousVersion("6.2", "BH.Engine.LadybugTools.Compute.EPWtoCSV(System.String)")]
3938
public static string EPWtoCSV(string epwFile, bool includeAdditional = false)
4039
{
4140
if (epwFile == null)
@@ -49,8 +48,8 @@ public static string EPWtoCSV(string epwFile, bool includeAdditional = false)
4948
BH.Engine.Base.Compute.RecordError($"{epwFile} doesn't appear to exist!");
5049
return null;
5150
}
52-
53-
PythonEnvironment env = Python.Query.VirtualEnv(Query.ToolkitName());
51+
52+
PythonEnvironment env = InstallPythonEnv_LBT(true);
5453
string additionalProperties = includeAdditional ? "True" : "False";
5554

5655
string pythonScript = string.Join("\n", new List<string>()

LadybugTools_Engine/Compute/EPWtoCustomObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static CustomObject EPWtoCustomObject(string epwFile)
4848
return null;
4949
}
5050

51-
PythonEnvironment env = Python.Query.VirtualEnv(Query.ToolkitName());
51+
PythonEnvironment env = InstallPythonEnv_LBT(true);
5252

5353
string pythonScript = string.Join("\n", new List<string>()
5454
{

LadybugTools_Engine/Compute/ExternalComfort.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
using BH.oM.LadybugTools;
3131
using BH.Engine.Serialiser;
3232
using BH.Engine.Geometry;
33+
using BH.Engine.Base;
3334

3435
namespace BH.Engine.LadybugTools
3536
{
@@ -71,7 +72,7 @@ public static ExternalComfort ExternalComfort(SimulationResult simulationResult,
7172

7273
// send to Python to simulate/load
7374
string externalComfortJsonStr = System.Text.RegularExpressions.Regex.Unescape(externalComfort.ToJson());
74-
PythonEnvironment env = Python.Query.VirtualEnv(Query.ToolkitName());
75+
PythonEnvironment env = InstallPythonEnv_LBT(true);
7576
string pythonScript = string.Join("\n", new List<string>()
7677
{
7778
"import json",
@@ -86,6 +87,12 @@ public static ExternalComfort ExternalComfort(SimulationResult simulationResult,
8687

8788
string output = env.RunPythonString(pythonScript).Trim().Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.None).Last();
8889

90+
if (output.Substring(0, 12).Contains("error"))
91+
{
92+
BH.Engine.Base.Compute.RecordError(Serialiser.Convert.FromJson(output).PropertyValue("error").ToString());
93+
return null;
94+
}
95+
8996
// reload from Python results
9097
return (ExternalComfort)Serialiser.Convert.FromJson(output);
9198
}

LadybugTools_Engine/Compute/GEMtoHBJSON.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static string GEMtoHBJSON(string gem)
4949
return null;
5050
}
5151

52-
PythonEnvironment env = Python.Query.VirtualEnv(Query.ToolkitName());
52+
PythonEnvironment env = InstallPythonEnv_LBT(true);
5353

5454
string gemFile = System.IO.Path.GetFullPath(gem);
5555
string outputDirectory = System.IO.Path.GetDirectoryName(gem);

LadybugTools_Engine/Compute/HBJSONtoGEM.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static string HBJSONtoGEM(string hbjson)
4949
return null;
5050
}
5151

52-
PythonEnvironment env = Python.Query.VirtualEnv(Query.ToolkitName());
52+
PythonEnvironment env = InstallPythonEnv_LBT(true);
5353

5454
string hbjsonFile = System.IO.Path.GetFullPath(hbjson);
5555
string outputDirectory = System.IO.Path.GetDirectoryName(hbjsonFile);

LadybugTools_Engine/Compute/InstallPythonEnv_LBT.cs

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,47 +22,62 @@
2222

2323
using BH.oM.Base.Attributes;
2424
using BH.oM.Python;
25+
using BH.oM.Python.Enums;
2526
using System.ComponentModel;
26-
using System.Collections.Generic;
2727
using System.IO;
28+
using BH.Engine.Python;
2829

2930
namespace BH.Engine.LadybugTools
3031
{
3132
public static partial class Compute
3233
{
3334
[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.")]
3435
[Input("run", "Run the installation process.")]
36+
[Input("reinstall", "Reinstall the environment if it already exists.")]
3537
[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)
3740
{
41+
// check if referenced Python is installed
3842
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;
3951

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());
4654

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);
4960

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)
5370
{
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);
6378
}
6479

65-
return localEnvironment;
80+
return env;
6681
}
6782
}
6883
}

LadybugTools_Engine/Compute/SimulationResult.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
using System.IO;
3131
using BH.oM.LadybugTools;
3232
using BH.Engine.Serialiser;
33+
using BH.Engine.Base;
3334

3435
namespace BH.Engine.LadybugTools
3536
{
@@ -77,7 +78,7 @@ public static SimulationResult SimulationResult(string epwFile, ILadybugToolsMat
7778

7879
// send to Python to simulate/load
7980
string simulationResultJsonStr = System.Text.RegularExpressions.Regex.Unescape(simulationResult.ToJson());
80-
PythonEnvironment env = Python.Query.VirtualEnv(Query.ToolkitName());
81+
PythonEnvironment env = InstallPythonEnv_LBT(true);
8182
string pythonScript = string.Join("\n", new List<string>()
8283
{
8384
"import json",
@@ -91,6 +92,12 @@ public static SimulationResult SimulationResult(string epwFile, ILadybugToolsMat
9192
});
9293
string output = env.RunPythonString(pythonScript).Trim().Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.None).Last();
9394

95+
if (output.Substring(0, 12).Contains("error"))
96+
{
97+
BH.Engine.Base.Compute.RecordError(Serialiser.Convert.FromJson(output).PropertyValue("error").ToString());
98+
return null;
99+
}
100+
94101
// reload from Python results
95102
return (SimulationResult)Serialiser.Convert.FromJson(output);
96103
}

LadybugTools_Engine/Create/Typology.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static Typology Typology(List<Shelter> shelters = null, string name = "",
7777

7878
return new Typology()
7979
{
80-
Name = name,
80+
Name = rtnName,
8181
Shelters = shelters.Where(s => s != null).ToList(),
8282
EvaporativeCoolingEffect = Enumerable.Repeat(evaporativeCoolingEffect, 8760).ToList(),
8383
WindSpeedMultiplier = Enumerable.Repeat(windSpeedMultiplier, 8760).ToList(),

LadybugTools_Engine/LadybugTools_Engine.csproj

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
44
<PropertyGroup>
@@ -159,16 +159,15 @@
159159
</PropertyGroup>
160160
<PropertyGroup>
161161
<PreBuildEvent>
162-
:: create infrastructure necessary to support Python environments
163162
if not exist "C:\ProgramData\BHoM\Extensions\PythonEnvironments" mkdir "C:\ProgramData\BHoM\Extensions\PythonEnvironments"
164163
if not exist "C:\ProgramData\BHoM\Extensions\PythonCode" mkdir "C:\ProgramData\BHoM\Extensions\PythonCode"
165-
:: remove any old versions within target directory for current toolkits Python code
164+
166165
if exist "C:\ProgramData\BHoM\Extensions\PythonCode\$(SolutionName)" rmdir "C:\ProgramData\BHoM\Extensions\PythonCode\$(SolutionName)" /S /Q
167-
:: move Python code over to toolkit extensions folder, redirecting output to temp log file to silence it
166+
mkdir "C:\ProgramData\BHoM\Extensions\PythonCode\$(SolutionName)"
167+
168168
robocopy "$(ProjectDir)Python" "C:\ProgramData\BHoM\Extensions\PythonCode\$(SolutionName)" /mir /xf "*.pyc" "*.ipynb" /xd "__*__" ".*" &gt; output.log
169-
:: remove temporary log file
170169
del output.log
171-
</PreBuildEvent>
170+
</PreBuildEvent>
172171
</PropertyGroup>
173172
<Import Project="..\packages\RhinoCommon.6.33.20343.16431\build\net45\RhinoCommon.targets" Condition="Exists('..\packages\RhinoCommon.6.33.20343.16431\build\net45\RhinoCommon.targets')" />
174173
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
@@ -177,4 +176,4 @@
177176
</PropertyGroup>
178177
<Error Condition="!Exists('..\packages\RhinoCommon.6.33.20343.16431\build\net45\RhinoCommon.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\RhinoCommon.6.33.20343.16431\build\net45\RhinoCommon.targets'))" />
179178
</Target>
180-
</Project>
179+
</Project>

0 commit comments

Comments
 (0)