|
| 1 | +"""Pushes a commit to the Openlayer platform.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import tarfile |
| 5 | +import tempfile |
| 6 | +from typing import Optional |
| 7 | + |
| 8 | + |
| 9 | +from ... import Openlayer |
| 10 | +from . import StorageType, _upload |
| 11 | + |
| 12 | + |
| 13 | +def push( |
| 14 | + client: Openlayer, |
| 15 | + directory: str, |
| 16 | + project_id: str, |
| 17 | + message: str = "New commit", |
| 18 | + storage_type: Optional[StorageType] = None, |
| 19 | +) -> None: |
| 20 | + """Push a new commit to the Openlayer platform. |
| 21 | +
|
| 22 | + This is equivalent to running `openlayer push` from the Openlayer CLI.""" |
| 23 | + if not os.path.exists(directory): |
| 24 | + raise ValueError(f"Directory {directory} does not exist.") |
| 25 | + |
| 26 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 27 | + tar_file_path = os.path.join(tmp_dir, "bundle.tar") |
| 28 | + with tarfile.open(tar_file_path, mode="w") as tar: |
| 29 | + tar.add(directory, arcname=os.path.basename(directory)) |
| 30 | + |
| 31 | + # Upload tar storage |
| 32 | + uploader = _upload.Uploader(client, storage_type) |
| 33 | + object_name = "bundle.tar" |
| 34 | + presigned_url_response = client.storage.presigned_url.create( |
| 35 | + object_name=object_name, |
| 36 | + ) |
| 37 | + uploader.upload( |
| 38 | + file_path=tar_file_path, |
| 39 | + object_name=object_name, |
| 40 | + presigned_url_response=presigned_url_response, |
| 41 | + ) |
| 42 | + |
| 43 | + # Create the project version (commit) |
| 44 | + client.projects.commits.create( |
| 45 | + project_id=project_id, |
| 46 | + commit={"message": message, "source": "cli"}, |
| 47 | + storage_uri=presigned_url_response.storage_uri, |
| 48 | + ) |
0 commit comments