Skip to content

Commit d221142

Browse files
committed
Scope scratch OS empty-field defaults to ImageVersionOS
Move _OSLESS_NAMES and the scratch-aware default_factory out of shared.py into version_os.py. The shared ExtensionField/ TagDisplayNameField are used by ImageVariant too; keeping the scratch check there would silently produce empty extension/ tagDisplayName for any variant named "scratch", risking tag and Containerfile collisions. - shared.py: restore original default_factory and + patterns - version_os.py: own _OSLESS_NAMES and * patterns locally
1 parent c8589ac commit d221142

2 files changed

Lines changed: 25 additions & 16 deletions

File tree

posit-bakery/posit_bakery/config/image/version_os.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
from pydantic import BaseModel, Field, field_validator, field_serializer
66
from pydantic_core.core_schema import ValidationInfo
77

8-
from posit_bakery.config.shared import BakeryYAMLModel, ExtensionField, TagDisplayNameField
8+
from posit_bakery.config.shared import BakeryYAMLModel
9+
from posit_bakery.const import REGEX_IMAGE_TAG_SUFFIX_ALLOWED_CHARACTERS_PATTERN
910
from .build_os import BuildOS, SUPPORTED_OS, ALTERNATE_NAMES, TargetPlatform, DEFAULT_PLATFORMS
1011
from .posit_product.const import URL_WITH_ENV_VARS_REGEX_PATTERN
1112

13+
_OSLESS_NAMES = frozenset({"scratch"})
14+
1215
log = logging.getLogger(__name__)
1316

1417
_NAME_VERSION_PATTERN = re.compile(r"(?P<name>[\D]+)(?P<version>[\d.]*)")
@@ -63,16 +66,30 @@ class ImageVersionOS(BakeryYAMLModel):
6366
Field(default=DEFAULT_PLATFORMS, description="List of platforms to build for this image."),
6467
]
6568
extension: Annotated[
66-
ExtensionField,
69+
str,
6770
Field(
71+
default_factory=lambda data: (
72+
""
73+
if data.get("name", "").lower().strip() in _OSLESS_NAMES
74+
else re.sub(r"[^a-zA-Z0-9_-]", "", data.get("name", "").lower())
75+
),
76+
pattern=r"^[a-zA-Z0-9_-]*$",
77+
validate_default=True,
6878
description="File extension used in the Containerfile filename in the pattern "
6979
"Containerfile.<os>.<variant> for this OS. Set to an empty string if no extension is needed.",
7080
examples=["ubuntu2204", "debian12"],
7181
),
7282
]
7383
tagDisplayName: Annotated[
74-
TagDisplayNameField,
84+
str,
7585
Field(
86+
default_factory=lambda data: (
87+
""
88+
if data.get("name", "").lower().strip() in _OSLESS_NAMES
89+
else re.sub(REGEX_IMAGE_TAG_SUFFIX_ALLOWED_CHARACTERS_PATTERN, "-", data.get("name", "").lower())
90+
),
91+
pattern=r"^[a-zA-Z0-9_.-]*$",
92+
validate_default=True,
7693
description="The name used in image tags for this OS. This is used to create the tag "
7794
"in the format <image>:<version>-<os>-<variant>.",
7895
examples=["ubuntu-22.04", "debian-12"],

posit-bakery/posit_bakery/config/shared.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,12 @@
1010

1111
from posit_bakery.const import REGEX_IMAGE_TAG_SUFFIX_ALLOWED_CHARACTERS_PATTERN
1212

13-
_OSLESS_NAMES = frozenset({"scratch"})
14-
1513
# Shared field configuration for file extensions.
1614
ExtensionField = Annotated[
1715
str,
1816
Field(
19-
default_factory=lambda data: (
20-
""
21-
if data.get("name", "").lower().strip() in _OSLESS_NAMES
22-
else re.sub(r"[^a-zA-Z0-9_-]", "", data.get("name", "").lower())
23-
),
24-
pattern=r"^[a-zA-Z0-9_-]*$",
17+
default_factory=lambda data: re.sub(r"[^a-zA-Z0-9_-]", "", data.get("name", "").lower()),
18+
pattern=r"^[a-zA-Z0-9_-]+$",
2519
validate_default=True,
2620
),
2721
]
@@ -31,12 +25,10 @@
3125
TagDisplayNameField = Annotated[
3226
str,
3327
Field(
34-
default_factory=lambda data: (
35-
""
36-
if data.get("name", "").lower().strip() in _OSLESS_NAMES
37-
else re.sub(REGEX_IMAGE_TAG_SUFFIX_ALLOWED_CHARACTERS_PATTERN, "-", data.get("name", "").lower())
28+
default_factory=lambda data: re.sub(
29+
REGEX_IMAGE_TAG_SUFFIX_ALLOWED_CHARACTERS_PATTERN, "-", data.get("name", "").lower()
3830
),
39-
pattern=r"^[a-zA-Z0-9_.-]*$",
31+
pattern=r"^[a-zA-Z0-9_.-]+$",
4032
validate_default=True,
4133
),
4234
]

0 commit comments

Comments
 (0)