diff options
| author | S. Solomon Darnell | 2025-03-28 21:52:21 -0500 |
|---|---|---|
| committer | S. Solomon Darnell | 2025-03-28 21:52:21 -0500 |
| commit | 4a52a71956a8d46fcb7294ac71734504bb09bcc2 (patch) | |
| tree | ee3dc5af3b6313e921cd920906356f5d4febc4ed /.venv/lib/python3.12/site-packages/azure/ai/ml/_local_endpoints/utilities | |
| parent | cc961e04ba734dd72309fb548a2f97d67d578813 (diff) | |
| download | gn-ai-master.tar.gz | |
Diffstat (limited to '.venv/lib/python3.12/site-packages/azure/ai/ml/_local_endpoints/utilities')
3 files changed, 149 insertions, 0 deletions
diff --git a/.venv/lib/python3.12/site-packages/azure/ai/ml/_local_endpoints/utilities/__init__.py b/.venv/lib/python3.12/site-packages/azure/ai/ml/_local_endpoints/utilities/__init__.py new file mode 100644 index 00000000..d540fd20 --- /dev/null +++ b/.venv/lib/python3.12/site-packages/azure/ai/ml/_local_endpoints/utilities/__init__.py @@ -0,0 +1,3 @@ +# --------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# --------------------------------------------------------- diff --git a/.venv/lib/python3.12/site-packages/azure/ai/ml/_local_endpoints/utilities/commandline_utility.py b/.venv/lib/python3.12/site-packages/azure/ai/ml/_local_endpoints/utilities/commandline_utility.py new file mode 100644 index 00000000..3f41e5f0 --- /dev/null +++ b/.venv/lib/python3.12/site-packages/azure/ai/ml/_local_endpoints/utilities/commandline_utility.py @@ -0,0 +1,111 @@ +# --------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# --------------------------------------------------------- + +import json +import os +import subprocess +import sys +import time + +from azure.ai.ml.exceptions import ErrorCategory, ErrorTarget, MlException + + +def _print_command_results(test_passed, time_taken, output): + print("Command {} in {} seconds.".format("successful" if test_passed else "failed", time_taken)) + print("Output: \n{}\n".format(output)) + + +def run_cli_command( + cmd_arguments, + custom_environment=None, + return_json=False, + timeout=None, + do_not_print=True, + stderr_to_stdout=True, +): + if not custom_environment: + custom_environment = os.environ + + # We do this join to construct a command because "shell=True" flag, used below, doesn't work with the vector + # argv form on a mac OS. + command_to_execute = " ".join(cmd_arguments) + + if not do_not_print: # Avoid printing the az login service principal password, for example + print("Preparing to run CLI command: \n{}\n".format(command_to_execute)) + print("Current directory: {}".format(os.getcwd())) + + start_time = time.time() + try: + # We redirect stderr to stdout, so that in the case of an error, especially in negative tests, + # we get the error reply back to check if the error is expected or not. + # We need "shell=True" flag so that the "az" wrapper works. + + # We also pass the environment variables, because for some tests we modify + # the environment variables. + + subprocess_args = { + "shell": True, + "stderr": subprocess.STDOUT, + "env": custom_environment, + } + + if not stderr_to_stdout: + subprocess_args = {"shell": True, "env": custom_environment} + + if sys.version_info[0] != 2: + subprocess_args["timeout"] = timeout + + output = subprocess.check_output(command_to_execute, **subprocess_args).decode(encoding="UTF-8") + + time_taken = time.time() - start_time + if not do_not_print: + _print_command_results(True, time_taken, output) + + if return_json: + try: + return json.loads(exclude_warnings(output)) + except Exception as e: + msg = "Expected JSON, instead got: \n{}\n" + raise MlException( + message=msg.format(output), + no_personal_data_message=msg.format("[something else]"), + target=ErrorTarget.LOCAL_ENDPOINT, + error_category=ErrorCategory.SYSTEM_ERROR, + ) from e + else: + return output + except subprocess.CalledProcessError as e: + time_taken = time.time() - start_time + output = e.output.decode(encoding="UTF-8") + if not do_not_print: + _print_command_results(False, time_taken, output) + + raise e + + +def exclude_warnings(cmd_output): + json_output = "" + start_index = None + end_index = None + curr_index = 0 + for cmd_line in cmd_output.splitlines(): + if cmd_line.startswith("{") and start_index is None: + start_index = curr_index + + if cmd_line.startswith("}"): + end_index = curr_index + + curr_index = curr_index + 1 + + curr_index = 0 + for cmd_line in cmd_output.splitlines(): + if start_index <= curr_index <= end_index: + if len(json_output) == 0: + json_output = cmd_line + else: + json_output = json_output + "\n" + cmd_line + + curr_index = curr_index + 1 + + return json_output diff --git a/.venv/lib/python3.12/site-packages/azure/ai/ml/_local_endpoints/utilities/wsl_utility.py b/.venv/lib/python3.12/site-packages/azure/ai/ml/_local_endpoints/utilities/wsl_utility.py new file mode 100644 index 00000000..33bb6036 --- /dev/null +++ b/.venv/lib/python3.12/site-packages/azure/ai/ml/_local_endpoints/utilities/wsl_utility.py @@ -0,0 +1,35 @@ +# --------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# --------------------------------------------------------- + + +from platform import uname + +from azure.ai.ml._local_endpoints.utilities.commandline_utility import run_cli_command + + +def in_wsl() -> bool: + """WSL is thought to be the only common Linux kernel with Microsoft in the + name, per Microsoft: + + https://github.com/microsoft/WSL/issues/4071#issuecomment-496715404 + + :return: True if running in WSL + :rtype: bool + """ + return "microsoft" in uname().release.lower() + + +def get_wsl_path(path: str) -> str: + """Converts a WSL unix path to a Windows Path + + Input /home/username/ for example. + Output /mnt/c/users/username + + :param path: The UNIX path + :type path: str + :return: A Windows Path + :rtype: str + """ + windows_path = run_cli_command(["wslpath", "-w", path]) + return windows_path |
