diff --git a/pygeoweaver/__main__.py b/pygeoweaver/__main__.py index 4cb8a5c..7d56ce4 100644 --- a/pygeoweaver/__main__.py +++ b/pygeoweaver/__main__.py @@ -4,8 +4,10 @@ """ from pygeoweaver import detail_host, detail_process, detail_workflow, export_workflow, \ show_history, import_workflow, list_hosts, list_processes, list_workflows, \ - start, stop, reset_password, run_process, run_worklfow, helpwith, ui + start, stop, reset_password, run_process, run_worklfow, helpwith from pygeoweaver.server import show +from pygeoweaver.utils import check_java + def main(): # start geoweaver @@ -40,6 +42,7 @@ def main(): # reset_password() show() # helpwith() + # check_java() if __name__ == "__main__": diff --git a/pygeoweaver/download_gw.bat b/pygeoweaver/download_gw.bat new file mode 100644 index 0000000..6f12c8f --- /dev/null +++ b/pygeoweaver/download_gw.bat @@ -0,0 +1,10 @@ +@echo off + +set "FILE=%USERPROFILE%\geoweaver.jar" +if exist "%FILE%" ( + echo %FILE% exists. Removing... + del "%FILE%" +) + +echo Downloading the latest geoweaver.jar to user home directory +cd %USERPROFILE% && curl -OL https://github.com/ESIPFed/Geoweaver/releases/download/latest/geoweaver.jar diff --git a/pygeoweaver/server.py b/pygeoweaver/server.py index 3bd3706..999366a 100644 --- a/pygeoweaver/server.py +++ b/pygeoweaver/server.py @@ -2,7 +2,8 @@ import subprocess import webbrowser from pygeoweaver.constants import GEOWEAVER_DEFAULT_ENDPOINT_URL -from pygeoweaver.utils import checkIPython, checkOS, download_geoweaver_jar, get_logger, get_module_absolute_path, get_root_dir +from pygeoweaver.utils import check_ipython, check_os, download_geoweaver_jar, get_logger, get_module_absolute_path, \ + get_root_dir """ This module provides function to start and stop Geoweaver server. @@ -13,30 +14,28 @@ logger = get_logger(__name__) + def start(force=False): download_geoweaver_jar(overwrite=force) - if checkOS() == 3: - raise RuntimeError("windows is not supported yet") + if check_os() == 3: + subprocess.run([f'{get_module_absolute_path()}/start.bat'], cwd=f"{get_root_dir()}/") else: - result = subprocess.run([f'{get_module_absolute_path()}/start.sh'], cwd=f"{get_root_dir()}/") - + subprocess.run([f'{get_module_absolute_path()}/start.sh'], cwd=f"{get_root_dir()}/") def stop(): - if checkOS() == 3: - raise RuntimeError("Windows is not supported yet") + if check_os() == 3: + subprocess.run([f'{get_module_absolute_path()}/stop.bat'], cwd=f"{get_root_dir()}/", shell=True) else: - result = subprocess.run([f'{get_module_absolute_path()}/stop.sh'], cwd=f"{get_root_dir()}/", shell=True) - + subprocess.run([f'{get_module_absolute_path()}/stop.sh'], cwd=f"{get_root_dir()}/", shell=True) + -def show(geoweaver_url = GEOWEAVER_DEFAULT_ENDPOINT_URL): +def show(geoweaver_url=GEOWEAVER_DEFAULT_ENDPOINT_URL): download_geoweaver_jar() # check if geoweaver is initialized - if checkIPython(): + if check_ipython(): logger.info("enter ipython block") from IPython.display import IFrame return IFrame(src=geoweaver_url, width='100%', height='500px') else: logger.info("enter self opening block") webbrowser.open(geoweaver_url) - - diff --git a/pygeoweaver/start.bat b/pygeoweaver/start.bat new file mode 100644 index 0000000..8e6e5d1 --- /dev/null +++ b/pygeoweaver/start.bat @@ -0,0 +1,34 @@ +@echo off + +echo Stop running Geoweaver if any.. +taskkill /f /im geoweaver.exe > nul + +echo Check Java.. + +echo Start Geoweaver.. +start /b javaw -jar "%USERPROFILE%\geoweaver.jar" + +set STATUS=0 +set counter=0 +:loop +ping -n 2 127.0.0.1 > nul +set /a counter+=1 +curl -s -o NUL -w "%%{http_code}" "http://localhost:8070/Geoweaver" > response.txt +set /p STATUS= nul + +echo Geoweaver stopped successfully. diff --git a/pygeoweaver/utils.py b/pygeoweaver/utils.py index 2ffb2d5..76646e8 100644 --- a/pygeoweaver/utils.py +++ b/pygeoweaver/utils.py @@ -5,6 +5,8 @@ import platform import sys +from IPython import get_ipython + def get_home_dir(): return os.path.expanduser('~') @@ -14,6 +16,7 @@ def get_root_dir(): head, tail = os.path.split(__file__) return head + def get_module_absolute_path(): module_path = os.path.abspath(__file__) return os.path.dirname(module_path) @@ -32,8 +35,10 @@ def download_geoweaver_jar(overwrite=False): if overwrite: os.remove(get_geoweaver_jar_path()) else: - subprocess.run(["chmod", "+x", get_geoweaver_jar_path()], cwd=f"{get_root_dir()}/") - return + system = platform.system() + if not system == "Windows": # Windows files are exec by default + subprocess.run(["chmod", "+x", get_geoweaver_jar_path()], cwd=f"{get_root_dir()}/") + return print("Downloading latest version of Geoweaver...") geoweaver_url = "https://github.com/ESIPFed/Geoweaver/releases/download/latest/geoweaver.jar" @@ -49,16 +54,16 @@ def download_geoweaver_jar(overwrite=False): raise RuntimeError("Fail to download geoweaver.jar") -def checkOS(): +def check_os(): if platform.system() == "Linux" or platform == "Linux2": return 1 elif platform.system() == "Darwin": return 2 - elif platform == "Windows": + elif platform.system() == "Windows": return 3 -def checkIPython(): +def check_ipython(): try: return get_ipython().__class__.__name__ == "ZMQInteractiveShell" except: @@ -70,28 +75,46 @@ def is_java_installed(): # Check if Java is installed by running "java -version" command subprocess.run(["java", "-version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) return True - except FileNotFoundError: + except subprocess.CalledProcessError: return False + def install_java(): system = platform.system() if system == "Darwin": - # Install Java on MacOS using Homebrew - os.system("/bin/bash -c '/usr/bin/ruby -e \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)\"'") + os.system( + "/bin/bash -c '/usr/bin/ruby -e \"$(curl -fsSL " + "https://raw.githubusercontent.com/Homebrew/install/master/install)\"'") os.system("brew install openjdk") elif system == "Linux": - # Install Java on Linux using apt package manager - os.system("sudo apt update") - os.system("sudo apt install -y default-jre default-jdk") + # need to check if the package manager type is apt or yum + # arch / debian + package_manager = None + if os.path.exists("/usr/bin/apt"): + package_manager = "apt" + elif os.path.exists("/usr/bin/yum"): + package_manager = "yum" + + if package_manager: + os.system(f"sudo {package_manager} update") + os.system(f"sudo {package_manager} install -y default-jre default-jdk") + else: + print("Package manager not found. Unable to install Java.") + sys.exit(1) elif system == "Windows": - # Install Java on Windows using Chocolatey package manager - os.system("powershell -Command \"Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))\"") + # note: this requires admin access to the pc, else it will fail saying + # Access to the path 'C:\ProgramData\chocolatey\lib-bad' is denied. + os.system( + "powershell -Command \"Set-ExecutionPolicy Bypass -Scope Process -Force; [" + "System.Net.ServicePointManager]::SecurityProtocol = 3072; iex ((New-Object " + "System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))\"") os.system("choco install -y openjdk") else: print("Unsupported operating system.") sys.exit(1) -def checkJava(): + +def check_java(): # Check if Java is installed if is_java_installed(): print("Java is already installed.") @@ -109,4 +132,3 @@ def get_logger(class_name): console_handler.setFormatter(formatter) logger.addHandler(console_handler) return logger - diff --git a/pyproject.toml b/pyproject.toml index 4358ca2..074572e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "pygeoweaver" -version = "0.6.7" +version = "0.6.8" authors = [ { name="Geoweaver team", email="geoweaver.app@gmail.com" }, ] @@ -22,7 +22,7 @@ classifiers = [ [tool.poetry] name = "pygeoweaver" -version = "0.6.7" +version = "0.6.8" description = "This is a wrapper package of the Geoweaver app." authors = ["Geoweaver team "] readme = "README.md"