Skip to content

Commit 51712b3

Browse files
committed
Add support to read existing flavors.yaml with gl-flavors-parse
Signed-off-by: Tobias Wolf <[email protected]>
1 parent b48ffe0 commit 51712b3

File tree

1 file changed

+112
-101
lines changed

1 file changed

+112
-101
lines changed

src/python_gardenlinux_lib/flavors/parse_flavors.py

Lines changed: 112 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,26 @@ def generate_markdown_table(combinations, no_arch):
213213
return table
214214

215215

216+
def _get_flavors_from_github(commit):
217+
"""Returns the flavors.yaml from GitHub if readable."""
218+
219+
# Try flavors.yaml first
220+
api_path = "/repos/gardenlinux/gardenlinux/contents/flavors.yaml"
221+
if commit != "latest":
222+
api_path = f"{api_path}?ref={commit}"
223+
command = ["gh", "api", api_path]
224+
logger.debug(f"Fetching flavors.yaml from GitHub for commit {commit_short}")
225+
result = subprocess.run(
226+
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
227+
)
228+
229+
if result.returncode == 0:
230+
content_data = json.loads(result.stdout)
231+
return base64.b64decode(content_data["content"]).decode("utf-8")
232+
else:
233+
raise RuntimeError("Failed receiving result from GitHub: {0}".format(result.stderr))
234+
235+
216236
def parse_flavors_commit(
217237
commit=None,
218238
version=None,
@@ -249,113 +269,104 @@ def parse_flavors_commit(
249269
Returns:
250270
list: List of flavor strings, or empty list if no flavors found
251271
"""
252-
try:
253-
version_info = (
254-
f"{version['major']}.{version.get('minor', 0)}" if version else "unknown"
255-
)
256-
if commit is None:
257-
commit = "latest"
258-
commit_short = commit[:8]
259272

260-
logger.debug(
261-
f"Checking flavors for version {version_info} (commit {commit_short})"
262-
)
273+
version_info = (
274+
f"{version['major']}.{version.get('minor', 0)}" if version else "unknown"
275+
)
276+
if commit is None:
277+
commit = "latest"
278+
commit_short = commit[:8]
263279

264-
# Try flavors.yaml first
265-
api_path = "/repos/gardenlinux/gardenlinux/contents/flavors.yaml"
266-
if commit != "latest":
267-
api_path = f"{api_path}?ref={commit}"
268-
command = ["gh", "api", api_path]
269-
logger.debug(f"Fetching flavors.yaml from GitHub for commit {commit_short}")
270-
result = subprocess.run(
271-
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
272-
)
280+
logger.debug(
281+
f"Checking flavors for version {version_info} (commit {commit_short})"
282+
)
273283

274-
if result.returncode == 0:
275-
content_data = json.loads(result.stdout)
276-
yaml_content = base64.b64decode(content_data["content"]).decode("utf-8")
277-
flavors_data = yaml.safe_load(yaml_content)
278-
279-
# Parse flavors with all filters
280-
combinations = parse_flavors_data(
281-
flavors_data,
282-
include_only_patterns=include_only_patterns or [],
283-
wildcard_excludes=wildcard_excludes or [],
284-
only_build=only_build,
285-
only_test=only_test,
286-
only_test_platform=only_test_platform,
287-
only_publish=only_publish,
288-
filter_categories=filter_categories or [],
289-
exclude_categories=exclude_categories or [],
290-
)
284+
flavors_content = None
291285

292-
all_flavors = set()
293-
for _, combination in combinations:
294-
all_flavors.add(combination)
286+
if os.access("./flavors.yaml", os.F_OK | os.R_OK):
287+
with open("./flavors.yaml", "r") as fp:
288+
flavors_content = fp.read()
289+
else:
290+
try:
291+
flavors_content = _get_flavors_from_github()
292+
except Exception as exc:
293+
logger.debug(exc)
294+
295+
if flavors_content is not None:
296+
# Parse flavors with all filters
297+
combinations = parse_flavors_data(
298+
yaml.safe_load(flavors_content),
299+
include_only_patterns=include_only_patterns or [],
300+
wildcard_excludes=wildcard_excludes or [],
301+
only_build=only_build,
302+
only_test=only_test,
303+
only_test_platform=only_test_platform,
304+
only_publish=only_publish,
305+
filter_categories=filter_categories or [],
306+
exclude_categories=exclude_categories or [],
307+
)
295308

296-
if all_flavors:
297-
logger.info(f"Found {len(all_flavors)} flavors in flavors.yaml")
298-
return sorted(all_flavors)
299-
else:
300-
logger.info("No flavors found in flavors.yaml")
301-
302-
# If no flavors.yaml found and query_s3 is enabled, try S3 artifacts
303-
if query_s3 and s3_objects and isinstance(s3_objects, dict):
304-
logger.debug("Checking S3 artifacts")
305-
index = s3_objects.get("index", {})
306-
artifacts = s3_objects.get("artifacts", [])
307-
308-
# Try index lookup first
309-
search_key = f"{version_info}-{commit_short}"
310-
if search_key in index:
311-
flavors = index[search_key]
312-
logger.debug(f"Found flavors in S3 index for {search_key}")
313-
else:
314-
# If no index match, search through artifacts
315-
found_flavors = set()
316-
317-
# Search for artifacts matching version and commit
318-
for key in artifacts:
319-
if version_info in key and commit_short in key:
320-
try:
321-
parts = key.split("/")
322-
if len(parts) >= 2:
323-
flavor_with_version = parts[1]
324-
flavor = flavor_with_version.rsplit(
325-
"-" + version_info, 1
326-
)[0]
327-
if flavor:
328-
found_flavors.add(flavor)
329-
except Exception as e:
330-
logger.debug(f"Error parsing artifact key {key}: {e}")
331-
continue
332-
333-
flavors = list(found_flavors)
334-
335-
# Apply filters to S3 flavors
336-
filtered_flavors = []
337-
for flavor in flavors:
338-
# Create a dummy combination with amd64 architecture for filtering
339-
combination = ("amd64", flavor)
340-
if should_include_only(
341-
flavor, include_only_patterns or []
342-
) and not should_exclude(flavor, wildcard_excludes or [], []):
343-
filtered_flavors.append(flavor)
344-
345-
if filtered_flavors:
346-
logger.info(
347-
f"Found {len(filtered_flavors)} flavors in S3 artifacts after filtering"
348-
)
349-
return sorted(filtered_flavors)
350-
else:
351-
logger.info(
352-
f"No flavors found in S3 for version {version_info} and commit {commit_short} after filtering"
353-
)
309+
all_flavors = set()
310+
for _, combination in combinations:
311+
all_flavors.add(combination)
354312

355-
return []
313+
if all_flavors:
314+
logger.info(f"Found {len(all_flavors)} flavors in flavors.yaml")
315+
return sorted(all_flavors)
316+
else:
317+
logger.info("No flavors found in flavors.yaml")
318+
elif query_s3 and s3_objects and isinstance(s3_objects, dict):
319+
logger.debug("Checking S3 artifacts")
320+
index = s3_objects.get("index", {})
321+
artifacts = s3_objects.get("artifacts", [])
322+
323+
# Try index lookup first
324+
search_key = f"{version_info}-{commit_short}"
325+
if search_key in index:
326+
flavors = index[search_key]
327+
logger.debug(f"Found flavors in S3 index for {search_key}")
328+
else:
329+
# If no index match, search through artifacts
330+
found_flavors = set()
331+
332+
# Search for artifacts matching version and commit
333+
for key in artifacts:
334+
if version_info in key and commit_short in key:
335+
try:
336+
parts = key.split("/")
337+
if len(parts) >= 2:
338+
flavor_with_version = parts[1]
339+
flavor = flavor_with_version.rsplit(
340+
"-" + version_info, 1
341+
)[0]
342+
if flavor:
343+
found_flavors.add(flavor)
344+
except Exception as e:
345+
logger.debug(f"Error parsing artifact key {key}: {e}")
346+
continue
347+
348+
flavors = list(found_flavors)
349+
350+
# Apply filters to S3 flavors
351+
filtered_flavors = []
352+
for flavor in flavors:
353+
# Create a dummy combination with amd64 architecture for filtering
354+
combination = ("amd64", flavor)
355+
if should_include_only(
356+
flavor, include_only_patterns or []
357+
) and not should_exclude(flavor, wildcard_excludes or [], []):
358+
filtered_flavors.append(flavor)
359+
360+
if filtered_flavors:
361+
logger.info(
362+
f"Found {len(filtered_flavors)} flavors in S3 artifacts after filtering"
363+
)
364+
return sorted(filtered_flavors)
365+
else:
366+
logger.info(
367+
f"No flavors found in S3 for version {version_info} and commit {commit_short} after filtering"
368+
)
356369

357-
except Exception as e:
358-
logger.error(f"Error parsing flavors for commit {commit_short}: {e}")
359370
return []
360371

361372

@@ -446,7 +457,7 @@ def main():
446457
)
447458

448459
if not flavors:
449-
sys.exit(1)
460+
sys.exit("No flavors parsed")
450461

451462
# Output the results in the requested format
452463
if args.json_by_arch:

0 commit comments

Comments
 (0)