Skip to content

[DYN-8392] Python dependencies in workspace references. #15948

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
Apr 1, 2025
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
48 changes: 41 additions & 7 deletions src/DynamoCore/Graph/Workspaces/WorkspaceModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using Dynamo.Logging;
using Dynamo.Models;
using Dynamo.Properties;
using Dynamo.PythonServices;
using Dynamo.Scheduler;
using Dynamo.Selection;
using Dynamo.Utilities;
Expand Down Expand Up @@ -672,20 +673,19 @@ public HashSet<Guid> Dependencies
}

/// <summary>
/// Event requesting subscribers to return additional package dependencies for
/// current workspace.
/// Event requesting subscribers to return Python engine mapping for the current workspace nodes.
/// </summary>
internal event Func<IEnumerable<INodeLibraryDependencyInfo>> RequestPackageDependencies;
internal event Func<Dictionary<Guid, String>> RequestPythonEngineMapping;

/// <summary>
/// Raised when the workspace needs to request for additional package dependencies
/// Raised when the workspace needs to request for Python engine mapping
/// that can be returned from other subscribers such as view extensions.
/// E.g. The PythonMigrationViewExtension returns additional package dependencies required for Python engines.
/// E.g. The PythonMigrationViewExtension computes additional package dependencies required for Python nodes.
/// </summary>
/// <returns></returns>
internal IEnumerable<INodeLibraryDependencyInfo> OnRequestPackageDependencies()
internal Dictionary<Guid, String> OnRequestPythonEngineMapping()
{
return RequestPackageDependencies?.Invoke();
return RequestPythonEngineMapping?.Invoke();
}

