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

BHoM managed Python virtualenvs #85

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
e4a5c10
Fresh start
tg359 Jul 1, 2022
11f8f77
Functional, untested
tg359 Jul 4, 2022
99798e2
Fixed relative imports for ToBHoM analytics
tg359 Jul 5, 2022
bc44350
Added installation of env code to env, and registered env with ipykernel
tg359 Jul 6, 2022
3968d03
Added ability to install more than one local package to new env
tg359 Jul 6, 2022
75b32eb
Restructured
tg359 Jul 8, 2022
d72cde9
minor updates
tg359 Jul 11, 2022
367dc89
minor updates
tg359 Jul 12, 2022
c3ea73a
NOw working with virtualenvs
tg359 Jul 21, 2022
b49e04d
updates
tg359 Jul 22, 2022
3412df6
Modified Python dir
tg359 Jul 27, 2022
021450e
minor fixes
tg359 Aug 2, 2022
4580209
Updating Python venv creation and framework
tg359 Aug 4, 2022
65c8f7c
Updated unit test location
tg359 Aug 14, 2022
8d95a29
Merge branch 'main' of https://github.com/BHoM/Python_Toolkit into Py…
tg359 Aug 14, 2022
e04fac0
Added versioning info for UIs
tg359 Aug 15, 2022
bd97891
fixed csproj
tg359 Aug 15, 2022
1ec0dab
fixed for code-compliance
tg359 Aug 15, 2022
07d7f9f
fixed for *-compliance
tg359 Aug 15, 2022
3be0c0d
added timeout to Run methods and fixed formatting
tg359 Aug 19, 2022
40726dd
updated installer method name
tg359 Aug 25, 2022
db05ce7
Fixed error returning null instead of existing env
tg359 Sep 7, 2022
7296b77
commit for posterity before getting signatures of main branch
tg359 Sep 8, 2022
0f102ff
Updated versioning.json
tg359 Sep 8, 2022
8e77c94
updated versioning.json
tg359 Sep 8, 2022
7002619
updated versioning.json
tg359 Sep 8, 2022
96121b9
versioning fun
tg359 Sep 8, 2022
3f1d5a6
versioning fun
tg359 Sep 8, 2022
9eeb7b2
more verisoning fun
tg359 Sep 8, 2022
841bf5c
Updated copyright for compliance
tg359 Sep 9, 2022
b0d4446
Readded Python.RunTime for versioning compliance
tg359 Sep 9, 2022
8a21c1e
Implemented valid Invoke methods for versioning compliance
tg359 Sep 9, 2022
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
Empty file.
58 changes: 0 additions & 58 deletions Python_Engine/Compute/DownloadFile.cs

This file was deleted.

55 changes: 29 additions & 26 deletions Python_Engine/Compute/DownloadPython.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,46 @@
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using System;
using BH.oM.Base.Attributes;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Python.Runtime;

namespace BH.Engine.Python
{
public static partial class Compute
{
/***************************************************/
/**** Public Methods ****/
/***************************************************/

public static bool DownloadPython()
[Description("Download a target version of Python.")]
[Input("version", "A Python version.")]
[Output("executablePath", "The path of the executable for the downloaded Python.")]
[PreviousVersion("5.3", "BH.Engine.Python.Compute.DownloadPython()")]
public static string DownloadPython(this BH.oM.Python.Enums.PythonVersion version)
{
string home = Query.EmbeddedPythonHome();
if (!Directory.Exists(home))
Directory.CreateDirectory(home);

string destination = Path.Combine(home, $"python.zip");
RunCommand($"curl {EMBEDDED_PYTHON_URL} -o {destination}");
return true;
}
string targetDirectory = Query.EnvironmentsDirectory();
string versionString = version.ToString().Replace("v", "");
string resultantDirectory = Path.Combine(targetDirectory, versionString);
string executable = Path.Combine(resultantDirectory, "python.exe");

if (File.Exists(executable))
return executable;

/***************************************************/
/**** Public Fields ****/
/***************************************************/
string pythonUrl = version.EmbeddableURL();
string pythonZipFile = Path.Combine(targetDirectory, Path.GetFileName(pythonUrl));

public const string EMBEDDED_PYTHON_URL = @"https://www.python.org/ftp/python/3.7.3/python-3.7.3-embed-amd64.zip";
List<string> commands = new List<string>()
{
$"curl {pythonUrl} -o {pythonZipFile}",
$"mkdir {resultantDirectory}",
$"tar -v -xf {pythonZipFile} -C {resultantDirectory}",
$"del {pythonZipFile}",
};
foreach (string command in commands)
{
RunCommandStdout(command, hideWindows: true);
}

/***************************************************/
return executable;
}
}
}


