|
| 1 | +import os |
| 2 | + |
| 3 | +import oras.client |
| 4 | + |
| 5 | +from alphatrion import consts |
| 6 | +from alphatrion.runtime.runtime import Runtime |
| 7 | + |
| 8 | +SUCCESS_CODE = 201 |
| 9 | + |
| 10 | + |
| 11 | +class Artifact: |
| 12 | + def __init__(self, runtime: Runtime): |
| 13 | + self._runtime = runtime |
| 14 | + self._url = os.environ.get(consts.ARTIFACT_REGISTRY_URL) |
| 15 | + self._url = self._url.replace("https://", "").replace("http://", "") |
| 16 | + self._client = oras.client.OrasClient( |
| 17 | + hostname=self._url.strip("/"), auth_backend="token" |
| 18 | + ) |
| 19 | + |
| 20 | + def push(self, experiment_name: str, files: list[str], version: str = "latest"): |
| 21 | + url = self._url if self._url.endswith("/") else f"{self._url}/" |
| 22 | + |
| 23 | + target = f"{url}{self._runtime._project_id}/{experiment_name}:{version}" |
| 24 | + |
| 25 | + try: |
| 26 | + self._client.push(target, files=files) |
| 27 | + except Exception as e: |
| 28 | + raise RuntimeError("Failed to push artifacts") from e |
| 29 | + |
| 30 | + def list_tags(self, experiment_name: str) -> list[str]: |
| 31 | + url = self._url if self._url.endswith("/") else f"{self._url}/" |
| 32 | + target = f"{url}{self._runtime._project_id}/{experiment_name}" |
| 33 | + try: |
| 34 | + tags = self._client.get_tags(target) |
| 35 | + return tags |
| 36 | + except Exception as e: |
| 37 | + raise RuntimeError("Failed to list artifacts tags") from e |
| 38 | + |
| 39 | + def delete_tags(self, experiment_name: str, versions: str | list): |
| 40 | + url = self._url if self._url.endswith("/") else f"{self._url}/" |
| 41 | + target = f"{url}{self._runtime._project_id}/{experiment_name}" |
| 42 | + |
| 43 | + try: |
| 44 | + self._client.delete_tags(target, tags=versions) |
| 45 | + except Exception as e: |
| 46 | + raise RuntimeError("Failed to delete tags") from e |
0 commit comments