Skip to content
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

Added version check to existing environment query #151

Merged
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
44 changes: 15 additions & 29 deletions LadybugTools_Engine/Compute/InstallPythonEnv_LBT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,43 +37,29 @@ public static partial class Compute
[Output("env", "The LadybugTools_Toolkit Python Environment, with BHoM code accessible.")]
public static PythonEnvironment InstallPythonEnv_LBT(bool run = false, bool reinstall = false)
{
// check if referenced Python is installed
string referencedExecutable = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) + @"\ladybug_tools\python\python.exe";

if (!Query.IsPollinationInstalled())
{
return null;
}

if (!run)
return null;

// find out whether this environment already exists
bool exists = Python.Query.VirtualEnvironmentExists(Query.ToolkitName());

if (reinstall)
Python.Compute.RemoveVirtualEnvironment(Query.ToolkitName());

// obtain python version
// check if referenced Python is installed, and get executable and version if it is
if (!Query.IsPollinationInstalled())
return null;
string referencedExecutable = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) + @"\ladybug_tools\python\python.exe";
PythonVersion pythonVersion = Python.Query.Version(referencedExecutable);

// create virtualenvironment
PythonEnvironment env = Python.Compute.VirtualEnvironment(version: pythonVersion, name: Query.ToolkitName(), reload: true);
// check if environment already exists. If it does, and no reinstall requested, load it
bool exists = Python.Query.VirtualEnvironmentExists(envName: Query.ToolkitName(), pythonVersion: pythonVersion);
if (run && exists && !reinstall)
return Python.Compute.VirtualEnvironment(version: pythonVersion, name: Query.ToolkitName(), reload: true);

// return null if environment could not be created/loaded
if (env == null)
return null;
// create environment from scratch
PythonEnvironment env = Python.Compute.VirtualEnvironment(version: pythonVersion, name: Query.ToolkitName(), reload: false);

// install packages if this is a reinstall, or the environment did not originally exist
if (reinstall || !exists)
{
// install local package
env.InstallPackageLocal(Path.Combine(Python.Query.DirectoryCode(), Query.ToolkitName()));
// install local package
env.InstallPackageLocal(Path.Combine(Python.Query.DirectoryCode(), Query.ToolkitName()));

// create requiremetns from referenced executable
string requirementsTxt = Python.Compute.RequirementsTxt(referencedExecutable, Path.Combine(Python.Query.DirectoryEnvironments(), $"requirements_{Query.ToolkitName()}.txt"));
env.InstallRequirements(requirementsTxt);
}
// create requirements from referenced executable
string requirementsTxt = Python.Compute.RequirementsTxt(referencedExecutable, Path.Combine(Python.Query.DirectoryEnvironments(), $"requirements_{Query.ToolkitName()}.txt"));
env.InstallRequirements(requirementsTxt);

return env;
}
Expand Down
4 changes: 2 additions & 2 deletions LadybugTools_Engine/LadybugTools_Engine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@
</ItemGroup>