/// <summary>
Expand Down Expand Up @@ -800,10 +800,44 @@ private List<INodeLibraryDependencyInfo> ComputeNodeLibraryDependencies()
{
var packageDependencies = new Dictionary<PackageInfo, PackageDependencyInfo>();

bool computePythonNodeMapping = true;
var pythonNodeMapping = new Dictionary<Guid, String>();

foreach (var node in Nodes)
{
var collected = GetNodePackage(node);

// Handle python nodes explicitly and use the collected node package for those node types.
if (node.ToString().Equals(PythonEngineManager.PythonNodeNamespace))
{
// Compute the node - python engine mapping for all python workspace nodes at once, when a python node is detected.
if (computePythonNodeMapping)
{
pythonNodeMapping = OnRequestPythonEngineMapping();
computePythonNodeMapping = false;
}

var pythonEngine = pythonNodeMapping[node.GUID];

// Since CPython3 is a default inbuilt python engine,no package dependency is set for that node.
if (pythonEngine.Equals(PythonEngineManager.CPython3EngineName))
{
continue;
}
else if (collected != null)
{
if (!packageDependencies.ContainsKey(collected))
{
packageDependencies[collected] = new PackageDependencyInfo(collected);
}
packageDependencies[collected].AddDependent(node.GUID);
packageDependencies[collected].State = PackageDependencyState.Loaded;

nodePackageDictionary[node.GUID] = collected;
continue;
}
}

if (nodePackageDictionary.ContainsKey(node.GUID))
{
var saved = nodePackageDictionary[node.GUID];
Expand Down
9 changes: 3 additions & 6 deletions src/Libraries/PythonNodeModels/PythonNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,10 @@ public string EngineName
/// <returns>Assembly Name</returns>
internal override AssemblyName GetNameOfAssemblyReferencedByNode()
{
if (NameOfAssemblyReferencedByNode == null)
var pyEng = PythonEngineManager.Instance.AvailableEngines.Where(x => x.Name.Equals(this.EngineName)).FirstOrDefault();
if (pyEng != null)
{
var pyEng = PythonEngineManager.Instance.AvailableEngines.Where(x => x.Name.Equals(this.EngineName)).FirstOrDefault();
if (pyEng != null)
{
NameOfAssemblyReferencedByNode = pyEng.GetType().Assembly.GetName();
}
NameOfAssemblyReferencedByNode = pyEng.GetType().Assembly.GetName();
}

return NameOfAssemblyReferencedByNode;
Expand Down
2 changes: 2 additions & 0 deletions src/NodeServices/PythonServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ internal static readonly Lazy<PythonEngineManager>
/// </summary>
internal static readonly string CPython3EngineName = "CPython3";

internal static readonly string PythonNodeNamespace = "PythonNodeModels.PythonNode";

/// <summary>
/// IronPython2 Engine name
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions src/PythonMigrationViewExtension/GraphPythonDependencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ internal IEnumerable<INodeLibraryDependencyInfo> AddPythonPackageDependency()
return new[] { packageDependencyInfo };
}

// Returns a dictionary of node and python engine mapping for the workspace python nodes.
internal Dictionary<Guid, String> GetPythonEngineMapping()
{
var pythonNodeMapping = new Dictionary<Guid, String>();

foreach (var node in workspace.Nodes.OfType<PythonNode>())
{
pythonNodeMapping.Add(node.GUID, node.EngineName);
}

return pythonNodeMapping;
}

/// <summary>
/// This recursive function returns true if any of the custom nodes in the input list has an IronPython dependency.
/// Any custom nodes in the input list traversed during evaluation have their dependencies cached in <see cref="CustomNodePythonDependencyMap"/>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void Loaded(ViewLoadedParams p)
DynamoViewModel = LoadedParams.DynamoWindow.DataContext as DynamoViewModel;
CurrentWorkspace = LoadedParams.CurrentWorkspaceModel as WorkspaceModel;
CustomNodeManager = (CustomNodeManager)LoadedParams.StartupParams.CustomNodeManager;
CurrentWorkspace.RequestPackageDependencies += PythonDependencies.AddPythonPackageDependency;
CurrentWorkspace.RequestPythonEngineMapping += PythonDependencies.GetPythonEngineMapping;
Dispatcher = Dispatcher.CurrentDispatcher;

SubscribeToDynamoEvents();
Expand Down Expand Up @@ -181,7 +181,7 @@ private void SubscribeToWorkspaceEvents()
{
CurrentWorkspace.NodeAdded += OnNodeAdded;
CurrentWorkspace.NodeRemoved += OnNodeRemoved;
CurrentWorkspace.RequestPackageDependencies += PythonDependencies.AddPythonPackageDependency;
CurrentWorkspace.RequestPythonEngineMapping += PythonDependencies.GetPythonEngineMapping;
}

private void SubscribeToPythonNodeEvents(PythonNodeBase node)
Expand All @@ -198,7 +198,7 @@ private void UnSubscribeWorkspaceEvents()
{
if (CurrentWorkspace != null)
{
CurrentWorkspace.RequestPackageDependencies -= PythonDependencies.AddPythonPackageDependency;
CurrentWorkspace.RequestPythonEngineMapping -= PythonDependencies.GetPythonEngineMapping;
CurrentWorkspace.NodeAdded -= OnNodeAdded;
CurrentWorkspace.NodeRemoved -= OnNodeRemoved;
CurrentWorkspace.Nodes
Expand All @@ -220,7 +220,7 @@ private void UnsubscribeEvents()

if (CurrentWorkspace != null)
{
CurrentWorkspace.RequestPackageDependencies -= PythonDependencies.AddPythonPackageDependency;
CurrentWorkspace.RequestPythonEngineMapping -= PythonDependencies.GetPythonEngineMapping;
}
UnSubscribeWorkspaceEvents();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,6 @@ internal void DependencyRegen(WorkspaceModel ws, bool forceCompute = false)
HasDependencyIssue = string.IsNullOrEmpty(info.Path);
}

var pythonPackageDependencies = ws.OnRequestPackageDependencies();
Copy link
Contributor Author

@reddyashish reddyashish Mar 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this as the OnRequestPackageDependencies was just setting the ironpython package details with a fixed version if the workspace has a ironpython2 node. Now, we will compute the dependency package directly when the engine is changed and the script is saved. Should handle Cpython and pythonnet3 cases as well.

if (pythonPackageDependencies != null)
packageDependencies.AddRange(pythonPackageDependencies);

if (packageDependencies.Any(d => d.State != PackageDependencyState.Loaded))
{
HasDependencyIssue = true;
Expand Down
10 changes: 8 additions & 2 deletions test/DynamoCoreTests/PackageDependencyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,18 @@ public void PythonEnginePackageDependencyIsCollectedAndSerialized()
Assert.IsNotNull(pyNode);
Assert.AreEqual(pyNode.EngineName, PythonEngineManager.CPython3EngineName);
Assert.AreEqual(PythonEngineManager.Instance.AvailableEngines.Count, 2);
UpdatePythonEngineAndRun(pyNode, "PythonNet3");

currentws.ForceComputeWorkspaceReferences = true;
var packageDependencies = currentws.NodeLibraryDependencies;
// Since Cpython is inbuilt and not loaded as a package, it shouldn't show up in package dependencies.
Assert.AreEqual(0, packageDependencies.Count);

// Change engine to PythonNet3, which is loaded as a package.
UpdatePythonEngineAndRun(pyNode, "PythonNet3");
currentws.ForceComputeWorkspaceReferences = true;

//assert that python engine imported from a package gets added to NodeLibraryDependencies
var packageDependencies = currentws.NodeLibraryDependencies;
packageDependencies = currentws.NodeLibraryDependencies;
Assert.AreEqual(1, packageDependencies.Count);
var package = packageDependencies.First();
Assert.AreEqual(new PackageDependencyInfo("TestCP311", new Version("1.0.8")), package);
Expand Down
Loading