From 33299c9f00eada9752d298722388dd9110449033 Mon Sep 17 00:00:00 2001 From: "J.P" <70083705+stapmoshun@users.noreply.github.com> Date: Sat, 13 Jan 2024 19:23:25 -0500 Subject: [PATCH] optimize chocolatey installer --- src/cloudmesh/common/Shell.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/cloudmesh/common/Shell.py b/src/cloudmesh/common/Shell.py index 8f94e97..93fdd37 100755 --- a/src/cloudmesh/common/Shell.py +++ b/src/cloudmesh/common/Shell.py @@ -621,8 +621,27 @@ def install_chocolatey(): # Join the script directory with "bin" bin_directory = os.path.join(script_directory, "bin") - print(f"Looking in {bin_directory} for install script...") + print(f"Looking in {bin_directory} for install script...") + # Check if the bin_directory exists + if not os.path.exists(bin_directory): + Console.info("dir does not exist? downloading script...") + # Create the directories if they don't exist + os.makedirs(bin_directory, exist_ok=True) + + # If it doesn't exist, download the script from the URL + url = "https://raw.githubusercontent.com/cloudmesh/cloudmesh-common/main/src/cloudmesh/common/bin/win-setup.bat" + response = requests.get(url) + + # Check if the request was successful + if response.status_code == 200: + # If it was, save the script to a file + with open(rf"{bin_directory}\win-setup.bat", "w") as file: + file.write(response.text) + else: + # If the request was not successful, print an error message and return + Console.error("Failed to download the script.") + return False # Command to install Chocolatey using the Command Prompt chocolatey_install_command = rf"powershell Start-Process -Wait -FilePath {bin_directory}\win-setup.bat" print(chocolatey_install_command) @@ -639,6 +658,12 @@ def install_chocolatey(): "You are currently standing in a non-existent directory." ) return False + # Check if the command failed + if completed_process.returncode != 0: + # If it failed, print an error message and return False + Console.error(f"Failed: {completed_process.stderr}") + return False + print(completed_process) Console.ok("Chocolatey installed") return True