Skip to content

Commit df87d4b

Browse files
committed
Add support to generate container names based on a given feature set
Signed-off-by: Tobias Wolf <wolf@b1-systems.de> On-behalf-of: SAP <tobias.wolf@sap.com>
1 parent df923f9 commit df87d4b

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

src/gardenlinux/features/__main__.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import argparse
99
import logging
1010
import os
11+
import re
1112
from functools import reduce
1213
from os import path
1314
from typing import Any, List, Set
@@ -18,17 +19,23 @@
1819
_ARGS_TYPE_ALLOWED = [
1920
"cname",
2021
"cname_base",
22+
"container_name",
2123
"commit_id",
2224
"features",
2325
"platforms",
2426
"flags",
27+
"flavor",
2528
"elements",
2629
"arch",
2730
"version",
2831
"version_and_commit_id",
2932
"graph",
3033
]
3134

35+
RE_CAMEL_CASE_SPLITTER = re.compile("([A-Z]+|[a-z0-9])([A-Z])(?!$)")
36+
"""
37+
CamelCase splitter RegExp
38+
"""
3239

3340
def main() -> None:
3441
"""
@@ -102,7 +109,7 @@ def main() -> None:
102109
commit_id_or_hash = cname.commit_id
103110
version = cname.version
104111

105-
if arch is None or arch == "" and (args.type in ("cname", "arch")):
112+
if arch is None or arch == "" and (args.type in ("cname", "container_name", "arch")):
106113
raise RuntimeError(
107114
"Architecture could not be determined and no default architecture set"
108115
)
@@ -121,9 +128,11 @@ def main() -> None:
121128
elif args.type in (
122129
"cname_base",
123130
"cname",
131+
"container_name",
124132
"elements",
125133
"features",
126134
"flags",
135+
"flavor",
127136
"graph",
128137
"platforms",
129138
):
@@ -143,7 +152,7 @@ def main() -> None:
143152
print(f"{version}-{commit_id_or_hash[:8]}")
144153

145154

146-
def get_cname_base(sorted_features: List[str]):
155+
def get_flavor(sorted_features: List[str]):
147156
"""
148157
Get the base cname for the feature set given.
149158
@@ -265,7 +274,7 @@ def print_output_from_features_parser(
265274

266275
sorted_minimal_features = sort_subset(minimal_feature_set, sorted_features)
267276

268-
cname_base = get_cname_base(sorted_minimal_features)
277+
cname_base = get_flavor(sorted_minimal_features)
269278

270279
if output_type == "cname_base":
271280
print(cname_base)
@@ -279,7 +288,9 @@ def print_output_from_features_parser(
279288
cname += f"-{version}-{commit_id_or_hash[:8]}"
280289

281290
print(cname)
282-
if output_type == "platforms":
291+
elif output_type == "container_name":
292+
print(RE_CAMEL_CASE_SPLITTER.sub("\\1_\\2", cname_base).lower())
293+
elif output_type == "platforms":
283294
print(",".join(features_by_type["platform"]))
284295
elif output_type == "elements":
285296
print(",".join(features_by_type["element"]))
@@ -303,6 +314,8 @@ def print_output_from_cname(output_type: str, cname_instance: CName) -> None:
303314
print(cname_instance.flavor)
304315
elif output_type == "cname":
305316
print(cname_instance.cname)
317+
elif output_type == "container_name":
318+
print(RE_CAMEL_CASE_SPLITTER.sub("\\1-\\2", cname_instance.flavor).lower())
306319
elif output_type == "platforms":
307320
print(cname_instance.feature_set_platform)
308321
elif output_type == "elements":

src/gardenlinux/features/cname_main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from os.path import basename, dirname
1212

1313
from .__main__ import (
14-
get_cname_base,
14+
get_flavor,
1515
get_minimal_feature_set,
1616
get_version_and_commit_id_from_files,
1717
sort_subset,
@@ -80,7 +80,7 @@ def main():
8080

8181
sorted_minimal_features = sort_subset(minimal_feature_set, sorted_features)
8282

83-
generated_cname = get_cname_base(sorted_minimal_features)
83+
generated_cname = get_flavor(sorted_minimal_features)
8484

8585
generated_cname += f"-{cname.arch}"
8686

tests/features/test_main.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@
1111
# -------------------------------
1212

1313

14-
def test_get_cname_base():
14+
def test_get_flavor():
1515
# Arrange
1616
sorted_features = ["base", "_hidden", "extra"]
1717

1818
# Act
19-
result = fema.get_cname_base(sorted_features)
19+
result = fema.get_flavor(sorted_features)
2020

2121
# Assert
2222
assert result == "base_hidden-extra"
2323

2424

25-
def test_get_cname_base_empty_raises():
26-
# get_cname_base with empty iterable raises TypeError
25+
def test_get_flavor_empty_raises():
26+
# get_flavor with empty iterable raises TypeError
2727
with pytest.raises(TypeError):
28-
fema.get_cname_base([])
28+
fema.get_flavor([])
2929

3030

3131
def test_sort_return_intersection_subset():
@@ -146,6 +146,20 @@ def test_main_prints_arch(monkeypatch, capsys):
146146
assert "amd64" in out
147147

148148

149+
def test_main_prints_container_name(monkeypatch, capsys):
150+
# Arrange
151+
argv = ["prog", "--arch", "amd64", "--cname", "container-pythonDev", "--version", "1.0", "container_name"]
152+
monkeypatch.setattr(sys, "argv", argv)
153+
monkeypatch.setattr(fema, "Parser", lambda *a, **kw: None)
154+
155+
# Act
156+
fema.main()
157+
158+
# Assert
159+
out = capsys.readouterr().out
160+
assert "container-python-dev" in out
161+
162+
149163
def test_main_prints_commit_id(monkeypatch, capsys):
150164
# Arrange
151165
argv = ["prog", "--arch", "amd64", "--cname", "flav", "commit_id"]

0 commit comments

Comments
 (0)