From f50be260153b97b0b688a7a66d8d5b1a63066cc2 Mon Sep 17 00:00:00 2001 From: ZihengSun Date: Fri, 2 Feb 2024 22:49:04 -0500 Subject: [PATCH] add pygeoweaver commands --- pygeoweaver/__main__.py | 52 ++++++++++++++++++++++++++++++++++++++++- pygeoweaver/server.py | 2 ++ pygeoweaver/utils.py | 50 ++++++++++++++++++++++++++++----------- pyproject.toml | 1 + 4 files changed, 91 insertions(+), 14 deletions(-) diff --git a/pygeoweaver/__main__.py b/pygeoweaver/__main__.py index 4d0471b..d2af773 100644 --- a/pygeoweaver/__main__.py +++ b/pygeoweaver/__main__.py @@ -2,6 +2,7 @@ The main function of pygeoweaver To run in CLI mode. """ +import click from pygeoweaver import ( detail_host, detail_process, @@ -19,9 +20,58 @@ run_workflow, helpwith, ) +from pygeoweaver.constants import GEOWEAVER_DEFAULT_ENDPOINT_URL from pygeoweaver.server import show +@click.group() +def geoweaver(): + """ + Geoweaver CLI: A tool for managing workflows and processes. + """ + pass + + +@geoweaver.command("start") +@click.option('--force', is_flag=True, help='Force overwrite the Geoweaver JAR file if it already exists.') +def start_command(force): + """ + Start the Geoweaver application. + """ + start(force) + + +@geoweaver.command("stop") +def stop_command(): + """ + Start the Geoweaver application. + """ + stop() + +@geoweaver.command("show") +@click.option('--geoweaver-url', default=GEOWEAVER_DEFAULT_ENDPOINT_URL, help='Geoweaver URL (default is GEOWEAVER_DEFAULT_ENDPOINT_URL)') +def show_command(geoweaver_url): + """ + Show graphical user interface of Geoweaver in browser + + Args: + geoweaver_url (_type_): _description_ + """ + show(geoweaver_url) + + +@geoweaver.group("create") +def create_command(): + """ + Create commands for Geoweaver. + """ + pass + + + + + + def main(): # start geoweaver # start() @@ -61,4 +111,4 @@ def main(): if __name__ == "__main__": - main() + geoweaver() diff --git a/pygeoweaver/server.py b/pygeoweaver/server.py index 2d630c1..919d2e7 100644 --- a/pygeoweaver/server.py +++ b/pygeoweaver/server.py @@ -28,10 +28,12 @@ def start(force=False): 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()}/" ) else: + logger.debug(f"Detected Linux/MacOs, running {get_module_absolute_path()}/start.sh") subprocess.run( [f"{get_module_absolute_path()}/start.sh"], cwd=f"{get_root_dir()}/" ) diff --git a/pygeoweaver/utils.py b/pygeoweaver/utils.py index 9e009aa..ccfa0c4 100644 --- a/pygeoweaver/utils.py +++ b/pygeoweaver/utils.py @@ -10,39 +10,37 @@ def get_home_dir(): + """ + Get the user's home directory. + """ return os.path.expanduser("~") def get_root_dir(): + """ + Get the root directory of the module. + """ head, tail = os.path.split(__file__) return head def get_java_bin_from_which(): + """ + Get the path of the Java binary using the 'which' command. + """ system = platform.system() if system == "Darwin" or system == "Linux": - try: - java_bin_sh = f"{get_root_dir()}/java_bin.sh" - os.chmod(java_bin_sh, 0o755) - output = subprocess.check_output([java_bin_sh], encoding="utf-8") - java_bin_path = output.strip() - except subprocess.CalledProcessError as e: - print(f"Command execution failed: {e.output}") - return None - elif system == "Windows": - print("Unsupported platform for windows yet.") - else: print("Unsupported platform.") @@ -50,7 +48,9 @@ def get_java_bin_from_which(): def get_java_bin_path(): - # Check if the 'java' command is available in the system path + """ + Get the path of the Java binary. + """ if sys.platform.startswith("win"): # Windows java_exe = "java.exe" else: @@ -71,19 +71,31 @@ def get_java_bin_path(): def get_module_absolute_path(): + """ + Get the absolute path of the module. + """ module_path = os.path.abspath(__file__) return os.path.dirname(module_path) def get_geoweaver_jar_path(): + """ + Get the path of the Geoweaver JAR file. + """ return f"{get_home_dir()}/geoweaver.jar" def check_geoweaver_jar(): + """ + Check if the Geoweaver JAR file exists. + """ return os.path.isfile(get_geoweaver_jar_path()) def download_geoweaver_jar(overwrite=False): + """ + Download the latest version of Geoweaver JAR file. + """ if check_geoweaver_jar(): if overwrite: os.remove(get_geoweaver_jar_path()) @@ -106,12 +118,15 @@ def download_geoweaver_jar(overwrite=False): if check_geoweaver_jar(): print("Geoweaver.jar is downloaded") - else: raise RuntimeError("Fail to download geoweaver.jar") def check_os(): + """ + Check the operating system and return corresponding code. + 1: Linux, 2: MacOS, 3: Windows + """ if platform.system() == "Linux" or platform == "Linux2": return 1 elif platform.system() == "Darwin": @@ -121,6 +136,9 @@ def check_os(): def check_ipython(): + """ + Check if the code is running in an IPython environment. + """ try: return get_ipython().__class__.__name__ == "ZMQInteractiveShell" except: @@ -128,6 +146,9 @@ def check_ipython(): def get_logger(class_name): + """ + Get a logger with the specified class name. + """ logger = logging.getLogger(class_name) logger.setLevel(logging.INFO) formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s") @@ -138,6 +159,9 @@ def get_logger(class_name): def copy_files(source_folder, destination_folder): + """ + Copy files from the source folder to the destination folder. + """ for root, dirs, files in os.walk(source_folder): for file in files: source_file = os.path.join(root, file) diff --git a/pyproject.toml b/pyproject.toml index ef10957..8166c4d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,6 +15,7 @@ classifiers = [ "Operating System :: OS Independent", ] dependencies = [ + "click", "setuptools", "requests", "pydantic",