|
5 | 5 | __license__ = "Apache-2.0" |
6 | 6 |
|
7 | 7 | import json |
| 8 | +import os |
8 | 9 | import pathlib |
9 | | -from typing import TYPE_CHECKING |
| 10 | +import re |
| 11 | +import shutil |
| 12 | +import tempfile |
| 13 | +import threading |
| 14 | +from typing import TYPE_CHECKING, BinaryIO |
10 | 15 |
|
11 | 16 | import jsonschema |
12 | 17 | import requests |
|
22 | 27 | from oras.utils.fileio import read_json, write_json |
23 | 28 |
|
24 | 29 | if TYPE_CHECKING: |
25 | | - from oras.copy.adapters import LayoutTarget |
26 | 30 | from oras.copy.descriptor import Descriptor |
27 | 31 | from oras.copy.options import CopyOptions |
28 | 32 | from oras.provider import Registry |
29 | 33 |
|
30 | 34 |
|
| 35 | +_VALID_DIGEST_RE = re.compile(r"^[a-z0-9]+:[a-f0-9]+$") |
| 36 | + |
| 37 | + |
| 38 | + |
31 | 39 | def NewLayout(path: str, validate: bool = True) -> Layout: |
32 | 40 | """ |
33 | 41 | Courtesy function to create a Layout from a path on disk. |
@@ -530,10 +538,8 @@ def as_target(self) -> LayoutTarget: |
530 | 538 | :func:`oras.copy.copy` as either source or destination. |
531 | 539 |
|
532 | 540 | :return: a Target adapter wrapping this layout |
533 | | - :rtype: oras.copy.adapters.LayoutTarget |
| 541 | + :rtype: oras.layout.layout.LayoutTarget |
534 | 542 | """ |
535 | | - from oras.copy.adapters import LayoutTarget |
536 | | - |
537 | 543 | return LayoutTarget(self) |
538 | 544 |
|
539 | 545 | def copy_to_registry( |
@@ -568,7 +574,7 @@ def copy_to_registry( |
568 | 574 | """ |
569 | 575 | from oras.container import Container |
570 | 576 | from oras.copy import copy as copy_fn |
571 | | - from oras.copy.adapters import LayoutTarget, RegistryTarget |
| 577 | + from oras.provider import RegistryTarget |
572 | 578 |
|
573 | 579 | src = LayoutTarget(self) |
574 | 580 | dst = RegistryTarget(provider, target, opts) |
@@ -605,7 +611,7 @@ def copy_from_registry( |
605 | 611 | """ |
606 | 612 | from oras.container import Container |
607 | 613 | from oras.copy import copy as copy_fn |
608 | | - from oras.copy.adapters import LayoutTarget, RegistryTarget |
| 614 | + from oras.provider import RegistryTarget |
609 | 615 |
|
610 | 616 | src = RegistryTarget(provider, source, opts) |
611 | 617 | dst = LayoutTarget(self) |
@@ -709,3 +715,150 @@ def pull_from_registry( |
709 | 715 | logger.debug( |
710 | 716 | f"Successfully pulled {target} to OCI layout at {self._oci_layout_path}" |
711 | 717 | ) |
| 718 | + |
| 719 | + |
| 720 | +class LayoutTarget: |
| 721 | + """ |
| 722 | + Adapts a :class:`Layout` directory into the copy engine's Target protocol. |
| 723 | +
|
| 724 | + Provides full read/write access to the OCI layout's content-addressable |
| 725 | + blobs and manages references via the layout's index.json annotations. |
| 726 | +
|
| 727 | + Implements Target (fetch, exists, push, tag, resolve), so it can serve |
| 728 | + as both source and destination in :func:`oras.copy.copy`. |
| 729 | + """ |
| 730 | + |
| 731 | + def __init__(self, layout: Layout): |
| 732 | + self._layout = layout |
| 733 | + self._index_lock = threading.Lock() |
| 734 | + |
| 735 | + @staticmethod |
| 736 | + def _validate_digest(digest: str) -> None: |
| 737 | + if not _VALID_DIGEST_RE.match(digest): |
| 738 | + raise ValueError(f"invalid digest format: {digest!r}") |
| 739 | + |
| 740 | + def _ensure_initialized(self) -> None: |
| 741 | + """Create OCI layout structure (oci-layout, index.json, blobs/) if missing.""" |
| 742 | + layout_dir = pathlib.Path(self._layout._oci_layout_path) |
| 743 | + layout_dir.mkdir(parents=True, exist_ok=True) |
| 744 | + (layout_dir / oras.defaults.oci_blobs_dir).mkdir(exist_ok=True) |
| 745 | + |
| 746 | + oci_layout_path = layout_dir / oras.defaults.oci_layout_file |
| 747 | + if not oci_layout_path.exists(): |
| 748 | + write_json( |
| 749 | + {"imageLayoutVersion": oras.defaults.oci_layout_version_pin}, |
| 750 | + str(oci_layout_path), |
| 751 | + ) |
| 752 | + |
| 753 | + index_path = layout_dir / oras.defaults.oci_image_index_file |
| 754 | + if not index_path.exists(): |
| 755 | + write_json( |
| 756 | + { |
| 757 | + "schemaVersion": oras.defaults.oci_index_schema_version, |
| 758 | + "manifests": [], |
| 759 | + }, |
| 760 | + str(index_path), |
| 761 | + ) |
| 762 | + |
| 763 | + def fetch(self, desc: Descriptor) -> BinaryIO: |
| 764 | + """Fetch blob content by digest, returning an open file handle.""" |
| 765 | + digest = desc["digest"] |
| 766 | + self._validate_digest(digest) |
| 767 | + path = self._layout.digest_to_blob_path(digest) |
| 768 | + return open(path, "rb") |
| 769 | + |
| 770 | + def exists(self, desc: Descriptor) -> bool: |
| 771 | + """Check if a blob exists on disk.""" |
| 772 | + digest = desc["digest"] |
| 773 | + self._validate_digest(digest) |
| 774 | + path = self._layout.digest_to_blob_path(digest) |
| 775 | + return path.exists() |
| 776 | + |
| 777 | + def push(self, desc: Descriptor, content: BinaryIO) -> None: |
| 778 | + """Write blob content to the layout (content-addressed, deduplicated). |
| 779 | +
|
| 780 | + Matching oras-go's Store.Push: skips silently if the blob already |
| 781 | + exists (same digest), otherwise writes atomically via a temp file + |
| 782 | + rename so concurrent writers never observe a partial blob. |
| 783 | + """ |
| 784 | + digest = desc["digest"] |
| 785 | + self._validate_digest(digest) |
| 786 | + |
| 787 | + self._ensure_initialized() |
| 788 | + |
| 789 | + path = self._layout.digest_to_blob_path(digest) |
| 790 | + if path.exists(): |
| 791 | + return # already present — deduplicate silently |
| 792 | + |
| 793 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 794 | + |
| 795 | + tmp_fd, tmp_name = tempfile.mkstemp(dir=str(path.parent)) |
| 796 | + try: |
| 797 | + # Stream rather than buffer: large blobs must not be fully |
| 798 | + # materialized in memory. |
| 799 | + with os.fdopen(tmp_fd, "wb") as f: |
| 800 | + shutil.copyfileobj(content, f) |
| 801 | + os.replace(tmp_name, str(path)) |
| 802 | + except Exception: |
| 803 | + try: |
| 804 | + os.unlink(tmp_name) |
| 805 | + except OSError: |
| 806 | + pass |
| 807 | + raise |
| 808 | + |
| 809 | + def tag(self, desc: Descriptor, reference: str) -> None: |
| 810 | + """Update index.json to associate the descriptor with a reference tag. |
| 811 | +
|
| 812 | + Matching oras-go's Store.Tag: finds an existing index.json entry for |
| 813 | + this reference and replaces it, or appends a new one. |
| 814 | + Thread-safe via _index_lock. |
| 815 | + """ |
| 816 | + self._ensure_initialized() |
| 817 | + |
| 818 | + layout_dir = pathlib.Path(self._layout._oci_layout_path) |
| 819 | + index_path = layout_dir / oras.defaults.oci_image_index_file |
| 820 | + |
| 821 | + new_entry = { |
| 822 | + "mediaType": desc.get("mediaType", ""), |
| 823 | + "digest": desc["digest"], |
| 824 | + "size": desc.get("size", 0), |
| 825 | + "annotations": {oras.defaults.oci_ref_name_annotation: reference}, |
| 826 | + } |
| 827 | + |
| 828 | + with self._index_lock: |
| 829 | + index_data = read_json(str(index_path)) |
| 830 | + manifests = index_data.setdefault("manifests", []) |
| 831 | + |
| 832 | + for i, entry in enumerate(manifests): |
| 833 | + if ( |
| 834 | + entry.get("annotations", {}).get( |
| 835 | + oras.defaults.oci_ref_name_annotation |
| 836 | + ) |
| 837 | + == reference |
| 838 | + ): |
| 839 | + manifests[i] = new_entry |
| 840 | + break |
| 841 | + else: |
| 842 | + manifests.append(new_entry) |
| 843 | + |
| 844 | + write_json(index_data, str(index_path)) |
| 845 | + |
| 846 | + def resolve(self, reference: str) -> Descriptor: |
| 847 | + """Resolve a reference tag to a descriptor via index.json.""" |
| 848 | + layout_dir = pathlib.Path(self._layout._oci_layout_path) |
| 849 | + index_path = layout_dir / oras.defaults.oci_image_index_file |
| 850 | + index_data = read_json(str(index_path)) |
| 851 | + for entry in index_data.get("manifests", []): |
| 852 | + annotations = entry.get("annotations", {}) |
| 853 | + if ( |
| 854 | + annotations.get(oras.defaults.oci_ref_name_annotation) |
| 855 | + == reference |
| 856 | + ): |
| 857 | + return { |
| 858 | + "mediaType": entry.get("mediaType", ""), |
| 859 | + "digest": entry.get("digest", ""), |
| 860 | + "size": entry.get("size", 0), |
| 861 | + } |
| 862 | + raise FileNotFoundError( |
| 863 | + f"Reference not found in layout index: {reference}" |
| 864 | + ) |
0 commit comments