Skip to content
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
6 changes: 5 additions & 1 deletion Python_Engine/Compute/BasePythonEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ public static PythonEnvironment BasePythonEnvironment(
bool exists = File.Exists(targetExecutable);

if (exists && reload)
return new PythonEnvironment() { Name = Query.ToolkitName(), Executable = targetExecutable };
{
PythonEnvironment env = new PythonEnvironment() { Name = Query.ToolkitName(), Executable = targetExecutable };
UpdateBHoMPackages(env);
return env;
}

if (exists && !reload)
// remove all existing environments and kernels
Expand Down
110 changes: 110 additions & 0 deletions Python_Engine/Compute/UpdateBHoMPackages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2026, 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;
using BH.oM.Base.Attributes;
using BH.oM.Python;
using BH.oM.Python.Enums;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace BH.Engine.Python
{
public static partial class Compute
{
[Description("Using the environment name (and version if the environment name is the base python environment), directly install/update missing BHoM packages.")]
[Input("environmentName", "The name of the environment to update.")]
[Input("version", "If the environment name is the name of the base python environment, the version that needs updating.")]
public static void UpdateBHoMPackages(this string environmentName, PythonVersion version = PythonVersion.Undefined)
{
//construct the expected PythonEnvironment and pass to UpdateBHoMPackages
PythonEnvironment env = new PythonEnvironment()
{
Name = environmentName,
};

//python version is only used if the environment name is this toolkits name.
if (environmentName == Query.ToolkitName())
{
if (version == PythonVersion.Undefined)
{
BH.Engine.Base.Compute.RecordError("The version must be specified when updating BHoM packages for a base python environment. No updates have occurred.");
return;
}

env.Executable = Path.Combine(Query.DirectoryBaseEnvironment(version), "python.exe");
}
else
env.Executable = Query.VirtualEnvironmentExecutable(environmentName);

UpdateBHoMPackages(env);
}

/***************************************************/

[Description("Using a PythonEnvironment, directly install/update missing BHoM packages.")]
[Input("environment", "The python environment to update.")]
public static void UpdateBHoMPackages(this PythonEnvironment environment)
{
if (!File.Exists(environment.Executable))
{
BH.Engine.Base.Compute.RecordError($"Given environment or base install {environment.Executable} does not exist.");
return;
}

//`python -m pip list -e --format json` lists all environment packages that are editable installs.
//As all BHoM packages are editable installs, this handily lists all the packages that should be updated if necessary.
//side effect of updating other editable installs if they have changed, but I suspect this won't be a problem as anyone who has these would update packages manually instead.
System.Diagnostics.Process process = new System.Diagnostics.Process()
{
StartInfo = new System.Diagnostics.ProcessStartInfo()
{
FileName = environment.Executable,
Arguments = $"-m pip list -e --format json",
UseShellExecute = false,
RedirectStandardOutput = true,
}
};

process.StartInfo.Environment["PYTHONHOME"] = "";
string stdOut;

using (Process p = Process.Start(process.StartInfo))
{
stdOut = p.StandardOutput.ReadToEnd();
p.WaitForExit();
}

stdOut = stdOut.TrimEnd('\r', '\n');

IEnumerable<CustomObject> objs = Serialiser.Convert.FromJsonArray(stdOut).OfType<CustomObject>();

foreach (CustomObject obj in objs)

Check warning on line 106 in Python_Engine/Compute/UpdateBHoMPackages.cs

View workflow job for this annotation

GitHub Actions / ci-code-compliance

Python_Engine/Compute/UpdateBHoMPackages.cs#L106

Python_Engine/Compute/UpdateBHoMPackages.cs#L106: The use of CustomData within the code is discouraged except in circumstances where volatile data is being used. - For more information see https://bhom.xyz/documentation/DevOps/Code%20Compliance%20and%20CI/Compliance%20Checks/IsUsingCustomData
InstallPackageLocal(environment, (string)obj.CustomData["editable_project_location"]);

Check warning on line 107 in Python_Engine/Compute/UpdateBHoMPackages.cs

View check run for this annotation

BHoMBot-CI / code-compliance

Python_Engine/Compute/UpdateBHoMPackages.cs#L106-L107

The use of CustomData within the code is discouraged except in circumstances where volatile data is being used. - For more information see https://bhom.xyz/documentation/DevOps/Code%20Compliance%20and%20CI/Compliance%20Checks/IsUsingCustomData
}
}
}
6 changes: 5 additions & 1 deletion Python_Engine/Compute/VirtualEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ public static PythonEnvironment VirtualEnvironment(this PythonVersion version, s
bool exists = Query.VirtualEnvironmentExists(name);

if (exists && reload)
return new PythonEnvironment() { Name = name, Executable = targetExecutable };
{
PythonEnvironment env = new PythonEnvironment() { Name = name, Executable = targetExecutable };
UpdateBHoMPackages(env);
return env;
}

if (exists && !reload)
{
Expand Down
Loading