Skip to content

Commit

Permalink
Merge pull request #6 from ZihengSun/main
Browse files Browse the repository at this point in the history
sync
  • Loading branch information
ZihengSun authored Jun 4, 2023
2 parents 50f5f63 + de233ab commit bc56dca
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 31 deletions.
5 changes: 4 additions & 1 deletion pygeoweaver/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -40,6 +42,7 @@ def main():
# reset_password()
show()
# helpwith()
# check_java()


if __name__ == "__main__":
Expand Down
10 changes: 10 additions & 0 deletions pygeoweaver/download_gw.bat
Original file line number Diff line number Diff line change
@@ -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
25 changes: 12 additions & 13 deletions pygeoweaver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)


34 changes: 34 additions & 0 deletions pygeoweaver/start.bat
Original file line number Diff line number Diff line change
@@ -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=<response.txt
del response.txt
if "%STATUS%"=="302" (
goto :success
)
if %counter%==20 (
goto :error
)
goto :loop

:success
type "%USERPROFILE%\geoweaver.log"
echo Success: Geoweaver is up
exit /b 0

:error
echo Error: Geoweaver is not up
exit /b 1
7 changes: 7 additions & 0 deletions pygeoweaver/stop.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@echo off

echo Stopping Geoweaver...

taskkill /f /im javaw.exe > nul

echo Geoweaver stopped successfully.
52 changes: 37 additions & 15 deletions pygeoweaver/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import platform
import sys

from IPython import get_ipython


def get_home_dir():
return os.path.expanduser('~')
Expand All @@ -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)
Expand All @@ -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"
Expand All @@ -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:
Expand All @@ -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.")
Expand All @@ -109,4 +132,3 @@ def get_logger(class_name):
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
return logger

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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="[email protected]" },
]
Expand All @@ -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 <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit bc56dca

Please sign in to comment.