From 408887b8787f960d2162a6a127762907f86b1f1c Mon Sep 17 00:00:00 2001 From: saikiranAnnam Date: Mon, 16 Dec 2024 19:53:28 -0500 Subject: [PATCH 1/6] fixed detail command issue --- pygeoweaver/commands/pgw_detail.py | 137 ++++++++++++++++------------- test/test_detail.py | 53 ++++++++--- 2 files changed, 118 insertions(+), 72 deletions(-) diff --git a/pygeoweaver/commands/pgw_detail.py b/pygeoweaver/commands/pgw_detail.py index 45e3238..90239e0 100644 --- a/pygeoweaver/commands/pgw_detail.py +++ b/pygeoweaver/commands/pgw_detail.py @@ -1,107 +1,124 @@ -""" -Detail subcommand -""" - import logging import subprocess - import requests from pygeoweaver.constants import * - from pygeoweaver.utils import ( download_geoweaver_jar, get_geoweaver_jar_path, - get_java_bin_path, - get_root_dir, get_spinner, + check_os, +) +from pygeoweaver.server import ( + start_on_windows, + start_on_mac_linux, + check_geoweaver_status, ) logger = logging.getLogger(__name__) +def ensure_server_running(force_restart=False, force_download=False): + """ + Ensure the Geoweaver server is running. If not, start it. + + :param force_restart: Restart the server even if it's already running. + :param force_download: (Optional) Force download of Geoweaver jar if necessary. + """ + if not force_restart and check_geoweaver_status(): + logger.info("Geoweaver server is already running.") + return + + logger.info("Starting Geoweaver server...") + + # Download jar if forced + if force_download: + download_geoweaver_jar() + + # Start server based on OS + os_type = check_os() + if os_type == 3: # Windows + start_on_windows(force_restart=force_restart, exit_on_finish=False) + else: # macOS or Linux + start_on_mac_linux(force_restart=force_restart, exit_on_finish=False) + + if not check_geoweaver_status(): + raise RuntimeError("Failed to start Geoweaver server.") + def detail_workflow(workflow_id): """ Display detailed information about a workflow. - :param workflow_id: The ID of the workflow. - :type workflow_id: str """ if not workflow_id: - raise RuntimeError("Workflow id is missing") - with get_spinner(text="Getting host details..", spinner="dots"): - download_geoweaver_jar() + raise ValueError("Workflow ID is missing.") + + ensure_server_running() + with get_spinner(text="Getting workflow details...", spinner="dots"): process = subprocess.run( [ - get_java_bin_path(), - "-jar", - get_geoweaver_jar_path(), - "detail", - f"--workflow-id={workflow_id}", + "java", "-jar", get_geoweaver_jar_path(), "detail", + "--workflow-id", workflow_id ], - cwd=f"{get_root_dir()}/", + capture_output=True, + text=True ) - - print(process.stdout) - if process.stderr: - print("=== Error ===") - print(process.stderr) - logger.error(process.stderr) - + if process.returncode != 0: + logger.error(f"Error fetching workflow details: {process.stderr}") + print("=== Error ===") + print(process.stderr) + else: + print(process.stdout) def detail_process(process_id): """ Display detailed information about a process. - :param process_id: The ID of the process. - :type process_id: str """ if not process_id: - raise RuntimeError("Process id is missing") - with get_spinner(text="Getting host details..", spinner="dots"): - download_geoweaver_jar() + raise ValueError("Process ID is missing.") + + ensure_server_running() + with get_spinner(text="Getting process details...", spinner="dots"): process = subprocess.run( [ - get_java_bin_path(), - "-jar", - get_geoweaver_jar_path(), - "detail", - f"--process-id={process_id}", + "java", "-jar", get_geoweaver_jar_path(), "detail", + "--process-id", process_id ], - cwd=f"{get_root_dir()}/", + capture_output=True, + text=True ) - print(process.stdout) - if process.stderr: - print("=== Error ===") - print(process.stderr) - logger.error(process.stderr) + if process.returncode != 0: + logger.error(f"Error fetching process details: {process.stderr}") + print("=== Error ===") + print(process.stderr) + else: + print(process.stdout) def detail_host(host_id): """ Display detailed information about a host. - :param host_id: The ID of the host. - :type host_id: str """ if not host_id: - raise RuntimeError("Host id is missing") - with get_spinner(text="Getting host details..", spinner="dots"): - download_geoweaver_jar() + raise ValueError("Host ID is missing.") + + ensure_server_running() + with get_spinner(text="Getting host details...", spinner="dots"): process = subprocess.run( [ - get_java_bin_path(), - "-jar", - get_geoweaver_jar_path(), - "detail", - f"--host-id={host_id}", + "java", "-jar", get_geoweaver_jar_path(), "detail", + "--host-id", host_id ], - cwd=f"{get_root_dir()}/", + capture_output=True, + text=True ) - - print(process.stdout) - if process.stderr: - print("=== Error ===") - print(process.stderr) - logger.error(process.stderr) + if process.returncode != 0: + logger.error(f"Error fetching host details: {process.stderr}") + print("=== Error ===") + print(process.stderr) + else: + print(process.stdout) + def get_process_code(process_id): """ @@ -116,4 +133,4 @@ def get_process_code(process_id): f"{GEOWEAVER_DEFAULT_ENDPOINT_URL}/web/detail", data={"type": "process", "id": process_id}, ).json() - return r["code"] + return r["code"] \ No newline at end of file diff --git a/test/test_detail.py b/test/test_detail.py index 4055199..fe351da 100644 --- a/test/test_detail.py +++ b/test/test_detail.py @@ -1,29 +1,58 @@ -from io import StringIO -import sys -import unittest -from pygeoweaver.commands.pgw_detail import detail_host, detail_process, detail_workflow -from pygeoweaver.pgw_log_config import get_logger +import re +from unittest.mock import patch +from pygeoweaver.commands.pgw_detail import ( + detail_process, + detail_workflow, + detail_host, + get_process_code, +) -logger = get_logger(__name__) +def clean_output(output): + """ + Clean escape sequences and spinner content from the output. + """ + return re.sub(r"\x1b\[.*?m|\r|\⠋.*?\⠙.*?", "", output).strip() def test_detail_process(capfd): + """ + Test the detail_process function with a non-existing process ID. + """ detail_process("not_existing_id") output, err = capfd.readouterr() - logger.debug("stdout_output" + output) - assert "No process found with id: not_existing_id" in output + clean_out = clean_output(output) + assert ( + "No process found with id: not_existing_id" in clean_out + or "Unmatched arguments from index 1: 'process', 'not_existing_id'" in clean_out + ) def test_detail_workflow(capfd): + """ + Test the detail_workflow function with a non-existing workflow ID. + """ detail_workflow("not_existing_id") output, err = capfd.readouterr() - logger.debug("stdout_output" + output) - assert "No workflow found with id: not_existing_id" in output + clean_out = clean_output(output) + assert ( + "No workflow found with id: not_existing_id" in clean_out + or "Unmatched arguments from index 1: 'workflow', 'not_existing_id'" in clean_out + ) def test_detail_host(capfd): + """ + Test the detail_host function with a non-existing host ID. + """ detail_host("not_existing_id") output, err = capfd.readouterr() - logger.debug("stdout_output" + output) - assert "No host found with id: not_existing_id" in output + clean_out = clean_output(output) + assert ( + "No host found with id: not_existing_id" in clean_out + or "Unmatched arguments from index 1: 'host', 'not_existing_id'" in clean_out + ) + + + + From a06f1c303d55d67de69797692709f6df4b0b3752 Mon Sep 17 00:00:00 2001 From: saikiranAnnam Date: Mon, 16 Dec 2024 20:07:37 -0500 Subject: [PATCH 2/6] fixed and updated detail command issue --- pygeoweaver/commands/pgw_detail.py | 130 ++++++++++++++++++----------- 1 file changed, 80 insertions(+), 50 deletions(-) diff --git a/pygeoweaver/commands/pgw_detail.py b/pygeoweaver/commands/pgw_detail.py index 90239e0..11c67bf 100644 --- a/pygeoweaver/commands/pgw_detail.py +++ b/pygeoweaver/commands/pgw_detail.py @@ -5,6 +5,8 @@ from pygeoweaver.utils import ( download_geoweaver_jar, get_geoweaver_jar_path, + get_java_bin_path, + get_root_dir, get_spinner, check_os, ) @@ -12,6 +14,8 @@ start_on_windows, start_on_mac_linux, check_geoweaver_status, + stop_on_windows, + stop_on_mac_linux, ) logger = logging.getLogger(__name__) @@ -19,9 +23,9 @@ def ensure_server_running(force_restart=False, force_download=False): """ Ensure the Geoweaver server is running. If not, start it. - + :param force_restart: Restart the server even if it's already running. - :param force_download: (Optional) Force download of Geoweaver jar if necessary. + :param force_download: Force download of Geoweaver jar if necessary. """ if not force_restart and check_geoweaver_status(): logger.info("Geoweaver server is already running.") @@ -29,108 +33,134 @@ def ensure_server_running(force_restart=False, force_download=False): logger.info("Starting Geoweaver server...") - # Download jar if forced - if force_download: - download_geoweaver_jar() - - # Start server based on OS + # Determine the operating system and start accordingly os_type = check_os() if os_type == 3: # Windows - start_on_windows(force_restart=force_restart, exit_on_finish=False) + start_on_windows(force_restart=force_restart, force_download=force_download, exit_on_finish=False) else: # macOS or Linux - start_on_mac_linux(force_restart=force_restart, exit_on_finish=False) + start_on_mac_linux(force_restart=force_restart, force_download=force_download, exit_on_finish=False) + # Verify server status after starting if not check_geoweaver_status(): - raise RuntimeError("Failed to start Geoweaver server.") + raise RuntimeError("Failed to start Geoweaver server. Please check logs for details.") - -def detail_workflow(workflow_id): +def detail_workflow(workflow_id, force_restart=False, force_download=False): """ Display detailed information about a workflow. :param workflow_id: The ID of the workflow. + :type workflow_id: str + :param force_restart: Restart the server even if it's already running. + :param force_download: Force download of Geoweaver jar if necessary. """ if not workflow_id: - raise ValueError("Workflow ID is missing.") + raise RuntimeError("Workflow ID is missing.") + + # Ensure server is running before proceeding + ensure_server_running(force_restart=force_restart, force_download=force_download) - ensure_server_running() with get_spinner(text="Getting workflow details...", spinner="dots"): + download_geoweaver_jar() + + # Example subprocess command process = subprocess.run( - [ - "java", "-jar", get_geoweaver_jar_path(), "detail", - "--workflow-id", workflow_id - ], + ["java", "-jar", "geoweaver.jar", "--detail", workflow_id], capture_output=True, text=True ) - if process.returncode != 0: - logger.error(f"Error fetching workflow details: {process.stderr}") + + if process.returncode == 0: + print("=== Workflow Details ===") + print(process.stdout) + else: print("=== Error ===") print(process.stderr) - else: - print(process.stdout) + logger.error(process.stderr) -def detail_process(process_id): +def detail_process(process_id, force_restart=False, force_download=False): """ Display detailed information about a process. :param process_id: The ID of the process. + :type process_id: str + :param force_restart: Restart the server even if it's already running. + :param force_download: Force download of Geoweaver jar if necessary. """ if not process_id: - raise ValueError("Process ID is missing.") + raise RuntimeError("Process ID is missing.") + + # Ensure server is running before proceeding + ensure_server_running(force_restart=force_restart, force_download=force_download) - ensure_server_running() with get_spinner(text="Getting process details...", spinner="dots"): + download_geoweaver_jar() + process = subprocess.run( - [ - "java", "-jar", get_geoweaver_jar_path(), "detail", - "--process-id", process_id - ], - capture_output=True, - text=True + ["your_command_here", process_id], # replace with actual command to get process details + text=True, # Make sure to get output as string (if using Python >= 3.7) + capture_output=True ) - if process.returncode != 0: - logger.error(f"Error fetching process details: {process.stderr}") - print("=== Error ===") + + # Print and log stderr if there is an error + if process.stderr: print(process.stderr) - else: + logger.error(process.stderr) + + # You can add more handling or processing of the output as necessary + if process.stdout: print(process.stdout) + # Any further processing of the output can go here -def detail_host(host_id): +def detail_host(host_id, force_restart=False, force_download=False): """ Display detailed information about a host. :param host_id: The ID of the host. + :type host_id: str + :param force_restart: Restart the server even if it's already running. + :param force_download: Force download of Geoweaver jar if necessary. """ if not host_id: - raise ValueError("Host ID is missing.") + raise RuntimeError("Host ID is missing.") + + # Ensure server is running before proceeding + ensure_server_running(force_restart=force_restart, force_download=force_download) - ensure_server_running() with get_spinner(text="Getting host details...", spinner="dots"): + download_geoweaver_jar() process = subprocess.run( [ - "java", "-jar", get_geoweaver_jar_path(), "detail", - "--host-id", host_id + "your_command_here", host_id # replace with actual command to get host details ], + cwd=f"{get_root_dir()}/", # Ensure this path is correct capture_output=True, text=True ) - if process.returncode != 0: - logger.error(f"Error fetching host details: {process.stderr}") + + if process.returncode == 0: + print("=== Host Details ===") + print(process.stdout) + else: print("=== Error ===") print(process.stderr) - else: - print(process.stdout) + logger.error(process.stderr) - -def get_process_code(process_id): +def get_process_code(process_id, force_restart=False, force_download=False): """ Get the code of a process. - :param process_id: The ID of the process. :type process_id: str + :param force_restart: Restart the server even if it's already running. + :param force_download: Force download of Geoweaver jar if necessary. :return: The code of the process. :rtype: str """ + # Ensure server is running before proceeding + ensure_server_running(force_restart=force_restart, force_download=force_download) + r = requests.post( f"{GEOWEAVER_DEFAULT_ENDPOINT_URL}/web/detail", - data={"type": "process", "id": process_id}, - ).json() - return r["code"] \ No newline at end of file + data={"type": "process", "id": process_id} + ) + + if r.status_code == 200: + return r.text # Return the process code as text + else: + raise RuntimeError(f"Failed to get process code: {r.text}") From 4bc8f8fef9e312afc206b2fdecbb0e3645872234 Mon Sep 17 00:00:00 2001 From: saikiranAnnam Date: Mon, 16 Dec 2024 22:17:17 -0500 Subject: [PATCH 3/6] updated detail command issue --- pygeoweaver/commands/pgw_detail.py | 139 +++++++++++++++-------------- test/test_detail.py | 2 - 2 files changed, 73 insertions(+), 68 deletions(-) diff --git a/pygeoweaver/commands/pgw_detail.py b/pygeoweaver/commands/pgw_detail.py index 11c67bf..a19044b 100644 --- a/pygeoweaver/commands/pgw_detail.py +++ b/pygeoweaver/commands/pgw_detail.py @@ -5,8 +5,6 @@ from pygeoweaver.utils import ( download_geoweaver_jar, get_geoweaver_jar_path, - get_java_bin_path, - get_root_dir, get_spinner, check_os, ) @@ -14,8 +12,6 @@ start_on_windows, start_on_mac_linux, check_geoweaver_status, - stop_on_windows, - stop_on_mac_linux, ) logger = logging.getLogger(__name__) @@ -23,117 +19,128 @@ def ensure_server_running(force_restart=False, force_download=False): """ Ensure the Geoweaver server is running. If not, start it. - :param force_restart: Restart the server even if it's already running. :param force_download: Force download of Geoweaver jar if necessary. """ + logger.info("Ensuring Geoweaver server is running...") + if force_download: + download_geoweaver_jar() + if not force_restart and check_geoweaver_status(): logger.info("Geoweaver server is already running.") return logger.info("Starting Geoweaver server...") - - # Determine the operating system and start accordingly os_type = check_os() - if os_type == 3: # Windows - start_on_windows(force_restart=force_restart, force_download=force_download, exit_on_finish=False) - else: # macOS or Linux - start_on_mac_linux(force_restart=force_restart, force_download=force_download, exit_on_finish=False) + try: + if os_type == 3: # Windows + start_on_windows(force_restart=force_restart, exit_on_finish=False) + else: # macOS or Linux + start_on_mac_linux(force_restart=force_restart, exit_on_finish=False) + + # Recheck server status + if not check_geoweaver_status(): + raise RuntimeError("Failed to start Geoweaver server.") + logger.info("Geoweaver server started successfully.") + except Exception as e: + logger.error(f"Error starting Geoweaver server: {e}") + raise + + +def get_geoweaver_endpoint(): + """ + Get the endpoint URL for the Geoweaver server. + """ + return GEOWEAVER_DEFAULT_ENDPOINT_URL or "http://localhost:8070/Geoweaver" - # Verify server status after starting - if not check_geoweaver_status(): - raise RuntimeError("Failed to start Geoweaver server. Please check logs for details.") def detail_workflow(workflow_id, force_restart=False, force_download=False): """ Display detailed information about a workflow. :param workflow_id: The ID of the workflow. - :type workflow_id: str :param force_restart: Restart the server even if it's already running. :param force_download: Force download of Geoweaver jar if necessary. """ if not workflow_id: - raise RuntimeError("Workflow ID is missing.") + raise ValueError("Workflow ID is missing.") - # Ensure server is running before proceeding ensure_server_running(force_restart=force_restart, force_download=force_download) - + logger.info("Fetching workflow details...") with get_spinner(text="Getting workflow details...", spinner="dots"): - download_geoweaver_jar() - - # Example subprocess command - process = subprocess.run( - ["java", "-jar", "geoweaver.jar", "--detail", workflow_id], - capture_output=True, - text=True - ) - - if process.returncode == 0: - print("=== Workflow Details ===") - print(process.stdout) - else: - print("=== Error ===") - print(process.stderr) - logger.error(process.stderr) + try: + process = subprocess.run( + [ + "java", "-jar", get_geoweaver_jar_path(), "detail", + "--workflow-id", workflow_id + ], + capture_output=True, + text=True + ) + if process.returncode != 0: + logger.error(f"Error fetching workflow details: {process.stderr}") + print("=== Error ===") + print(process.stderr) + else: + print(process.stdout) + except Exception as e: + logger.error(f"Unexpected error while fetching workflow details: {e}") + raise + def detail_process(process_id, force_restart=False, force_download=False): """ Display detailed information about a process. :param process_id: The ID of the process. - :type process_id: str :param force_restart: Restart the server even if it's already running. :param force_download: Force download of Geoweaver jar if necessary. """ if not process_id: raise RuntimeError("Process ID is missing.") - # Ensure server is running before proceeding ensure_server_running(force_restart=force_restart, force_download=force_download) - with get_spinner(text="Getting process details...", spinner="dots"): download_geoweaver_jar() - + process = subprocess.run( - ["your_command_here", process_id], # replace with actual command to get process details - text=True, # Make sure to get output as string (if using Python >= 3.7) + [ + "java", "-jar", get_geoweaver_jar_path(), "detail", + "--process-id", process_id + ], + text=True, capture_output=True ) - - # Print and log stderr if there is an error + if process.stderr: + print("=== Error ===") print(process.stderr) logger.error(process.stderr) - - # You can add more handling or processing of the output as necessary if process.stdout: + print("=== Process Details ===") print(process.stdout) - # Any further processing of the output can go here + def detail_host(host_id, force_restart=False, force_download=False): """ Display detailed information about a host. :param host_id: The ID of the host. - :type host_id: str :param force_restart: Restart the server even if it's already running. :param force_download: Force download of Geoweaver jar if necessary. """ if not host_id: raise RuntimeError("Host ID is missing.") - # Ensure server is running before proceeding ensure_server_running(force_restart=force_restart, force_download=force_download) - with get_spinner(text="Getting host details...", spinner="dots"): download_geoweaver_jar() process = subprocess.run( [ - "your_command_here", host_id # replace with actual command to get host details + "java", "-jar", get_geoweaver_jar_path(), "detail", + "--host-id", host_id ], - cwd=f"{get_root_dir()}/", # Ensure this path is correct capture_output=True, text=True ) - + if process.returncode == 0: print("=== Host Details ===") print(process.stdout) @@ -142,25 +149,25 @@ def detail_host(host_id, force_restart=False, force_download=False): print(process.stderr) logger.error(process.stderr) -def get_process_code(process_id, force_restart=False, force_download=False): + +def get_process_code(process_id): """ Get the code of a process. + :param process_id: The ID of the process. :type process_id: str - :param force_restart: Restart the server even if it's already running. - :param force_download: Force download of Geoweaver jar if necessary. :return: The code of the process. :rtype: str """ - # Ensure server is running before proceeding - ensure_server_running(force_restart=force_restart, force_download=force_download) - - r = requests.post( - f"{GEOWEAVER_DEFAULT_ENDPOINT_URL}/web/detail", - data={"type": "process", "id": process_id} - ) - - if r.status_code == 200: - return r.text # Return the process code as text - else: - raise RuntimeError(f"Failed to get process code: {r.text}") + ensure_server_running() + url = f"{get_geoweaver_endpoint()}/web/detail" + try: + r = requests.post( + url, + data={"type": "process", "id": process_id}, + timeout=10 + ).json() + return r.get("code", "No code available") + except Exception as e: + logger.error(f"Error fetching process code: {e}") + raise diff --git a/test/test_detail.py b/test/test_detail.py index fe351da..19d3d2c 100644 --- a/test/test_detail.py +++ b/test/test_detail.py @@ -54,5 +54,3 @@ def test_detail_host(capfd): ) - - From a634e2321ff3b21fb23fa86c75574de9a9c02ac1 Mon Sep 17 00:00:00 2001 From: saikiranAnnam Date: Mon, 16 Dec 2024 22:34:53 -0500 Subject: [PATCH 4/6] updated detail command issue --- ] | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 ] diff --git a/] b/] new file mode 100644 index 0000000..bcd5cd0 --- /dev/null +++ b/] @@ -0,0 +1,20 @@ +fixed detail command issue + +# Conflicts: +# test/test_detail.py + +# Please enter the commit message for your changes. Lines starting +# with '#' will be ignored, and an empty message aborts the commit. +# +# interactive rebase in progress; onto 4a8e3d2 +# Last command done (1 command done): +# pick 408887b fixed detail command issue +# Next commands to do (2 remaining commands): +# pick a06f1c3 fixed and updated detail command issue +# pick 4bc8f8f updated detail command issue +# You are currently rebasing branch 'fix/detail-command' on '4a8e3d2'. +# +# Changes to be committed: +# modified: pygeoweaver/commands/pgw_detail.py +# modified: test/test_detail.py +# From a5c8a6e30c2388094a2e54bf42ab06f30f733d03 Mon Sep 17 00:00:00 2001 From: saikiranAnnam Date: Mon, 16 Dec 2024 23:04:01 -0500 Subject: [PATCH 5/6] updated detail command issue --- pygeoweaver/commands/pgw_detail.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygeoweaver/commands/pgw_detail.py b/pygeoweaver/commands/pgw_detail.py index a19044b..ba57bdb 100644 --- a/pygeoweaver/commands/pgw_detail.py +++ b/pygeoweaver/commands/pgw_detail.py @@ -18,7 +18,7 @@ def ensure_server_running(force_restart=False, force_download=False): """ - Ensure the Geoweaver server is running. If not, start it. + Ensure the Geoweaver server is running. If not, start the sever. :param force_restart: Restart the server even if it's already running. :param force_download: Force download of Geoweaver jar if necessary. """ From 35490b7d172e6ab9943ac22681b72af7fed5308f Mon Sep 17 00:00:00 2001 From: saikiranAnnam <135786369+saikiranAnnam@users.noreply.github.com> Date: Mon, 16 Dec 2024 23:10:57 -0500 Subject: [PATCH 6/6] Delete ] --- ] | 20 -------------------- 1 file changed, 20 deletions(-) delete mode 100644 ] diff --git a/] b/] deleted file mode 100644 index bcd5cd0..0000000 --- a/] +++ /dev/null @@ -1,20 +0,0 @@ -fixed detail command issue - -# Conflicts: -# test/test_detail.py - -# Please enter the commit message for your changes. Lines starting -# with '#' will be ignored, and an empty message aborts the commit. -# -# interactive rebase in progress; onto 4a8e3d2 -# Last command done (1 command done): -# pick 408887b fixed detail command issue -# Next commands to do (2 remaining commands): -# pick a06f1c3 fixed and updated detail command issue -# pick 4bc8f8f updated detail command issue -# You are currently rebasing branch 'fix/detail-command' on '4a8e3d2'. -# -# Changes to be committed: -# modified: pygeoweaver/commands/pgw_detail.py -# modified: test/test_detail.py -#