66"""
77
88from argparse import ArgumentParser
9+ from git import Repo
10+ from git .exc import GitError
11+ from pathlib import Path
12+ from tempfile import TemporaryDirectory
913import json
1014import os
1115import sys
1216
17+ from ..constants import GL_REPOSITORY_URL
1318from ..git import Git
14- from ..github import GitHub
1519
1620from .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+
190185if __name__ == "__main__" :
191186 # Create a null logger as default
192187 main ()
0 commit comments