Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/openlayer/lib/data/commit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Pushes a commit to the Openlayer platform."""

import os
import tarfile
import tempfile
from typing import Optional


from ... import Openlayer
from . import StorageType, _upload


def push(
client: Openlayer,
directory: str,
project_id: str,
message: str = "New commit",
storage_type: Optional[StorageType] = None,
) -> None:
"""Push a new commit to the Openlayer platform.

This is equivalent to running `openlayer push` from the Openlayer CLI."""
if not os.path.exists(directory):
raise ValueError(f"Directory {directory} does not exist.")

with tempfile.TemporaryDirectory() as tmp_dir:
tar_file_path = os.path.join(tmp_dir, "bundle.tar")
with tarfile.open(tar_file_path, mode="w") as tar:
tar.add(directory, arcname=os.path.basename(directory))

# Upload tar storage
uploader = _upload.Uploader(client, storage_type)
object_name = "bundle.tar"
presigned_url_response = client.storage.presigned_url.create(
object_name=object_name,
)
uploader.upload(
file_path=tar_file_path,
object_name=object_name,
presigned_url_response=presigned_url_response,
)

# Create the project version (commit)
client.projects.commits.create(
project_id=project_id,
commit={"message": message, "source": "cli"},
storage_uri=presigned_url_response.storage_uri,
)