Skip to content

Commit fe31da4

Browse files
committed
Enforce one platform policy for GardenLinux canonical names
Signed-off-by: Tobias Wolf <[email protected]> On-behalf-of: SAP <[email protected]>
1 parent 7380f34 commit fe31da4

File tree

5 files changed

+79
-91
lines changed

5 files changed

+79
-91
lines changed

src/gardenlinux/features/__main__.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"container_tag",
2424
"commit_id",
2525
"features",
26-
"platforms",
26+
"platform",
2727
"flags",
2828
"flavor",
2929
"elements",
@@ -147,7 +147,7 @@ def main() -> None:
147147
"flags",
148148
"flavor",
149149
"graph",
150-
"platforms",
150+
"platform",
151151
):
152152
if args.type == "graph" or len(args.ignore) > 0:
153153
features_parser = Parser(gardenlinux_root, feature_dir_name)
@@ -259,7 +259,7 @@ def print_output_from_features_parser(
259259
:param flavor: Flavor
260260
:param ignores_list: Features to ignore
261261
262-
:since: 0.11.0
262+
:since: 1.0.0
263263
"""
264264

265265
additional_filter_func = lambda node: node not in ignores_list
@@ -270,13 +270,13 @@ def print_output_from_features_parser(
270270
flavor, additional_filter_func=additional_filter_func
271271
)
272272
)
273-
elif (output_type in "platforms", "elements", "flags"):
273+
elif (output_type in "platform", "elements", "flags"):
274274
features_by_type = parser.filter_as_dict(
275275
flavor, additional_filter_func=additional_filter_func
276276
)
277277

278-
if output_type == "platforms":
279-
print(",".join(features_by_type["platform"]))
278+
if output_type == "platform":
279+
print(features_by_type["platform"][0])
280280
elif output_type == "elements":
281281
print(",".join(features_by_type["element"]))
282282
elif output_type == "flags":
@@ -305,8 +305,8 @@ def print_output_from_features_parser(
305305
print(cname)
306306
elif output_type == "container_name":
307307
print(RE_CAMEL_CASE_SPLITTER.sub("\\1_\\2", cname_base).lower())
308-
elif output_type == "platforms":
309-
print(",".join(features_by_type["platform"]))
308+
elif output_type == "platform":
309+
print(features_by_type["platform"][0])
310310
elif output_type == "elements":
311311
print(",".join(features_by_type["element"]))
312312
elif output_type == "flags":
@@ -322,7 +322,7 @@ def print_output_from_cname(output_type: str, cname_instance: CName) -> None:
322322
:param output_type: Output type
323323
:param cname_instance: CName instance
324324
325-
:since: 0.11.0
325+
:since: 1.0.0
326326
"""
327327

328328
if output_type == "cname_base":
@@ -331,7 +331,7 @@ def print_output_from_cname(output_type: str, cname_instance: CName) -> None:
331331
print(cname_instance.cname)
332332
elif output_type == "container_name":
333333
print(RE_CAMEL_CASE_SPLITTER.sub("\\1-\\2", cname_instance.flavor).lower())
334-
elif output_type == "platforms":
334+
elif output_type == "platform":
335335
print(cname_instance.feature_set_platform)
336336
elif output_type == "elements":
337337
print(cname_instance.feature_set_element)

src/gardenlinux/features/cname.py

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import re
88
from configparser import UNNAMED_SECTION, ConfigParser
9-
from os import PathLike
9+
from os import environ, PathLike
1010
from pathlib import Path
1111
from typing import List, Optional
1212

@@ -51,8 +51,9 @@ def __init__(self, cname, arch=None, commit_hash=None, version=None):
5151
self._commit_id = None
5252
self._feature_elements_cached = None
5353
self._feature_flags_cached = None
54-
self._feature_platforms_cached = None
54+
self._feature_platform_cached = None
5555
self._feature_set_cached = None
56+
self._flag_multiple_platforms = bool(environ.get("GL_ALLOW_FRANKENSTEIN", False))
5657
self._flavor = None
5758
self._version = None
5859

@@ -117,7 +118,7 @@ def cname(self) -> str:
117118
:return: (str) CName
118119
:since: 0.7.0
119120
"""
120-
assert self._flavor is not None, "CName flavor is not set!"
121+
121122
cname = self._flavor
122123

123124
if self._arch is not None:
@@ -134,7 +135,7 @@ def commit_hash(self) -> str:
134135
Returns the commit hash if part of the cname parsed.
135136
136137
:return: (str) Commit hash
137-
:since: 0.11.0
138+
:since: 1.0.0
138139
"""
139140

140141
if self._commit_hash is None:
@@ -151,7 +152,7 @@ def commit_hash(self, commit_hash) -> None:
151152
152153
:param commit_hash: Commit hash
153154
154-
:since: 0.11.0
155+
:since: 1.0.0
155156
"""
156157

157158
if self._commit_id is not None and not commit_hash.startswith(self._commit_id):
@@ -202,7 +203,7 @@ def feature_set_element(self) -> str:
202203
Returns the feature set of type "element" for the cname parsed.
203204
204205
:return: (str) Feature set elements
205-
:since: 0.11.0
206+
:since: 1.0.0
206207
"""
207208

208209
if self._feature_elements_cached is not None:
@@ -216,7 +217,7 @@ def feature_set_flag(self) -> str:
216217
Returns the feature set of type "flag" for the cname parsed.
217218
218219
:return: (str) Feature set flags
219-
:since: 0.11.0
220+
:since: 1.0.0
220221
"""
221222

222223
if self._feature_flags_cached is not None:
@@ -229,29 +230,38 @@ def feature_set_platform(self) -> str:
229230
"""
230231
Returns the feature set of type "platform" for the cname parsed.
231232
232-
:return: (str) Feature set platforms
233-
:since: 0.11.0
233+
:return: (str) Feature set platform
234+
:since: 1.0.0
234235
"""
235236

236-
if self._feature_platforms_cached is not None:
237-
return ",".join(self._feature_platforms_cached)
237+
if self._feature_platform_cached is not None:
238+
return self._feature_platform_cached
239+
240+
platforms = Parser().filter_as_dict(self.flavor)["platform"]
241+
242+
if self._flag_multiple_platforms:
243+
return ",".join(platforms)
238244

239-
return ",".join(Parser().filter_as_dict(self.flavor)["platform"])
245+
assert len(platforms) < 2; "Only one platform is supported"
246+
return platforms[0]
240247

241248
@property
242249
def release_metadata_string(self) -> str:
243250
"""
244251
Returns the release metadata describing the given CName instance.
245252
246253
:return: (str) Release metadata describing the given CName instance
247-
:since: 0.11.0
254+
:since: 1.0.0
248255
"""
249256

250257
features = Parser().filter_as_dict(self.flavor)
251258

259+
if not self._flag_multiple_platforms:
260+
assert len(features["platform"]) < 2; "Only one platform is supported"
261+
252262
elements = ",".join(features["element"])
253263
flags = ",".join(features["flag"])
254-
platforms = ",".join(features["platform"])
264+
platform = ",".join(features["platform"])
255265

256266
metadata = f"""
257267
ID={GL_RELEASE_ID}
@@ -264,7 +274,7 @@ def release_metadata_string(self) -> str:
264274
BUG_REPORT_URL="{GL_BUG_REPORT_URL}"
265275
GARDENLINUX_CNAME="{self.cname}"
266276
GARDENLINUX_FEATURES="{self.feature_set}"
267-
GARDENLINUX_FEATURES_PLATFORMS="{platforms}"
277+
GARDENLINUX_FEATURES_PLATFORM="{platform}"
268278
GARDENLINUX_FEATURES_ELEMENTS="{elements}"
269279
GARDENLINUX_FEATURES_FLAGS="{flags}"
270280
GARDENLINUX_VERSION="{self.version}"
@@ -282,24 +292,9 @@ def platform(self) -> str:
282292
:return: (str) Feature set platforms
283293
:since: 0.7.0
284294
"""
285-
assert self._flavor is not None, "Flavor not set!"
286295

287296
return self.feature_set_platform
288297

289-
@property
290-
def platforms(self) -> List[str]:
291-
"""
292-
Returns the platforms for the cname parsed.
293-
294-
:return: (str) Platforms
295-
:since: 0.11.0
296-
"""
297-
298-
if self._feature_platforms_cached is not None:
299-
return self._feature_platforms_cached
300-
301-
return Parser().filter_as_dict(self.flavor)["platform"]
302-
303298
@property
304299
def version(self) -> Optional[str]:
305300
"""
@@ -331,7 +326,7 @@ def load_from_release_file(self, release_file: PathLike | str) -> None:
331326
332327
:param release_file: Release metadata file
333328
334-
:since: 0.11.0
329+
:since: 1.0.0
335330
"""
336331

337332
if not isinstance(release_file, PathLike):
@@ -350,7 +345,7 @@ def load_from_release_file(self, release_file: PathLike | str) -> None:
350345
"GARDENLINUX_FEATURES",
351346
"GARDENLINUX_FEATURES_ELEMENTS",
352347
"GARDENLINUX_FEATURES_FLAGS",
353-
"GARDENLINUX_FEATURES_PLATFORMS",
348+
"GARDENLINUX_FEATURES_PLATFORM",
354349
"GARDENLINUX_VERSION",
355350
):
356351
if not release_config.has_option(UNNAMED_SECTION, release_field):
@@ -395,9 +390,9 @@ def load_from_release_file(self, release_file: PathLike | str) -> None:
395390
UNNAMED_SECTION, "GARDENLINUX_FEATURES_FLAGS"
396391
).split(",")
397392

398-
self._feature_platforms_cached = release_config.get(
399-
UNNAMED_SECTION, "GARDENLINUX_FEATURES_PLATFORMS"
400-
).split(",")
393+
self._feature_platform_cached = release_config.get(
394+
UNNAMED_SECTION, "GARDENLINUX_FEATURES_PLATFORM"
395+
)
401396

402397
def save_to_release_file(
403398
self, release_file: PathLike | str, overwrite: Optional[bool] = False
@@ -407,7 +402,7 @@ def save_to_release_file(
407402
408403
:param release_file: Release metadata file
409404
410-
:since: 0.11.0
405+
:since: 1.0.0
411406
"""
412407

413408
if not isinstance(release_file, PathLike):

src/gardenlinux/features/parser.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,8 @@ def get_cname_as_feature_set(cname):
368368
369369
:param cname: Canonical name
370370
371-
:return: (set) Features of the cname
372-
:since: 0.7.0
371+
:return: (list) Features of the cname
372+
:since: 1.0.0
373373
"""
374374

375375
cname = cname.replace("_", "-_")
@@ -473,6 +473,20 @@ def key_function(node):
473473

474474
return list(networkx.lexicographical_topological_sort(graph, key=key_function))
475475

476+
@staticmethod
477+
def subset(input_set: Set[str], order_list: List[str]) -> List[str]:
478+
"""
479+
Returns items from `order_list` if given in `input_set`.
480+
481+
:param input_set: Set of values for filtering
482+
:param order_list: Set of values to be filtered
483+
484+
:return: (list) Subset
485+
:since: 1.0.0
486+
"""
487+
488+
return [item for item in order_list if item in input_set]
489+
476490
@staticmethod
477491
def sort_reversed_graph_nodes(graph):
478492
"""

src/gardenlinux/s3/__main__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ def main() -> None:
3636
if args.action == "download-artifacts-from-bucket":
3737
S3Artifacts(args.bucket).download_to_directory(args.cname, args.path)
3838
elif args.action == "upload-artifacts-to-bucket":
39-
S3Artifacts(args.bucket).upload_from_directory(args.cname, args.path, dry_run=args.dry_run)
39+
S3Artifacts(args.bucket).upload_from_directory(
40+
args.cname, args.path, dry_run=args.dry_run
41+
)

0 commit comments

Comments
 (0)