Skip to content

Commit

Permalink
finished adding all the functions into the click interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ZihengSun committed Feb 3, 2024
1 parent 22e8110 commit cec2089
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 2 deletions.
235 changes: 235 additions & 0 deletions pygeoweaver/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
from pygeoweaver.sc_create import create_process, create_process_from_file, create_workflow
from pygeoweaver.sc_detail import get_process_code
from pygeoweaver.sc_find import get_process_by_id, get_process_by_language, get_process_by_name
from pygeoweaver.sc_history import get_process_history, get_workflow_history
from pygeoweaver.sc_list import list_processes_in_workflow
from pygeoweaver.sc_sync import sync, sync_workflow
from pygeoweaver.server import show


Expand Down Expand Up @@ -242,6 +245,238 @@ def get_process_by_language_command(language):
"""
return get_process_by_language(language)

@geoweaver.group("history")
def history_command():
"""
history commands for Geoweaver.
"""
pass

@history_command.command("show")
@click.argument('history_id', type=str)
def show_history_command(history_id):
"""
Show history for a workflow or process.
:param history_id: The ID of the history.
:type history_id: str
"""
show_history(history_id)


@history_command.command("get_process")
@click.argument('process_id', type=str)
def get_process_history_command(process_id):
"""
Get list of history for a process using process id.
:param process_id: The ID of the process.
:type process_id: str
"""
get_process_history(process_id)


@history_command.command("get_workflow")
@click.argument('workflow_id', type=str)
def get_workflow_history_command(workflow_id):
"""
Get list of history for a workflow using workflow id.
:param workflow_id: The ID of the workflow.
:type workflow_id: str
"""
get_workflow_history(workflow_id)


@geoweaver.group("import")
def import_command():
"""
import commands for Geoweaver.
"""
pass

@import_command.command("workflow")
@click.argument('workflow_zip_file_path', type=click.Path(exists=True, dir_okay=False))
def import_workflow_command(workflow_zip_file_path):
"""
Import a workflow from a zip file.
:param workflow_zip_file_path: The path to the Geoweaver workflow zip file.
:type workflow_zip_file_path: str
"""
import_workflow(workflow_zip_file_path)


@geoweaver.group("list")
def list_command():
"""
list commands for Geoweaver.
"""
pass


@list_command.command("host")
def list_hosts_command():
"""
List all hosts in Geoweaver.
Downloads the Geoweaver JAR, then runs the 'list' command with the '--host' option.
Note: Requires Geoweaver to be initialized and the JAR file to be available.
"""
list_hosts()


@list_command.command("process")
def list_processes_command():
"""
List all processes in Geoweaver.
Downloads the Geoweaver JAR, ensures executable permissions, and then runs the 'list' command with the '--process' option.
Note: Requires Geoweaver to be initialized and the JAR file to be available.
"""
list_processes()


@list_command.command()
@click.argument('workflow_id', type=str)
def list_processes_in_workflow_command(workflow_id):
"""
List processes in a specific workflow.
Downloads the Geoweaver JAR and queries the Geoweaver server for details of a workflow.
Extracts information about nodes in the workflow and returns a list of dictionaries containing 'title' and 'id'.
:param workflow_id: The ID of the workflow.
:type workflow_id: str
:return: List of dictionaries containing 'title' and 'id' of processes in the workflow.
:rtype: list
"""
list_processes_in_workflow(workflow_id)


@list_command.command("workflow")
def list_workflows_command():
"""
List all workflows in Geoweaver.
Downloads the Geoweaver JAR, then runs the 'list' command with the '--workflow' option.
Note: Requires Geoweaver to be initialized and the JAR file to be available.
"""
list_workflows()


@geoweaver.group("run")
def run_command():
"""
Run commands for Geoweaver.
"""
pass


@run_command.command()
@click.option('--process-id', required=True, type=str, help='ID of the process.')
@click.option('--host-id', required=True, type=str, help='ID of the host.')
@click.option('--password', type=str, help='Password for authentication.')
@click.option('--environment', type=str, help='Environment for the process.')
@click.option('--sync-path', type=click.Path(exists=True), help='Path for synchronization.')
def run_process_command(process_id, host_id, password, environment, sync_path):
"""
Run a process.
Args:
--process-id: ID of the process. (required)
--host-id: ID of the host. (required)
--password: Password for authentication. (optional)
--environment: Environment for the process. (optional)
--sync-path: Path for synchronization. (optional)
"""
run_process(
process_id=process_id,
host_id=host_id,
password=password,
environment=environment,
sync_path=sync_path,
)


@run_command.command()
@click.argument('workflow_id', type=str)
@click.option('--workflow-folder-path', '-d', type=str, help='Geoweaver workflow folder path.')
@click.option('--workflow-zip-file-path', '-f', type=str, help='Path to workflow zip file.')
@click.option('--environments', '-e', type=str, help='Environments to run on. List of environment ids with comma as separator.')
@click.option('--hosts', '-h', type=str, help='Hosts to run on. List of host ids with comma as separator.')
@click.option('--passwords', '-p', type=str, help='Passwords to the target hosts. List of passwords with comma as separator.')
@click.option('--sync-path', type=click.Path(exists=True), help='Path for synchronization.')
def run_workflow_command(workflow_id, workflow_folder_path, workflow_zip_file_path, environments, hosts, passwords, sync_path):
"""
Run a workflow.
Args:
<workflow_id>: Workflow ID to run.
-d, --workflow-folder-path: Geoweaver workflow folder path. (optional)
-f, --workflow-zip-file-path: Path to workflow zip file. (optional)
-e, --environments: Environments to run on. List of environment ids with comma as separator. (optional)
-h, --hosts: Hosts to run on. List of host ids with comma as separator. (optional)
-p, --passwords: Passwords to the target hosts. List of passwords with comma as separator. (optional)
"""
run_workflow(
workflow_id=workflow_id,
workflow_folder_path=workflow_folder_path,
workflow_zip_file_path=workflow_zip_file_path,
environment_list=environments,
host_list=hosts,
password_list=passwords,
sync_path=sync_path,
)


@geoweaver.group("sync")
def sync_command():
"""
sync commands for Geoweaver.
"""
pass

@sync_command.command("process")
@click.option('--process-id', required=True, type=str, help='The ID of the Geoweaver process.')
@click.option('--local-path', required=True, type=click.Path(exists=True, file_okay=True, dir_okay=True),
help='The local path to save or load the process code.')
@click.option('--direction', required=True, type=click.Choice(['download', 'upload'], case_sensitive=False),
help='The direction of the sync, either "download" or "upload".')
def sync_command(process_id: str, local_path: typing.Union[str, os.PathLike], direction: str):
"""
Sync code for a Geoweaver process between the local machine and the Geoweaver server.
:param process_id: The ID of the Geoweaver process.
:type process_id: str
:param local_path: The local path to save or load the process code.
:type local_path: Union[str, os.PathLike]
:param direction: The direction of the sync, either "download" or "upload".
:type direction: str
"""
sync(process_id, local_path, direction)


@sync_command.command("workflow")
@click.option('--workflow-id', required=True, type=str, help='The ID of the Geoweaver workflow.')
@click.option('--sync-to-path', required=True, type=click.Path(exists=True, file_okay=False, dir_okay=True),
help='The local path to sync the Geoweaver workflow.')
def sync_workflow_command(workflow_id: str, sync_to_path: typing.Union[str, os.PathLike]):
"""
Sync a Geoweaver workflow, including its code and history, between the local machine and the Geoweaver server.
:param workflow_id: The ID of the Geoweaver workflow.
:type workflow_id: str
:param sync_to_path: The local path to sync the Geoweaver workflow.
:type sync_to_path: Union[str, os.PathLike]
"""
sync_workflow(workflow_id, sync_to_path)




def main():
# start geoweaver
Expand Down
2 changes: 1 addition & 1 deletion pygeoweaver/sc_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ def import_workflow(workflow_zip_file_path):
)

def import_workflow_from_github(git_repo_url):
pass
raise Exception("This feature is not implemented yet")
33 changes: 33 additions & 0 deletions pygeoweaver/sc_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@


def list_hosts():
"""
List all hosts in Geoweaver.
Downloads the Geoweaver JAR, then runs the 'list' command with the '--host' option.
Note: Requires Geoweaver to be initialized and the JAR file to be available.
"""
download_geoweaver_jar()
subprocess.run(
[get_java_bin_path(), "-jar", get_geoweaver_jar_path(), "list", "--host"],
Expand All @@ -22,6 +29,13 @@ def list_hosts():


def list_processes():
"""
List all processes in Geoweaver.
Downloads the Geoweaver JAR, ensures executable permissions, and then runs the 'list' command with the '--process' option.
Note: Requires Geoweaver to be initialized and the JAR file to be available.
"""
download_geoweaver_jar()
subprocess.run(["chmod", "+x", get_geoweaver_jar_path()], cwd=f"{get_root_dir()}/")
subprocess.run(
Expand All @@ -31,6 +45,17 @@ def list_processes():


def list_processes_in_workflow(workflow_id):
"""
List processes in a specific workflow.
Downloads the Geoweaver JAR and queries the Geoweaver server for details of a workflow.
Extracts information about nodes in the workflow and returns a list of dictionaries containing 'title' and 'id'.
:param workflow_id: The ID of the workflow.
:type workflow_id: str
:return: List of dictionaries containing 'title' and 'id' of processes in the workflow.
:rtype: list
"""
download_geoweaver_jar()
payload = {"id": workflow_id, "type": "workflow"}
r = requests.post(
Expand All @@ -47,8 +72,16 @@ def list_processes_in_workflow(workflow_id):


def list_workflows():
"""
List all workflows in Geoweaver.
Downloads the Geoweaver JAR, then runs the 'list' command with the '--workflow' option.
Note: Requires Geoweaver to be initialized and the JAR file to be available.
"""
download_geoweaver_jar()
subprocess.run(
[get_java_bin_path(), "-jar", get_geoweaver_jar_path(), "list", "--workflow"],
cwd=f"{get_root_dir()}/",
)

19 changes: 18 additions & 1 deletion pygeoweaver/sc_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@


def sync(process_id: str, local_path: typing.Union[str, os.PathLike], direction: str):
"""
Sync code for a Geoweaver process between the local machine and the Geoweaver server.
:param process_id: The ID of the Geoweaver process.
:type process_id: str
:param local_path: The local path to save or load the process code.
:type local_path: Union[str, os.PathLike]
:param direction: The direction of the sync, either "download" or "upload".
:type direction: str
"""
print(f"Proceeding with {direction}\n")
if direction == "download":
if not local_path:
Expand Down Expand Up @@ -60,8 +70,15 @@ def sync(process_id: str, local_path: typing.Union[str, os.PathLike], direction:
"Please specify the direction to sync. Choices - [UPLOAD, DOWNLOAD]"
)


def sync_workflow(workflow_id: str, sync_to_path: typing.Union[str, os.PathLike]):
"""
Sync a Geoweaver workflow, including its code and history, between the local machine and the Geoweaver server.
:param workflow_id: The ID of the Geoweaver workflow.
:type workflow_id: str
:param sync_to_path: The local path to sync the Geoweaver workflow.
:type sync_to_path: Union[str, os.PathLike]
"""
download_geoweaver_jar()
# download workflow
r = requests.post(
Expand Down
1 change: 1 addition & 0 deletions pygeoweaver/start.bat
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ taskkill /f /im geoweaver.exe > nul
echo Check Java..

echo Start Geoweaver..
echo javaw -jar "%USERPROFILE%\geoweaver.jar"
start /b javaw -jar "%USERPROFILE%\geoweaver.jar"

set STATUS=0
Expand Down

0 comments on commit cec2089

Please sign in to comment.