69 changes: 0 additions & 69 deletions Python_Engine/Compute/Init.cs

This file was deleted.

119 changes: 119 additions & 0 deletions Python_Engine/Compute/InstallBasePythonEnvironment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2022, 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.Attributes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;

namespace BH.Engine.Python
{
public static partial class Compute
{
[Description("Install the base Python Environment for BHoM workflows.")]
[Input("run", "Run the installation process for the BHoM Python Environment.")]
[Output("env", "The base Python Environment for BHoM workflows.")]
public static oM.Python.PythonEnvironment InstallBasePythonEnvironment(bool run = false)
{
if (!run)
{
BH.Engine.Base.Compute.RecordWarning($"Please toggle `{nameof(run)}` to true.");
return null;
}

// Python version for base environment
oM.Python.Enums.PythonVersion version = oM.Python.Enums.PythonVersion.v3_10_5;

// set location where base Python env will be created
string targetDirectory = Path.Combine(Query.EnvironmentsDirectory(), Query.ToolkitName());
if (!Directory.Exists(targetDirectory))
Directory.CreateDirectory(targetDirectory);

// set executable path for later use in installation
string executable = Path.Combine(targetDirectory, "python.exe");
oM.Python.PythonEnvironment env = new oM.Python.PythonEnvironment() { Name = Query.ToolkitName(), Executable = executable };

if (env.EnvironmentExists())
return env;

// create log of installation as process continues - useful for debugging if things go wrong!
string logFile = Path.Combine(targetDirectory, "installation.log");

// prepare constants for use in installation process
string pythonUrl = version.EmbeddableURL();
string pythonZipFile = Path.Combine(targetDirectory, "embeddable_python.zip");
string pipInstallerUrl = "https://bootstrap.pypa.io/get-pip.py";
string pipInstallerFile = Path.Combine(targetDirectory, "get-pip.py");

using (StreamWriter sw = File.AppendText(logFile))
{
sw.WriteLine(LoggingHeader("Installation started for BHoM base Python environment"));

// download and unpack files
List<string> installationCommands = new List<string>() {
$"curl {pythonUrl} -o {pythonZipFile}", // download Python zip file
$"tar -v -xf {pythonZipFile} -C {targetDirectory}", // unzip files
$"del {pythonZipFile}", // remove zip file
$"curl {pipInstallerUrl} -o {pipInstallerFile}", // download PIP installer
$"{executable} {pipInstallerFile} --no-warn-script-location", // install PIP
$"del {pipInstallerFile}", // remove PIP installer file
};

foreach (string command in installationCommands)
{
sw.WriteLine($"[{System.DateTime.Now.ToString("s")}] {command}");
sw.WriteLine(Compute.RunCommandStdout($"{command}", hideWindows: true));
}

// modify directory to replicate "normal" Python installation
List<string> pthFiles = System.IO.Directory.GetFiles(targetDirectory, "*.*", SearchOption.TopDirectoryOnly).Where(s => s.EndsWith("._pth")).ToList();
string libDirectory = System.IO.Directory.CreateDirectory(Path.Combine(targetDirectory, "DLLs")).FullName;
List<string> libFiles = System.IO.Directory.GetFiles(targetDirectory, "*.*", SearchOption.TopDirectoryOnly).Where(s => (s.EndsWith(".dll") || s.EndsWith(".pyd")) && !Path.GetFileName(s).Contains("python") && !Path.GetFileName(s).Contains("vcruntime")).ToList();

List<string> modificationCommands = new List<string>() { };
foreach (string pthFile in pthFiles)
{
modificationCommands.Add($"del {pthFile}"); // delete *._pth file/s
}
foreach (string libFile in libFiles)
{
modificationCommands.Add($"move /y {libFile} {libDirectory}"); // move specific *._pyd and *.dll file/s into DLLs directory
}

foreach (string command in modificationCommands)
{
sw.WriteLine($"[{System.DateTime.Now.ToString("s")}] {command}");
sw.WriteLine(Compute.RunCommandStdout($"{command}", hideWindows: true));
}

// install packages into base environment
string packageInstallationCommand = $"{executable} -m pip install --no-warn-script-location -e {Path.Combine(Query.CodeDirectory(), Query.ToolkitName())}";
sw.WriteLine($"[{System.DateTime.Now.ToString("s")}] {packageInstallationCommand}");
sw.WriteLine(Compute.RunCommandStdout($"{packageInstallationCommand}", hideWindows: true)); // install packages into this environment
}

return env;
}
}
}
59 changes: 0 additions & 59 deletions Python_Engine/Compute/InstallPip.cs

This file was deleted.

Loading