<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="xcopy &quot;$(ProjectDir)Convert\System.Web.Extensions.dll&quot; &quot;C:\\ProgramData\\BHoM\\Assemblies&quot; /Y&#xD;&#xA;&#xD;&#xA;if not exist &quot;C:\ProgramData\BHoM\Extensions\PythonEnvironments&quot; mkdir &quot;C:\ProgramData\BHoM\Extensions\PythonEnvironments&quot;&#xD;&#xA;if not exist &quot;C:\ProgramData\BHoM\Extensions\PythonCode&quot; mkdir &quot;C:\ProgramData\BHoM\Extensions\PythonCode&quot;&#xD;&#xA; &#xD;&#xA;if exist &quot;C:\ProgramData\BHoM\Extensions\PythonCode\$(SolutionName)&quot; rmdir &quot;C:\ProgramData\BHoM\Extensions\PythonCode\$(SolutionName)&quot; /S /Q&#xD;&#xA;mkdir &quot;C:\ProgramData\BHoM\Extensions\PythonCode\$(SolutionName)&quot;&#xD;&#xA;&#xD;&#xA;robocopy &quot;$(ProjectDir)Python&quot; &quot;C:\ProgramData\BHoM\Extensions\PythonCode\$(SolutionName)&quot; /mir /xf &quot;*.pyc&quot; &quot;*.ipynb&quot; /xd &quot;__*__&quot; &quot;.*&quot; &gt; output.log&#xD;&#xA;del output.log" />
<Exec Command="xcopy $(ProjectDir)Convert\System.Web.Extensions.dll $(ProgramData)\BHoM\Assemblies /Y&#xD;&#xA;&#xD;&#xA;if not exist $(ProgramData)\BHoM\Extensions\PythonEnvironments mkdir $(ProgramData)\BHoM\Extensions\PythonEnvironments&#xD;&#xA;if not exist $(ProgramData)\BHoM\Extensions\PythonCode mkdir $(ProgramData)\BHoM\Extensions\PythonCode&#xD;&#xA; &#xD;&#xA;if exist $(ProgramData)\BHoM\Extensions\PythonCode\$(SolutionName) rmdir $(ProgramData)\BHoM\Extensions\PythonCode\$(SolutionName) /S /Q&#xD;&#xA;mkdir $(ProgramData)\BHoM\Extensions\PythonCode\$(SolutionName)&#xD;&#xA;&#xD;&#xA;robocopy $(ProjectDir)Python $(ProgramData)\BHoM\Extensions\PythonCode\$(SolutionName) /mir /xf &quot;*.pyc&quot; &quot;*.ipynb&quot; /xd &quot;__*__&quot; &quot;.*&quot; &gt; output.log&#xD;&#xA;del output.log" />
</Target>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="xcopy &quot;$(TargetDir)$(TargetFileName)&quot; &quot;C:\\ProgramData\\BHoM\\Assemblies&quot; /Y" />
<Exec Command="xcopy $(TargetDir)$(TargetFileName) $(ProgramData)\BHoM\Assemblies /Y" />
</Target>

</Project>
21 changes: 4 additions & 17 deletions LadybugTools_Engine/Query/IsPollinationInstalled.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ namespace BH.Engine.LadybugTools
public static partial class Query
{
[Description("Return True if Pollination is installed to the currently supported version.")]
[Input("targetPollinationVersion", "The target Pollination version that BHoM currently supports. Default is 1.35.14 which has an uninstaller ProductVersion of 1.38.104, which is the number that is checked against on the uninstaller that comes bundled with Pollincation.")]
[Input("includeBuildNumber", "If true, the build number (the third number in the X.X.X ProductVersion) will be included in the comparison.")]
[Input("targetPollinationVersion", "The target Pollination version that BHoM currently supports.")]
[Output("bool", "True if Pollination is installed to the currently supported version.")]
public static bool IsPollinationInstalled(string targetPollinationVersion = "1.38.104", bool includeBuildNumber = false)
public static bool IsPollinationInstalled(string targetPollinationVersion = "1.35.14")
{
// check if referenced Python is installed
string referencedExecutable = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) + @"\ladybug_tools\python\python.exe";
Expand All @@ -49,26 +48,14 @@ public static bool IsPollinationInstalled(string targetPollinationVersion = "1.3
}

// obtain version of pollination installed
string referencedUninstaller = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) + @"\ladybug_tools\uninstall.exe";
string referencedUninstaller = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) + @"\pollination\uninstall.exe";
FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(referencedUninstaller);
if (includeBuildNumber && (versionInfo.ProductVersion != targetPollinationVersion))
if (versionInfo.ProductVersion != targetPollinationVersion)
{
Base.Compute.RecordError($"Pollination version installed ({versionInfo.ProductVersion}) is not the same as the version required for this code to function correctly ({targetPollinationVersion}).");
return false;
}

// check that version matches the target version
int major = int.Parse(targetPollinationVersion.Split('.')[0]);
int minor = int.Parse(targetPollinationVersion.Split('.')[1]);
if (versionInfo.ProductMajorPart != major || versionInfo.ProductMinorPart != minor)
{
if (versionInfo.ProductVersion != targetPollinationVersion)
{
Base.Compute.RecordError($"Pollination version installed ({versionInfo.ProductVersion}) is not the same as the version required for this code to function correctly ({targetPollinationVersion}).");
return false;
}
}

return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion LadybugTools_oM/LadybugTools_oM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="xcopy &quot;$(TargetDir)$(TargetFileName)&quot; &quot;C:\ProgramData\BHoM\Assemblies&quot; /Y" />
<Exec Command="xcopy $(TargetDir)$(TargetFileName) $(ProgramData)\BHoM\Assemblies /Y" />
</Target>

</Project>
Loading