Skip to content

Commit 0eb399f

Browse files
committed
Enhance handling of flavors.yaml checked out from GitHub
Signed-off-by: Tobias Wolf <[email protected]>
1 parent d726c39 commit 0eb399f

File tree

4 files changed

+38
-42
lines changed

4 files changed

+38
-42
lines changed

src/gardenlinux/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@
140140
"secureboot.aws-efivars": "application/io.gardenlinux.cert.secureboot.aws-efivars",
141141
}
142142

143+
GL_REPOSITORY_URL = "https://github.com/gardenlinux/gardenlinux"
144+
143145
OCI_ANNOTATION_SIGNATURE_KEY = "io.gardenlinux.oci.signature"
144146
OCI_ANNOTATION_SIGNED_STRING_KEY = "io.gardenlinux.oci.signed-string"
145147
OCI_IMAGE_INDEX_MEDIA_TYPE = "application/vnd.oci.image.index.v1+json"

src/gardenlinux/flavors/__main__.py

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
"""
77

88
from argparse import ArgumentParser
9+
from git import Repo
10+
from git.exc import GitError
11+
from pathlib import Path
12+
from tempfile import TemporaryDirectory
913
import json
1014
import os
1115
import sys
1216

17+
from ..constants import GL_REPOSITORY_URL
1318
from ..git import Git
14-
from ..github import GitHub
1519

1620
from .parser import Parser
1721

@@ -52,7 +56,7 @@ def parse_args():
5256
parser.add_argument(
5357
"--commit",
5458
default=None,
55-
help="Commit hash to fetch flavors.yaml from GitHub (if not specified, uses local file).",
59+
help="Commit hash to fetch flavors.yaml from GitHub. An existing 'flavors.yaml' file will be preferred.",
5660
)
5761
parser.add_argument(
5862
"--no-arch",
@@ -126,44 +130,27 @@ def main():
126130

127131
args = parse_args()
128132

129-
if args.commit:
130-
# Use GitHub API to fetch flavors.yaml
131-
github = GitHub()
132-
flavors_content = github.get_flavors_yaml(commit=args.commit)
133-
134-
parser = Parser(data=flavors_content)
135-
136-
combinations = parser.filter(
137-
include_only_patterns=args.include_only,
138-
wildcard_excludes=args.exclude,
139-
only_build=args.build,
140-
only_test=args.test,
141-
only_test_platform=args.test_platform,
142-
only_publish=args.publish,
143-
filter_categories=args.category,
144-
exclude_categories=args.exclude_category,
145-
)
146-
else:
147-
# Use local file
148-
flavors_file = os.path.join(Git().root, "flavors.yaml")
149-
150-
if not os.path.isfile(flavors_file):
151-
sys.exit(f"Error: {flavors_file} does not exist.")
152-
153-
# Load and validate the flavors.yaml
154-
with open(flavors_file, "r") as file:
155-
flavors_data = file.read()
156-
157-
combinations = Parser(flavors_data).filter(
158-
include_only_patterns=args.include_only,
159-
wildcard_excludes=args.exclude,
160-
only_build=args.build,
161-
only_test=args.test,
162-
only_test_platform=args.test_platform,
163-
only_publish=args.publish,
164-
filter_categories=args.category,
165-
exclude_categories=args.exclude_category,
166-
)
133+
try:
134+
flavors_data = _get_flavors_file_data(Path(Git().root, "flavors.yaml"))
135+
except GitError:
136+
with TemporaryDirectory() as git_directory:
137+
repo = Repo.clone_from(GL_REPOSITORY_URL, git_directory, no_origin=True, sparse=True)
138+
139+
ref = repo.heads.main
140+
if args.commit is not None: ref = ref.set_commit(args.commit)
141+
142+
flavors_data = _get_flavors_file_data(Path(repo.working_dir, "flavors.yaml"))
143+
144+
combinations = Parser(flavors_data).filter(
145+
include_only_patterns=args.include_only,
146+
wildcard_excludes=args.exclude,
147+
only_build=args.build,
148+
only_test=args.test,
149+
only_test_platform=args.test_platform,
150+
only_publish=args.publish,
151+
filter_categories=args.category,
152+
exclude_categories=args.exclude_category,
153+
)
167154

168155
if args.json_by_arch:
169156
grouped_combinations = Parser.group_by_arch(combinations)
@@ -187,6 +174,14 @@ def main():
187174
print("\n".join(sorted(set(printable_combinations))))
188175

189176

177+
def _get_flavors_file_data(flavors_file):
178+
if not flavors_file.exists():
179+
raise RuntimeError(f"Error: {flavors_file} does not exist.")
180+
181+
# Load and validate the flavors.yaml
182+
with flavors_file.open("r") as fp:
183+
return fp.read()
184+
190185
if __name__ == "__main__":
191186
# Create a null logger as default
192187
main()

src/gardenlinux/flavors/parser.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
from ..constants import GL_FLAVORS_SCHEMA
1212
from ..logger import LoggerSetup
13-
from ..github import GitHub
1413

1514

1615
class Parser(object):

src/gardenlinux/git/git.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# -*- coding: utf-8 -*-
22

3-
import sys
43
from git import Repo
54
from git import Git as _Git
65
from os import PathLike
76
from pathlib import Path
7+
import sys
88

99
from ..logger import LoggerSetup
1010

0 commit comments

Comments
 (0)