| 1 |
# This code is part of the Ansible collection community.docker, but is an independent component. |
| 2 |
# This particular file, and this file only, is based on containerd's platforms Go module |
| 3 |
# (https://github.com/containerd/containerd/tree/main/platforms) |
| 4 |
# |
| 5 |
# Copyright (c) 2023 Felix Fontein <[email protected]> |
| 6 |
# Copyright The containerd Authors |
| 7 |
# |
| 8 |
# It is licensed under the Apache 2.0 license (see LICENSES/Apache-2.0.txt in this collection) |
| 9 |
# SPDX-License-Identifier: Apache-2.0 |
| 10 |
|
| 11 |
# Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time. |
| 12 |
# Do not use this from other collections or standalone plugins/modules! |
| 13 |
|
| 14 |
from __future__ import annotations |
| 15 |
|
| 16 |
import re |
| 17 |
import typing as t |
| 18 |
|
| 19 |
_VALID_STR = re.compile("^[A-Za-z0-9_-]+$") |
| 20 |
|
| 21 |
|
| 22 |
def _validate_part(string: str, part: str, part_name: str) -> str: |
| 23 |
if not part: |
| 24 |
raise ValueError(f'Invalid platform string "{string}": {part_name} is empty') |
| 25 |
if not _VALID_STR.match(part): |
| 26 |
raise ValueError( |
| 27 |
f'Invalid platform string "{string}": {part_name} has invalid characters' |
| 28 |
) |
| 29 |
return part |
| 30 |
|
| 31 |
|
| 32 |
# See https://github.com/containerd/containerd/blob/main/platforms/database.go#L32-L38 |
| 33 |
_KNOWN_OS = ( |
| 34 |
"aix", |
| 35 |
"android", |
| 36 |
"darwin", |
| 37 |
"dragonfly", |
| 38 |
"freebsd", |
| 39 |
"hurd", |
| 40 |
"illumos", |
| 41 |
"ios", |
| 42 |
"js", |
| 43 |
"linux", |
| 44 |
"nacl", |
| 45 |
"netbsd", |
| 46 |
"openbsd", |
| 47 |
"plan9", |
| 48 |
"solaris", |
| 49 |
"windows", |
| 50 |
"zos", |
| 51 |
) |
| 52 |
|
| 53 |
# See https://github.com/containerd/containerd/blob/main/platforms/database.go#L54-L60 |
| 54 |
_KNOWN_ARCH = ( |
| 55 |
"386", |
| 56 |
"amd64", |
| 57 |
"amd64p32", |
| 58 |
"arm", |
| 59 |
"armbe", |
| 60 |
"arm64", |
| 61 |
"arm64be", |
| 62 |
"ppc64", |
| 63 |
"ppc64le", |
| 64 |
"loong64", |
| 65 |
"mips", |
| 66 |
"mipsle", |
| 67 |
"mips64", |
| 68 |
"mips64le", |
| 69 |
"mips64p32", |
| 70 |
"mips64p32le", |
| 71 |
"ppc", |
| 72 |
"riscv", |
| 73 |
"riscv64", |
| 74 |
"s390", |
| 75 |
"s390x", |
| 76 |
"sparc", |
| 77 |
"sparc64", |
| 78 |
"wasm", |
| 79 |
) |
| 80 |
|
| 81 |
|
| 82 |
def _normalize_os(os_str: str) -> str: |
| 83 |
# See normalizeOS() in https://github.com/containerd/containerd/blob/main/platforms/database.go |
| 84 |
os_str = os_str.lower() |
| 85 |
if os_str == "macos": |
| 86 |
os_str = "darwin" |
| 87 |
return os_str |
| 88 |
|
| 89 |
|
| 90 |
_NORMALIZE_ARCH = { |
| 91 |
("i386", None): ("386", ""), |
| 92 |
("x86_64", "v1"): ("amd64", ""), |
| 93 |
("x86-64", "v1"): ("amd64", ""), |
| 94 |
("amd64", "v1"): ("amd64", ""), |
| 95 |
("x86_64", None): ("amd64", None), |
| 96 |
("x86-64", None): ("amd64", None), |
| 97 |
("amd64", None): ("amd64", None), |
| 98 |
("aarch64", "8"): ("arm64", ""), |
| 99 |
("arm64", "8"): ("arm64", ""), |
| 100 |
("aarch64", "v8"): ("arm64", ""), |
| 101 |
("arm64", "v8"): ("arm64", ""), |
| 102 |
("aarch64", None): ("arm64", None), |
| 103 |
("arm64", None): ("arm64", None), |
| 104 |
("armhf", None): ("arm", "v7"), |
| 105 |
("armel", None): ("arm", "v6"), |
| 106 |
("arm", ""): ("arm", "v7"), |
| 107 |
("arm", "5"): ("arm", "v5"), |
| 108 |
("arm", "6"): ("arm", "v6"), |
| 109 |
("arm", "7"): ("arm", "v7"), |
| 110 |
("arm", "8"): ("arm", "v8"), |
| 111 |
("arm", None): ("arm", None), |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
def _normalize_arch(arch_str: str, variant_str: str) -> tuple[str, str]: |
| 116 |
# See normalizeArch() in https://github.com/containerd/containerd/blob/main/platforms/database.go |
| 117 |
arch_str = arch_str.lower() |
| 118 |
variant_str = variant_str.lower() |
| 119 |
res = _NORMALIZE_ARCH.get((arch_str, variant_str)) |
| 120 |
if res is None: |
| 121 |
res = _NORMALIZE_ARCH.get((arch_str, None)) |
| 122 |
if res is None: |
| 123 |
return arch_str, variant_str |
| 124 |
arch_str = res[0] |
| 125 |
if res[1] is not None: |
| 126 |
variant_str = res[1] |
| 127 |
return arch_str, variant_str |
| 128 |
|
| 129 |
|
| 130 |
class _Platform: |
| 131 |
def __init__( |
| 132 |
self, os: str | None = None, arch: str | None = None, variant: str | None = None |
| 133 |
) -> None: |
| 134 |
self.os = os |
| 135 |
self.arch = arch |
| 136 |
self.variant = variant |
| 137 |
if variant is not None: |
| 138 |
if arch is None: |
| 139 |
raise ValueError("If variant is given, architecture must be given too") |
| 140 |
if os is None: |
| 141 |
raise ValueError("If variant is given, os must be given too") |
| 142 |
|
| 143 |
@classmethod |
| 144 |
def parse_platform_string( |
| 145 |
cls, |
| 146 |
string: str | None, |
| 147 |
daemon_os: str | None = None, |
| 148 |
daemon_arch: str | None = None, |
| 149 |
) -> t.Self: |
| 150 |
# See Parse() in https://github.com/containerd/containerd/blob/main/platforms/platforms.go |
| 151 |
if string is None: |
| 152 |
return cls() |
| 153 |
if not string: |
| 154 |
raise ValueError("Platform string must be non-empty") |
| 155 |
parts = string.split("/", 2) |
| 156 |
arch = None |
| 157 |
variant = None |
| 158 |
if len(parts) == 1: |
| 159 |
_validate_part(string, string, "OS/architecture") |
| 160 |
# The part is either OS or architecture |
| 161 |
os = _normalize_os(string) |
| 162 |
if os in _KNOWN_OS: |
| 163 |
if daemon_arch is not None: |
| 164 |
arch, variant = _normalize_arch(daemon_arch, "") |
| 165 |
return cls(os=os, arch=arch, variant=variant) |
| 166 |
arch, variant = _normalize_arch(os, "") |
| 167 |
if arch in _KNOWN_ARCH: |
| 168 |
return cls( |
| 169 |
os=_normalize_os(daemon_os) if daemon_os else None, |
| 170 |
arch=arch or None, |
| 171 |
variant=variant or None, |
| 172 |
) |
| 173 |
raise ValueError( |
| 174 |
f'Invalid platform string "{string}": unknown OS or architecture' |
| 175 |
) |
| 176 |
os = _validate_part(string, parts[0], "OS") |
| 177 |
if not os: |
| 178 |
raise ValueError(f'Invalid platform string "{string}": OS is empty') |
| 179 |
arch = ( |
| 180 |
_validate_part(string, parts[1], "architecture") if len(parts) > 1 else None |
| 181 |
) |
| 182 |
if arch is not None and not arch: |
| 183 |
raise ValueError( |
| 184 |
f'Invalid platform string "{string}": architecture is empty' |
| 185 |
) |
| 186 |
variant = ( |
| 187 |
_validate_part(string, parts[2], "variant") if len(parts) > 2 else None |
| 188 |
) |
| 189 |
if variant is not None and not variant: |
| 190 |
raise ValueError(f'Invalid platform string "{string}": variant is empty') |
| 191 |
assert arch is not None # otherwise variant would be None as well |
| 192 |
arch, variant = _normalize_arch(arch, variant or "") |
| 193 |
if len(parts) == 2 and arch == "arm" and variant == "v7": |
| 194 |
variant = None |
| 195 |
if len(parts) == 3 and arch == "arm64" and variant == "": |
| 196 |
variant = "v8" |
| 197 |
return cls(os=_normalize_os(os), arch=arch, variant=variant or None) |
| 198 |
|
| 199 |
def __str__(self) -> str: |
| 200 |
if self.variant: |
| 201 |
assert ( |
| 202 |
self.os is not None and self.arch is not None |
| 203 |
) # ensured in constructor |
| 204 |
parts: list[str] = [self.os, self.arch, self.variant] |
| 205 |
elif self.os: |
| 206 |
if self.arch: |
| 207 |
parts = [self.os, self.arch] |
| 208 |
else: |
| 209 |
parts = [self.os] |
| 210 |
elif self.arch is not None: |
| 211 |
parts = [self.arch] |
| 212 |
else: |
| 213 |
parts = [] |
| 214 |
return "/".join(parts) |
| 215 |
|
| 216 |
def __repr__(self) -> str: |
| 217 |
return ( |
| 218 |
f"_Platform(os={self.os!r}, arch={self.arch!r}, variant={self.variant!r})" |
| 219 |
) |
| 220 |
|
| 221 |
def __eq__(self, other: object) -> bool: |
| 222 |
if not isinstance(other, _Platform): |
| 223 |
return NotImplemented |
| 224 |
return ( |
| 225 |
self.os == other.os |
| 226 |
and self.arch == other.arch |
| 227 |
and self.variant == other.variant |
| 228 |
) |
| 229 |
|
| 230 |
|
| 231 |
def normalize_platform_string( |
| 232 |
string: str, daemon_os: str | None = None, daemon_arch: str | None = None |
| 233 |
) -> str: |
| 234 |
return str( |
| 235 |
_Platform.parse_platform_string( |
| 236 |
string, daemon_os=daemon_os, daemon_arch=daemon_arch |
| 237 |
) |
| 238 |
) |
| 239 |
|
| 240 |
|
| 241 |
def compose_platform_string( |
| 242 |
os: str | None = None, |
| 243 |
arch: str | None = None, |
| 244 |
variant: str | None = None, |
| 245 |
daemon_os: str | None = None, |
| 246 |
daemon_arch: str | None = None, |
| 247 |
) -> str: |
| 248 |
if os is None and daemon_os is not None: |
| 249 |
os = _normalize_os(daemon_os) |
| 250 |
if arch is None and daemon_arch is not None: |
| 251 |
arch, variant = _normalize_arch(daemon_arch, variant or "") |
| 252 |
variant = variant or None |
| 253 |
return str(_Platform(os=os, arch=arch, variant=variant or None)) |
| 254 |
|
| 255 |
|
| 256 |
def compare_platform_strings(string1: str, string2: str) -> bool: |
| 257 |
return _Platform.parse_platform_string(string1) == _Platform.parse_platform_string( |
| 258 |
string2 |
| 259 |
) |