Skip to content

Commit

Permalink
add pygeoweaver commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ZihengSun committed Feb 3, 2024
1 parent 422fc0f commit f50be26
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 14 deletions.
52 changes: 51 additions & 1 deletion pygeoweaver/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
The main function of pygeoweaver
To run in CLI mode.
"""
import click
from pygeoweaver import (
detail_host,
detail_process,
Expand All @@ -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()
Expand Down Expand Up @@ -61,4 +111,4 @@ def main():


if __name__ == "__main__":
main()
geoweaver()
2 changes: 2 additions & 0 deletions pygeoweaver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()}/"
)
Expand Down
50 changes: 37 additions & 13 deletions pygeoweaver/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,47 @@


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.")

return java_bin_path


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:
Expand All @@ -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())
Expand All @@ -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":
Expand All @@ -121,13 +136,19 @@ def check_os():


def check_ipython():
"""
Check if the code is running in an IPython environment.
"""
try:
return get_ipython().__class__.__name__ == "ZMQInteractiveShell"
except:
return False


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")
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ classifiers = [
"Operating System :: OS Independent",
]
dependencies = [
"click",
"setuptools",
"requests",
"pydantic",
Expand Down

0 comments on commit f50be26

Please sign in to comment.