From 217aad66e2cb7ec3df2d24d20b394de324e10db3 Mon Sep 17 00:00:00 2001 From: Martin Misol Monzo Date: Tue, 29 Sep 2020 09:51:47 -0400 Subject: [PATCH] Draft Python modules support --- .../PackageManagerClientViewModel.cs | 1 + src/DynamoPackages/Package.cs | 25 +++++++++++++++++++ src/DynamoPackages/PackageManagerClient.cs | 13 +++++----- src/Libraries/DSCPython/CPythonEvaluator.cs | 12 ++++++++- .../MigrationAssistant/ScriptMigrator.cs | 2 +- 5 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/DynamoCoreWpf/ViewModels/PackageManager/PackageManagerClientViewModel.cs b/src/DynamoCoreWpf/ViewModels/PackageManager/PackageManagerClientViewModel.cs index f17e46192cd..dc02e26ae4d 100644 --- a/src/DynamoCoreWpf/ViewModels/PackageManager/PackageManagerClientViewModel.cs +++ b/src/DynamoCoreWpf/ViewModels/PackageManager/PackageManagerClientViewModel.cs @@ -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 { dynPkg }); packageDownloadHandle.DownloadState = PackageDownloadHandle.State.Installed; } diff --git a/src/DynamoPackages/Package.cs b/src/DynamoPackages/Package.cs index f09c4e72f2d..84450dd1e96 100644 --- a/src/DynamoPackages/Package.cs +++ b/src/DynamoPackages/Package.cs @@ -15,6 +15,7 @@ using Dynamo.Utilities; using Greg.Requests; using Newtonsoft.Json; +using Python.Included; using String = System.String; namespace Dynamo.PackageManager @@ -132,6 +133,7 @@ internal IEnumerable NodeLibraries public ObservableCollection LoadedAssemblies { get; private set; } public ObservableCollection LoadedCustomNodes { get; private set; } public ObservableCollection Dependencies { get; private set; } + public ObservableCollection PythonModules { get; set; } public ObservableCollection AdditionalFiles { get; private set; } /// @@ -159,6 +161,7 @@ public Package(string directory, string name, string versionName, string license LoadedCustomNodes = new ObservableCollection(); AdditionalFiles = new ObservableCollection(); Header = PackageUploadBuilder.NewRequestBody(this); + PythonModules = new ObservableCollection(); } public static Package FromDirectory(string rootPath, ILogger logger) @@ -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) @@ -235,6 +246,20 @@ public IEnumerable EnumerateAssemblyFilesInBinDirectory() return Directory.EnumerateFiles(RootDirectory, "*.dll", SearchOption.AllDirectories); } + /// + /// Performs any install-time action required by the package. + /// For now, this only includes the installation of Python modules. + /// + 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); + } + } + /// /// 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. diff --git a/src/DynamoPackages/PackageManagerClient.cs b/src/DynamoPackages/PackageManagerClient.cs index 509cc8ed712..e41c2eb01e5 100644 --- a/src/DynamoPackages/PackageManagerClient.cs +++ b/src/DynamoPackages/PackageManagerClient.cs @@ -137,12 +137,13 @@ internal PackageVersion GetPackageVersionHeader(string id, string version) /// internal IEnumerable GetKnownHosts() { - return FailFunc.TryExecute(() => - { - var hosts = new Hosts(); - var hostsResponse = this.client.ExecuteAndDeserializeWithContent>(hosts); - return hostsResponse.content; - }, new List()); + //return FailFunc.TryExecute(() => + //{ + // var hosts = new Hosts(); + // var hostsResponse = this.client.ExecuteAndDeserializeWithContent>(hosts); + // return hostsResponse.content; + //}, new List()); + return new List(); } internal bool GetTermsOfUseAcceptanceStatus() diff --git a/src/Libraries/DSCPython/CPythonEvaluator.cs b/src/Libraries/DSCPython/CPythonEvaluator.cs index a2072844833..b0bac48d7e6 100644 --- a/src/Libraries/DSCPython/CPythonEvaluator.cs +++ b/src/Libraries/DSCPython/CPythonEvaluator.cs @@ -178,7 +178,7 @@ public static object EvaluatePythonScript( return null; } - Python.Included.Installer.SetupPython().Wait(); + SetupPython(); if (!PythonEngine.IsInitialized) { @@ -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; + } + } + /// /// Creates and initializaes the global Python scope. /// diff --git a/src/PythonMigrationViewExtension/MigrationAssistant/ScriptMigrator.cs b/src/PythonMigrationViewExtension/MigrationAssistant/ScriptMigrator.cs index 675245bea45..c8796b0ba28 100644 --- a/src/PythonMigrationViewExtension/MigrationAssistant/ScriptMigrator.cs +++ b/src/PythonMigrationViewExtension/MigrationAssistant/ScriptMigrator.cs @@ -18,7 +18,7 @@ internal static class ScriptMigrator /// internal static string MigrateCode(string code) { - Installer.SetupPython().Wait(); + Installer.SetupPython(); if (!PythonEngine.IsInitialized) {