From 1129b02bc9667af4ee44aace94fc777a97a4d7fa Mon Sep 17 00:00:00 2001 From: ZihengSun Date: Tue, 13 Feb 2024 00:08:43 -0500 Subject: [PATCH] use python script, discard the bat scripts --- pygeoweaver/server.py | 76 ++++++++++++++++++++++++++++++++++++++----- pygeoweaver/start.bat | 1 + pygeoweaver/stop.bat | 2 +- pygeoweaver/utils.py | 2 +- 4 files changed, 70 insertions(+), 11 deletions(-) diff --git a/pygeoweaver/server.py b/pygeoweaver/server.py index 919d2e7..655da4f 100644 --- a/pygeoweaver/server.py +++ b/pygeoweaver/server.py @@ -2,6 +2,8 @@ import socket import subprocess import webbrowser +import time +import requests from pygeoweaver.constants import GEOWEAVER_DEFAULT_ENDPOINT_URL from pygeoweaver.jdk_utils import check_java from pygeoweaver.utils import ( @@ -23,15 +25,75 @@ logger = get_logger(__name__) +def start_on_windows(): + + # Stop running Geoweaver if any + print("Stop running Geoweaver if any..") + subprocess.run(["taskkill", "/f", "/im", "geoweaver.exe"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + + # Get the user's home directory + home_dir = os.path.expanduser("~") + + # Check if Java command exists in the system PATH + print("Check java exists..") + java_cmd = "java" + try: + subprocess.run(["where", "java"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True) + except subprocess.CalledProcessError: + # Java command not found in PATH, check JDK folder in home directory + jdk_home = os.path.join(home_dir, "jdk", "jdk-11.0.18+10") # Change this to your JDK installation directory + print("Check jdk_home", jdk_home) + java_cmd = os.path.join(jdk_home, "bin", "java.exe") + if not os.path.exists(java_cmd): + print("Java command not found.") + exit(1) + + print("Start Geoweaver..") + geoweaver_jar = os.path.join(home_dir, "geoweaver.jar") + print(f'"{java_cmd}" -jar "{geoweaver_jar}"') + subprocess.Popen([java_cmd, "-jar", geoweaver_jar], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, creationflags=subprocess.CREATE_NEW_CONSOLE) + + status = 0 + counter = 0 + max_attempts = 20 + retry_delay = 2 + while counter < max_attempts: + time.sleep(retry_delay) + counter += 1 + try: + response = requests.get("http://localhost:8070/Geoweaver", allow_redirects=False) + if response.status_code == 302: + log_file = os.path.join(home_dir, "geoweaver.log") + # Ensure the log file exists, create it if it doesn't + if not os.path.exists(log_file): + open(log_file, "a").close() # Create an empty file if it doesn't exist + + # Now you can safely open the log file for reading + with open(log_file, "r") as f: + print(f.read()) + print("Success: Geoweaver is up") + exit(0) + except Exception as e: + # print(f"Error occurred during request: {e}") + continue + + print("Error: Geoweaver is not up") + exit(1) + + +def stop_on_windows(): + print("Stopping Geoweaver...") + subprocess.run(["taskkill", "/f", "/im", "java.exe"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + print("Geoweaver stopped successfully.") + + def start(force=False): download_geoweaver_jar(overwrite=force) check_java() if check_os() == 3: - logger.debug(f"Detected Windows, running {get_module_absolute_path()}/start.bat") - subprocess.run( - [f"{get_module_absolute_path()}/start.bat"], cwd=f"{get_root_dir()}/" - ) + logger.debug(f"Detected Windows, running start python script..") + start_on_windows() else: logger.debug(f"Detected Linux/MacOs, running {get_module_absolute_path()}/start.sh") subprocess.run( @@ -42,11 +104,7 @@ def start(force=False): def stop(): check_java() if check_os() == 3: - subprocess.run( - [f"{get_module_absolute_path()}/stop.bat"], - cwd=f"{get_root_dir()}/", - shell=True, - ) + stop_on_windows() else: subprocess.run( [f"{get_module_absolute_path()}/stop.sh"], diff --git a/pygeoweaver/start.bat b/pygeoweaver/start.bat index bd5716c..259a96e 100644 --- a/pygeoweaver/start.bat +++ b/pygeoweaver/start.bat @@ -16,6 +16,7 @@ where java > nul 2>&1 if %ERRORLEVEL% NEQ 0 ( rem Java command not found in PATH, check JDK folder in home directory set "jdk_home=%home_dir%\jdk\jdk-11.0.18+10" REM Change this to your JDK installation directory + echo Check jdk_home %jdk_home% if exist "%jdk_home%\bin\java.exe" ( set "java_cmd=%jdk_home%\bin\java.exe" echo Java command found in JDK directory: %java_cmd% diff --git a/pygeoweaver/stop.bat b/pygeoweaver/stop.bat index 0586206..38c4ff4 100644 --- a/pygeoweaver/stop.bat +++ b/pygeoweaver/stop.bat @@ -2,6 +2,6 @@ echo Stopping Geoweaver... -taskkill /f /im javaw.exe > nul +taskkill /f /im java.exe > nul echo Geoweaver stopped successfully. diff --git a/pygeoweaver/utils.py b/pygeoweaver/utils.py index 40f24aa..ed08e48 100644 --- a/pygeoweaver/utils.py +++ b/pygeoweaver/utils.py @@ -108,7 +108,7 @@ def download_geoweaver_jar(overwrite=False): subprocess.run( ["chmod", "+x", get_geoweaver_jar_path()], cwd=f"{get_root_dir()}/" ) - return + return print("Downloading latest version of Geoweaver...") geoweaver_url = (