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

Draft Python modules support #11152

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,7 @@ internal void SetPackageState(PackageDownloadHandle packageDownloadHandle, strin
Package dynPkg;
if (packageDownloadHandle.Extract(DynamoViewModel.Model, downloadPath, out dynPkg))
{
dynPkg.Install();
PackageManagerExtension.PackageLoader.LoadPackages(new List<Package> { dynPkg });
packageDownloadHandle.DownloadState = PackageDownloadHandle.State.Installed;
}
Expand Down
25 changes: 25 additions & 0 deletions src/DynamoPackages/Package.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Dynamo.Utilities;
using Greg.Requests;
using Newtonsoft.Json;
using Python.Included;
using String = System.String;

namespace Dynamo.PackageManager
Expand Down Expand Up @@ -132,6 +133,7 @@ internal IEnumerable<Assembly> NodeLibraries
public ObservableCollection<PackageAssembly> LoadedAssemblies { get; private set; }
public ObservableCollection<CustomNodeInfo> LoadedCustomNodes { get; private set; }
public ObservableCollection<PackageDependency> Dependencies { get; private set; }
public ObservableCollection<PackageDependency> PythonModules { get; set; }
public ObservableCollection<PackageFileInfo> AdditionalFiles { get; private set; }

/// <summary>
Expand Down Expand Up @@ -159,6 +161,7 @@ public Package(string directory, string name, string versionName, string license
LoadedCustomNodes = new ObservableCollection<CustomNodeInfo>();
AdditionalFiles = new ObservableCollection<PackageFileInfo>();
Header = PackageUploadBuilder.NewRequestBody(this);
PythonModules = new ObservableCollection<PackageDependency>();
}

public static Package FromDirectory(string rootPath, ILogger logger)
Expand Down Expand Up @@ -197,6 +200,14 @@ public static Package FromJson(string headerPath, ILogger logger)
foreach (var dep in body.dependencies)
pkg.Dependencies.Add(dep);

if (body.python_modules != null)
{
foreach (var mod in body.python_modules)
{
pkg.PythonModules.Add(mod);
}
}

return pkg;
}
catch (Exception e)
Expand Down Expand Up @@ -235,6 +246,20 @@ public IEnumerable<string> EnumerateAssemblyFilesInBinDirectory()
return Directory.EnumerateFiles(RootDirectory, "*.dll", SearchOption.AllDirectories);
}

/// <summary>
/// Performs any install-time action required by the package.
/// For now, this only includes the installation of Python modules.
/// </summary>
public void Install()
{
Installer.SetupPython();
foreach (var module in PythonModules)
{
// TODO: Can we check the version of the installed module? Reinstall if needed?
Installer.PipInstallModule(module.name);
}
}

/// <summary>
/// Add assemblies at runtime to the package. Does not load the assembly into the node library.
/// If the package is already present in LoadedAssemblies, this will mutate it's IsNodeLibrary property.
Expand Down
13 changes: 7 additions & 6 deletions src/DynamoPackages/PackageManagerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,13 @@ internal PackageVersion GetPackageVersionHeader(string id, string version)
/// </summary>
internal IEnumerable<string> GetKnownHosts()
{
return FailFunc.TryExecute(() =>
{
var hosts = new Hosts();
var hostsResponse = this.client.ExecuteAndDeserializeWithContent<List<String>>(hosts);
return hostsResponse.content;
}, new List<string>());
//return FailFunc.TryExecute(() =>
//{
// var hosts = new Hosts();
// var hostsResponse = this.client.ExecuteAndDeserializeWithContent<List<String>>(hosts);
// return hostsResponse.content;
//}, new List<string>());
return new List<string>();
}

internal bool GetTermsOfUseAcceptanceStatus()
Expand Down
12 changes: 11 additions & 1 deletion src/Libraries/DSCPython/CPythonEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public static object EvaluatePythonScript(
return null;
}

Python.Included.Installer.SetupPython().Wait();
SetupPython();

if (!PythonEngine.IsInitialized)
{
Expand Down Expand Up @@ -246,6 +246,16 @@ public static object EvaluatePythonScript(
}
}

private static bool isPythonSetup = false;
private static void SetupPython()
{
if (!isPythonSetup)
{
Python.Included.Installer.SetupPython();
isPythonSetup = true;
}
}

/// <summary>
/// Creates and initializaes the global Python scope.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal static class ScriptMigrator
/// <returns></returns>
internal static string MigrateCode(string code)
{
Installer.SetupPython().Wait();
Installer.SetupPython();

if (!PythonEngine.IsInitialized)
{
Expand Down