From 61d3f4de7e01917765e3afe89ca513b66ecdaba9 Mon Sep 17 00:00:00 2001 From: Gustavo Cid Ornelas Date: Thu, 13 Mar 2025 17:07:40 -0300 Subject: [PATCH 1/2] feat: add wait_for_commit_completion convenience method --- src/openlayer/lib/data/commit.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/openlayer/lib/data/commit.py b/src/openlayer/lib/data/commit.py index e94e8ff7..97b9f645 100644 --- a/src/openlayer/lib/data/commit.py +++ b/src/openlayer/lib/data/commit.py @@ -3,11 +3,13 @@ import os import tarfile import tempfile +import time from typing import Optional from ... import Openlayer from . import StorageType, _upload +from ...types.commit_retrieve_response import CommitRetrieveResponse def push( @@ -46,3 +48,30 @@ def push( commit={"message": message, "source": "cli"}, storage_uri=presigned_url_response.storage_uri, ) + + +def wait_for_commit_completion( + client: Openlayer, project_version_id: str, verbose: bool = True +) -> CommitRetrieveResponse: + """Wait for a commit to be processed by the Openlayer platform. + + Waits until the commit status is "completed" or "failed". + """ + while True: + commit = client.commits.retrieve(project_version_id=project_version_id) + if commit.status == "completed": + if verbose: + print(f"Commit {project_version_id} completed successfully.") + return commit + elif commit.status == "failed": + raise Exception( + f"Commit {project_version_id} failed with status message:" + f" {commit.status_message}" + ) + else: + if verbose: + print( + f"Commit {project_version_id} is still processing (status:" + f" {commit.status})..." + ) + time.sleep(1) From 234afc8963ae0ac2fc0ab15a732f5820a8af8df7 Mon Sep 17 00:00:00 2001 From: Gustavo Cid Ornelas Date: Tue, 18 Mar 2025 09:15:30 -0300 Subject: [PATCH 2/2] feat: add option to wait for commit completion to push function --- src/openlayer/lib/data/commit.py | 41 +++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/src/openlayer/lib/data/commit.py b/src/openlayer/lib/data/commit.py index 97b9f645..b46ced99 100644 --- a/src/openlayer/lib/data/commit.py +++ b/src/openlayer/lib/data/commit.py @@ -18,10 +18,28 @@ def push( project_id: str, message: str = "New commit", storage_type: Optional[StorageType] = None, -) -> None: + wait_for_completion: bool = False, + verbose: bool = False, +) -> Optional[CommitRetrieveResponse]: """Push a new commit to the Openlayer platform. - This is equivalent to running `openlayer push` from the Openlayer CLI.""" + This is equivalent to running `openlayer push` from the Openlayer CLI. + + If `wait_for_completion` is True, the function will wait for the commit to be + completed and return the commit object. + + Args: + client: The Openlayer client. + directory: The directory to push. + project_id: The id of the project to push to. + message: The commit message. + storage_type: The storage type to use. + wait_for_completion: Whether to wait for the commit to be completed. + verbose: Whether to print verbose output. + + Returns: + The commit object if `wait_for_completion` is True, otherwise None. + """ if not os.path.exists(directory): raise ValueError(f"Directory {directory} does not exist.") @@ -43,12 +61,21 @@ def push( ) # Create the project version (commit) - client.projects.commits.create( + commit = client.projects.commits.create( project_id=project_id, commit={"message": message, "source": "cli"}, storage_uri=presigned_url_response.storage_uri, ) + if wait_for_completion: + return wait_for_commit_completion( + client=client, + project_version_id=commit.id, + verbose=verbose, + ) + + return None + def wait_for_commit_completion( client: Openlayer, project_version_id: str, verbose: bool = True @@ -56,6 +83,14 @@ def wait_for_commit_completion( """Wait for a commit to be processed by the Openlayer platform. Waits until the commit status is "completed" or "failed". + + Args: + client: The Openlayer client. + project_version_id: The id of the project version (commit) to wait for. + verbose: Whether to print verbose output. + + Returns: + The commit object. """ while True: commit = client.commits.retrieve(project_version_id=project_version_id)