Skip to content

Commit

Permalink
Merge pull request #4 from ZihengSun/main
Browse files Browse the repository at this point in the history
sync
  • Loading branch information
ZihengSun authored May 25, 2023
2 parents ee241f8 + 09cb8b8 commit e4d46c3
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 59 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ dist/*
.idea/
*/__pycache__/*
*.ipynb
pygeoweaver/__pycache__/__init__.cpython-39.pyc
pygeoweaver/__pycache__/__main__.cpython-39.pyc
pygeoweaver/__pycache__/server.cpython-39.pyc
pygeoweaver/__pycache__/utils.cpython-39.pyc
.coverage
5 changes: 3 additions & 2 deletions pygeoweaver/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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
from pygeoweaver.server import show

def main():
# start geoweaver
Expand Down Expand Up @@ -37,8 +38,8 @@ def main():

# reset localhost password for Geoweaver
# reset_password()
ui()
helpwith()
show()
# helpwith()


if __name__ == "__main__":
Expand Down
Binary file removed pygeoweaver/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
Binary file removed pygeoweaver/__pycache__/server.cpython-39.pyc
Binary file not shown.
Binary file removed pygeoweaver/__pycache__/utils.cpython-39.pyc
Binary file not shown.
2 changes: 2 additions & 0 deletions pygeoweaver/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

GEOWEAVER_DEFAULT_ENDPOINT_URL="http://localhost:8070/Geoweaver"
1 change: 0 additions & 1 deletion pygeoweaver/sc_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@
from pygeoweaver.server import *
from pygeoweaver.sc_resetpassword import *
from pygeoweaver.sc_help import *
from pygeoweaver.utils import ui
15 changes: 13 additions & 2 deletions pygeoweaver/server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import subprocess
from pygeoweaver.utils import checkOS, download_geoweaver_jar, get_root_dir
import webbrowser
from pygeoweaver.constants import GEOWEAVER_DEFAULT_ENDPOINT_URL
from pygeoweaver.utils import checkIPython, checkOS, download_geoweaver_jar, get_logger, get_root_dir

"""
This module provides function to start and stop Geoweaver server.
Expand All @@ -9,6 +11,7 @@
"""

logger = get_logger(__name__)

def start(force=False):
download_geoweaver_jar(overwrite=force)
Expand All @@ -26,6 +29,14 @@ def stop():
result = subprocess.run(['./stop.sh'], cwd=f"{get_root_dir()}/", shell=True)



def show(geoweaver_url = GEOWEAVER_DEFAULT_ENDPOINT_URL):
download_geoweaver_jar() # check if geoweaver is initialized
if checkIPython():
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)


3 changes: 3 additions & 0 deletions pygeoweaver/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
echo "Stop running Geoweaver if any.."
pkill -f geoweaver

echo "Check Java.."


echo "Start Geoweaver.."
nohup java -jar ~/geoweaver.jar > ~/geoweaver.log &

Expand Down
68 changes: 56 additions & 12 deletions pygeoweaver/utils.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
import logging
import os
import subprocess
import requests
import platform

from IPython import get_ipython
from IPython.display import IFrame


def ui():
download_geoweaver_jar() # check if geoweaver is initialized
shell_type = str(get_ipython().__class__.__module__)
if shell_type == "google.colab._shell" or shell_type == "ipykernel.zmqshell":
return IFrame(src="http://localhost:8070/Geoweaver/", width='100%', height='500px')
else:
print('Web UI for python bindings can be only used for Colab / Jupyter / Interactive Python shell')
import sys


def get_home_dir():
Expand Down Expand Up @@ -62,3 +52,57 @@ def checkOS():
return 2
elif platform == "Windows":
return 3


def checkIPython():
try:
return get_ipython().__class__.__name__ == "ZMQInteractiveShell"
except:
return False


def is_java_installed():
try:
# Check if Java is installed by running "java -version" command
subprocess.run(["java", "-version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return True
except FileNotFoundError:
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("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")
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'))\"")
os.system("choco install -y openjdk")
else:
print("Unsupported operating system.")
sys.exit(1)

def checkJava():
# Check if Java is installed
if is_java_installed():
print("Java is already installed.")
else:
print("Java is not installed. Installing...")
install_java()
print("Java installation complete.")


def get_logger(class_name):
logger = logging.getLogger(class_name)
logger.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
return logger

9 changes: 6 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[build-system]
requires = ["setuptools>=61.0", "wheel"]
requires = ["setuptools>=61.0", "wheel", "pytest-cov"]
build-backend = "setuptools.build_meta"

[project]
name = "pygeoweaver"
version = "0.6.5"
version = "0.6.6"
authors = [
{ name="Geoweaver team", email="[email protected]" },
]
Expand All @@ -22,7 +22,7 @@ classifiers = [

[tool.poetry]
name = "pygeoweaver"
version = "0.6.2"
version = "0.6.6"
description = "This is a wrapper package of the Geoweaver app."
authors = ["Geoweaver team <[email protected]>"]
readme = "README.md"
Expand All @@ -34,3 +34,6 @@ setuptools = ">=61.0"
requests = "2.28.2"

[tool.poetry.scripts]

[tool.pytest.ini_options]
addopts = "-p no:warnings --cov=pygeoweaver --cov-report=html"
39 changes: 0 additions & 39 deletions test/test_all.py

This file was deleted.

32 changes: 32 additions & 0 deletions test/test_detail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@


from io import StringIO
import sys
import unittest
from pygeoweaver.sc_detail import detail_host, detail_process, detail_workflow
from pygeoweaver.utils import get_logger


logger = get_logger(__name__)


def test_detail_process(capfd):
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


def test_detail_workflow(capfd):
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


def test_detail_host(capfd):
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

53 changes: 53 additions & 0 deletions test/test_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
The main function of pygeoweaver
To run in CLI mode.
"""
import logging
from unittest.mock import patch
import requests
from pygeoweaver import start, stop

import unittest

from pygeoweaver.constants import GEOWEAVER_DEFAULT_ENDPOINT_URL
from pygeoweaver.server import show
from pygeoweaver.utils import get_logger


logger = get_logger(__name__)



class TestServer(unittest.TestCase):

def test_server_start_stop(self):
start()
response = requests.get(GEOWEAVER_DEFAULT_ENDPOINT_URL)
self.assertEqual(response.status_code, 200, f"Failed to access URL: {GEOWEAVER_DEFAULT_ENDPOINT_URL}")
stop()
with self.assertRaises(requests.exceptions.ConnectionError):
response = requests.get(GEOWEAVER_DEFAULT_ENDPOINT_URL)

stop() # stop again should have no issue


def test_windows(self):
with patch('pygeoweaver.server.checkOS') as mock_checkos:
mock_checkos.return_value = 3
with self.assertRaises(RuntimeError):
start()
with self.assertRaises(RuntimeError):
stop()

def test_show_gui(self):
with patch('pygeoweaver.webbrowser.open') as mock_browser_open:
show()
mock_browser_open.assert_called_once()

with patch('pygeoweaver.server.checkIPython') as mock_checkipython:
mock_checkipython.return_value = True
show()
mock_browser_open.assert_called_once()

if __name__ == "__main__":
unittest.main()

0 comments on commit e4d46c3

Please sign in to comment.