|
| 1 | +/* |
| 2 | + * This file is part of the Buildings and Habitats object Model (BHoM) |
| 3 | + * Copyright (c) 2015 - 2023, the respective contributors. All rights reserved. |
| 4 | + * |
| 5 | + * Each contributor holds copyright over their respective contributions. |
| 6 | + * The project versioning (Git) records all such contribution source information. |
| 7 | + * |
| 8 | + * |
| 9 | + * The BHoM is free software: you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU Lesser General Public License as published by |
| 11 | + * the Free Software Foundation, either version 3.0 of the License, or |
| 12 | + * (at your option) any later version. |
| 13 | + * |
| 14 | + * The BHoM is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU Lesser General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Lesser General Public License |
| 20 | + * along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>. |
| 21 | + */ |
| 22 | + |
| 23 | +using BH.oM.Base.Attributes; |
| 24 | +using BH.oM.Python.Enums; |
| 25 | +using System; |
| 26 | +using System.ComponentModel; |
| 27 | +using System.IO; |
| 28 | +using System.Xml.Linq; |
| 29 | + |
| 30 | +namespace BH.Engine.Python |
| 31 | +{ |
| 32 | + public static partial class Compute |
| 33 | + { |
| 34 | + [Description("Download a file from a Url.")] |
| 35 | + [Input("fileUrl", "The file to download.")] |
| 36 | + [Input("targetDirectory", "The destination directory to download the file to.")] |
| 37 | + [Input("filename", "An optional input to rename the downloaded file.")] |
| 38 | + [Input("overwrite", "An optional input to overwrite the file if it already exists, otherwise if it does then return the existing file.")] |
| 39 | + [Output("filePath", "The path to the downloaded file.")] |
| 40 | + public static string DownloadFile( |
| 41 | + string fileUrl, |
| 42 | + string targetDirectory, |
| 43 | + string filename = null, |
| 44 | + bool overwrite = true |
| 45 | + ) |
| 46 | + { |
| 47 | + if (string.IsNullOrWhiteSpace(fileUrl)) |
| 48 | + { |
| 49 | + BH.Engine.Base.Compute.RecordError($"The fileUrl cannot be null or empty."); |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + if (string.IsNullOrWhiteSpace(targetDirectory)) |
| 55 | + { |
| 56 | + BH.Engine.Base.Compute.RecordError($"The targetDirectory cannot be null or empty."); |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + if (!Directory.Exists(targetDirectory)) |
| 62 | + { |
| 63 | + BH.Engine.Base.Compute.RecordError($"The targetDirectory \"{targetDirectory}\" does not exist."); |
| 64 | + return null; |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + if (string.IsNullOrWhiteSpace(filename)) |
| 69 | + filename = Path.GetFileName(fileUrl); |
| 70 | + |
| 71 | + string filePath = Path.Combine(targetDirectory, filename); |
| 72 | + |
| 73 | + if (File.Exists(filePath)) |
| 74 | + { |
| 75 | + if (!overwrite) |
| 76 | + return filePath; |
| 77 | + File.Delete(filePath); |
| 78 | + } |
| 79 | + |
| 80 | + using (var client = new System.Net.WebClient()) |
| 81 | + { |
| 82 | + client.DownloadFile(fileUrl, filePath); |
| 83 | + } |
| 84 | + |
| 85 | + return filePath; |
| 86 | + } |
| 87 | + |
| 88 | + // TODO - THIS METHOD HAS CHANGED BUT IS STILL USED, SO NEEDS DEPRECATING |
| 89 | + // changed from what to what ? |
| 90 | + [Description("Download the target version of Python.")] |
| 91 | + [Input("version", "A Python version.")] |
| 92 | + [Input("name", "Name of target exe file.")] |
| 93 | + [Output("executablePath", "The path of the executable for the downloaded Python.")] |
| 94 | + [PreviousVersion("6.3", "BH.Engine.Python.Compute.DownloadPython(BH.oM.Python.Enums.PythonVersion)")] |
| 95 | + public static string DownloadPython(this PythonVersion version, string name = null) |
| 96 | + { |
| 97 | + string url = version.EmbeddableURL(); |
| 98 | + if (string.IsNullOrEmpty(name)) |
| 99 | + name = Path.GetFileNameWithoutExtension(url); |
| 100 | + string targetExecutable = Path.Combine(Query.DirectoryEnvironments(), name, "python.exe"); |
| 101 | + |
| 102 | + if (File.Exists(targetExecutable)) |
| 103 | + return targetExecutable; |
| 104 | + |
| 105 | + string zipfile = DownloadFile(url, Query.DirectoryEnvironments()); |
| 106 | + UnzipFile(zipfile, Query.DirectoryEnvironments(), name, true); |
| 107 | + |
| 108 | + return targetExecutable; |
| 109 | + } |
| 110 | + |
| 111 | + [Description("Download the pip installer")] |
| 112 | + [Input("targetDirectory", "The directory into which get-pip.py will be downloaded.")] |
| 113 | + [Output("getpipPath", "The path of the file used to install pip into an embedded Python environment.")] |
| 114 | + public static string DownloadGetPip(string targetDirectory) |
| 115 | + { |
| 116 | + return DownloadFile("https://bootstrap.pypa.io/get-pip.py", targetDirectory); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments