File: registry_data.py

package info (click to toggle)
python-podman 5.4.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,140 kB
  • sloc: python: 7,532; makefile: 82; sh: 75
file content (88 lines) | stat: -rw-r--r-- 2,964 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""Module for tracking registry metadata."""

import logging
from typing import Any, Optional, Union
from collections.abc import Mapping

from podman import api
from podman.domain.images import Image
from podman.domain.manager import PodmanResource
from podman.errors import InvalidArgument

logger = logging.getLogger("podman.images")


class RegistryData(PodmanResource):
    """Registry metadata about Image."""

    def __init__(self, image_name: str, *args, **kwargs) -> None:
        """Initialize RegistryData object.

        Args:
            image_name: Name of Image.

        Keyword Args:
            client (APIClient): Configured connection to a Podman service.
            collection (Manager): Manager of this category of resource,
                named `collection` for compatibility

        """
        super().__init__(*args, **kwargs)
        self.image_name = image_name

        self.attrs = kwargs.get("attrs")
        if self.attrs is None:
            self.attrs = self.manager.get(image_name).attrs

    def pull(self, platform: Optional[str] = None) -> Image:
        """Returns Image pulled by identifier.

        Args:
            platform: Platform for which to pull Image. Default: None (all platforms.)
        """
        repository = api.parse_repository(self.image_name)
        return self.manager.pull(repository, tag=self.id, platform=platform)

    def has_platform(self, platform: Union[str, Mapping[str, Any]]) -> bool:
        """Returns True if platform is available for Image.

        Podman API does not support "variant" therefore it is ignored.

        Args:
            platform: Name as os[/arch[/variant]] or Mapping[str,Any]

        Returns:
            True if platform is available

        Raises:
            InvalidArgument: when platform value is not valid
            APIError: when service reports an error
        """
        invalid_platform = InvalidArgument(f"'{platform}' is not a valid platform descriptor.")

        if platform is None:
            platform = {}

        if isinstance(platform, dict):
            if not {"os", "architecture"} <= platform.keys():
                version = self.client.version()
                platform["os"] = platform.get("os", version["Os"])
                platform["architecture"] = platform.get("architecture", version["Arch"])
        elif isinstance(platform, str):
            elements = platform.split("/")
            if 1 < len(elements) > 3:
                raise invalid_platform

            platform = {"os": elements[0]}
            if len(elements) > 2:
                platform["variant"] = elements[2]
            if len(elements) > 1:
                platform["architecture"] = elements[1]
        else:
            raise invalid_platform

        return (
            # Variant not carried in libpod attrs
            platform["os"] == self.attrs["Os"]
            and platform["architecture"] == self.attrs["Architecture"]
        )