44import sys
55
66import snapshot_manager .config as config
7+ import snapshot_manager .copr_util as copr_util
78import snapshot_manager .snapshot_manager as snapshot_manager
9+ import snapshot_manager .util as util
810
11+ # This shows the default value of arguments in the help text.
12+ # See https://docs.python.org/3/library/argparse.html#argparse.ArgumentDefaultsHelpFormatter
13+ ARG_PARSE_SHOW_DEFAULT_VALUE = {
14+ "formatter_class" : argparse .ArgumentDefaultsHelpFormatter
15+ }
916
10- def main ():
11- cfg = config .Config ()
1217
18+ def main ():
1319 logging .basicConfig (
1420 level = logging .INFO ,
1521 format = "[%(asctime)s] %(levelname)s [%(filename)s:%(lineno)d %(funcName)s] %(message)s" ,
1622 datefmt = "%d/%b/%Y %H:%M:%S" ,
1723 stream = sys .stderr ,
1824 )
1925
20- # This shows the default value of arguments in the help text.
21- # See https://docs.python.org/3/library/argparse.html#argparse.ArgumentDefaultsHelpFormatter
22- parser_args = {"formatter_class" : argparse .ArgumentDefaultsHelpFormatter }
26+ cfg = config .Config ()
27+ args = build_argument_parser (cfg = cfg ).parse_args ()
28+ cmd = args .command
29+
30+ copr_client = None
31+ all_chroots = []
32+ config_map = config .build_config_map ()
33+
34+ # For some commands we need a copr client and a config map set up.
35+ if cmd in (
36+ "check" ,
37+ "get-chroots" ,
38+ "has-all-good-builds" ,
39+ "delete-project" ,
40+ "github-matrix" ,
41+ ):
42+ copr_client = copr_util .make_client ()
43+ all_chroots = copr_util .get_all_chroots (client = copr_client )
44+ util .augment_config_map_with_chroots (
45+ config_map = config_map , all_chroots = all_chroots
46+ )
47+
48+ if cmd in ("check" , "get-chroots" , "has-all-good-builds" , "delete-project" ):
49+ if args .strategy not in config_map :
50+ logging .error (
51+ f"No strategy with name '{ args .strategy } ' found in list of strategies: { config_map .keys ()} "
52+ )
53+ sys .exit (1 )
54+ cfg = config_map [args .strategy ]
55+
56+ if cmd == "check" :
57+ cfg .github_repo = args .github_repo
58+ cfg .datetime = args .datetime
59+ cfg .strategy = args .strategy
60+ snapshot_manager .SnapshotManager (config = cfg ).check_todays_builds ()
61+ elif cmd == "retest" :
62+ cfg .github_repo = args .github_repo
63+ snapshot_manager .SnapshotManager (config = cfg ).retest (
64+ issue_number = args .issue_number ,
65+ trigger_comment_id = args .trigger_comment_id ,
66+ chroots = args .chroots ,
67+ )
68+ elif cmd == "github-matrix" :
69+ json = util .serialize_config_map_to_github_matrix (
70+ config_map = config_map ,
71+ strategy = args .strategy ,
72+ lookback_days = args .lookback_days ,
73+ )
74+ print (json )
75+ elif cmd == "get-chroots" :
76+ print (" " .join (cfg .chroots ))
77+ elif cmd == "delete-project" :
78+ cfg .datetime = args .datetime
79+ copr_util .delete_project (
80+ client = copr_client ,
81+ ownername = cfg .copr_ownername ,
82+ projectname = cfg .copr_projectname ,
83+ )
84+ elif cmd == "has-all-good-builds" :
85+ cfg .datetime = args .datetime
86+ states = copr_util .get_all_build_states (
87+ client = copr_client ,
88+ ownername = cfg .copr_ownername ,
89+ projectname = cfg .copr_projectname ,
90+ )
91+ cfg .packages = args .packages
92+ builds_succeeded = copr_util .has_all_good_builds (
93+ required_chroots = cfg .chroots ,
94+ required_packages = cfg .packages ,
95+ states = states ,
96+ )
97+ if not builds_succeeded :
98+ logging .warning ("Not all builds were successful" )
99+ sys .exit (1 )
100+ logging .info ("All required builds were successful" )
101+ else :
102+ logging .error (f"Unsupported command: { cmd } " )
103+ sys .exit (1 )
23104
105+
106+ def build_argument_parser (cfg : config .Config ) -> argparse .ArgumentParser :
24107 mainparser = argparse .ArgumentParser (
25108 description = "Program for managing LLVM snapshots" ,
26- ** parser_args ,
27- )
28-
29- mainparser .add_argument (
30- "--github-token-env" ,
31- metavar = "ENV_NAME" ,
32- type = str ,
33- dest = "github_token_env" ,
34- default = cfg .github_token_env ,
35- help = "Default name of the environment variable which holds the github token" ,
109+ ** ARG_PARSE_SHOW_DEFAULT_VALUE ,
36110 )
37111
38112 mainparser .add_argument (
@@ -44,26 +118,64 @@ def main():
44118 help = "Repo where to open or update issues." ,
45119 )
46120
47- # For config file support see:
48- # https://newini.wordpress.com/2021/06/11/how-to-import-config-file-to-argparse-using-configparser/
49- # subparser_check.add_argument(
50- # "--config-file",
51- # type=str,
52- # nargs="+",
53- # dest="config_file",
54- # help="Path to config file?",
55- # required=False
56- # )
57-
58121 subparsers = mainparser .add_subparsers (help = "Command to run" , dest = "command" )
59122
60- subparser_retest = subparsers .add_parser (
123+ argument_parser_retest (cfg , subparsers = subparsers )
124+ argument_parser_get_chroots (cfg , subparsers = subparsers )
125+ argument_parser_delete_project (cfg , subparsers = subparsers )
126+ argument_parser_github_matrix (cfg , subparsers = subparsers )
127+ argument_parser_check (cfg = cfg , subparsers = subparsers )
128+ argument_parser_has_all_good_builds (cfg = cfg , subparsers = subparsers )
129+
130+ return mainparser
131+
132+
133+ def add_strategy_argument (argparser : argparse .ArgumentParser ) -> argparse .Action :
134+ argparser .add_argument (
135+ "--strategy" ,
136+ dest = "strategy" ,
137+ type = str ,
138+ default = "" ,
139+ help = f"Strategy to use" ,
140+ )
141+
142+
143+ def add_yyyymmdd_argument (argparser : argparse .ArgumentParser ) -> argparse .Action :
144+ return argparser .add_argument (
145+ "--yyyymmdd" ,
146+ type = lambda s : datetime .datetime .strptime (s , "%Y%m%d" ),
147+ dest = "datetime" ,
148+ default = datetime .datetime .now ().strftime ("%Y%m%d" ),
149+ help = "Default day for which to run command" ,
150+ )
151+
152+
153+ def argument_parser_has_all_good_builds (cfg : config .Config , subparsers ) -> None :
154+ sp = subparsers .add_parser (
155+ "has-all-good-builds" ,
156+ description = "Checks if the given " ,
157+ ** ARG_PARSE_SHOW_DEFAULT_VALUE ,
158+ )
159+ sp .add_argument (
160+ "--packages" ,
161+ metavar = "PKG" ,
162+ type = str ,
163+ nargs = "+" ,
164+ dest = "packages" ,
165+ default = ["llvm" ],
166+ help = "Which packages check (e.g. llvm)" ,
167+ )
168+ add_strategy_argument (sp )
169+ add_yyyymmdd_argument (sp )
170+
171+
172+ def argument_parser_retest (cfg : config .Config , subparsers ) -> None :
173+ sp = subparsers .add_parser (
61174 "retest" ,
62175 description = "Issues a new testing-farm request for one or more chroots" ,
63- ** parser_args ,
176+ ** ARG_PARSE_SHOW_DEFAULT_VALUE ,
64177 )
65-
66- subparser_retest .add_argument (
178+ sp .add_argument (
67179 "--chroots" ,
68180 metavar = "CHROOT" ,
69181 type = str ,
@@ -72,135 +184,67 @@ def main():
72184 required = True ,
73185 help = "Which chroots to retest (e.g. fedora-rawhide-x86_64)" ,
74186 )
75-
76- subparser_retest .add_argument (
187+ sp .add_argument (
77188 "--trigger-comment-id" ,
78189 type = int ,
79190 dest = "trigger_comment_id" ,
80191 required = True ,
81192 help = "ID of the comment that contains the /retest <CHROOT> string" ,
82193 )
83-
84- subparser_retest .add_argument (
194+ sp .add_argument (
85195 "--issue-number" ,
86196 type = int ,
87197 dest = "issue_number" ,
88198 required = True ,
89199 help = "In what issue number did the comment appear in." ,
90200 )
91201
92- subparser_check = subparsers .add_parser (
93- "check" ,
94- description = "Check Copr status and update today's github issue" ,
95- ** parser_args ,
96- )
97202
98- subparser_check .add_argument (
99- "--packages" ,
100- metavar = "PKG" ,
101- type = str ,
102- nargs = "+" ,
103- dest = "packages" ,
104- default = cfg .packages ,
105- help = "Which packages are required to build?" ,
203+ def argument_parser_get_chroots (cfg : config .Config , subparsers ) -> None :
204+ sp = subparsers .add_parser (
205+ "get-chroots" ,
206+ description = "Prints a space separated list of chroots for a given strategy" ,
207+ ** ARG_PARSE_SHOW_DEFAULT_VALUE ,
106208 )
209+ add_strategy_argument (sp )
107210
108- subparser_check .add_argument (
109- "--chroot-pattern" ,
110- metavar = "REGULAR_EXPRESSION" ,
111- type = str ,
112- dest = "chroot_pattern" ,
113- default = cfg .chroot_pattern ,
114- help = "Chroots regex pattern for required chroots." ,
115- )
116211
117- subparser_check .add_argument (
118- "--build-strategy" ,
119- type = str ,
120- dest = "build_strategy" ,
121- default = cfg .build_strategy ,
122- help = "Build strategy to look for (e.g. 'standalone', 'big-merge', 'bootstrap')." ,
212+ def argument_parser_delete_project (cfg : config .Config , subparsers ) -> None :
213+ sp = subparsers .add_parser (
214+ "delete-project" ,
215+ description = "Deletes a project for the given day and strategy" ,
216+ ** ARG_PARSE_SHOW_DEFAULT_VALUE ,
123217 )
218+ add_strategy_argument (sp )
219+ add_yyyymmdd_argument (sp )
124220
125- subparser_check .add_argument (
126- "--maintainer-handle" ,
127- metavar = "GITHUB_HANDLE_WITHOUT_AT_SIGN" ,
128- type = str ,
129- dest = "maintainer_handle" ,
130- default = cfg .maintainer_handle ,
131- help = "Maintainer handle to use for assigning issues." ,
132- )
133221
134- subparser_check .add_argument (
135- "--copr-ownername" ,
136- metavar = "COPR-OWNWERNAME" ,
137- type = str ,
138- dest = "copr_ownername" ,
139- default = cfg .copr_ownername ,
140- help = "Copr ownername to check." ,
222+ def argument_parser_github_matrix (cfg : config .Config , subparsers ) -> None :
223+ sp = subparsers .add_parser (
224+ "github-matrix" ,
225+ description = "Prints the github workflow matrix for a given or all strategies" ,
226+ ** ARG_PARSE_SHOW_DEFAULT_VALUE ,
141227 )
142-
143- subparser_check .add_argument (
144- "--copr-project-tpl" ,
145- metavar = "COPR-PROJECT-TPL" ,
146- type = str ,
147- dest = "copr_project_tpl" ,
148- default = cfg .copr_project_tpl ,
149- help = "Copr project name to check. 'YYYYMMDD' will be replaced, so make sure you have it in there." ,
228+ add_strategy_argument (sp )
229+ sp .add_argument (
230+ "--lookback" ,
231+ metavar = "DAY" ,
232+ type = int ,
233+ nargs = "+" ,
234+ dest = "lookback_days" ,
235+ default = [0 ],
236+ help = "Integers for how many days to look back (0 means just today)" ,
150237 )
151238
152- subparser_check .add_argument (
153- "--copr-monitor-tpl" ,
154- metavar = "COPR-MONITOR-TPL" ,
155- type = str ,
156- dest = "copr_monitor_tpl" ,
157- default = cfg .copr_monitor_tpl ,
158- help = "URL to the Copr monitor page. We'll use this in the issue comment's body, not for querying Copr." ,
159- # See https://github.com/python/cpython/issues/113878 for when we can
160- # use the __doc__ of a dataclass field.
161- # help=config.Config.copr_monitor_tpl.__doc__
162- )
163239
164- subparser_check .add_argument (
165- "--yyyymmdd" ,
166- type = lambda s : datetime .datetime .strptime (s , "%Y%m%d" ),
167- dest = "datetime" ,
168- default = datetime .datetime .now ().strftime ("%Y%m%d" ),
169- help = "Default day for which to check" ,
240+ def argument_parser_check (cfg : config .Config , subparsers ) -> None :
241+ sp = subparsers .add_parser (
242+ "check" ,
243+ description = "Check Copr status and update today's github issue" ,
244+ ** ARG_PARSE_SHOW_DEFAULT_VALUE ,
170245 )
171-
172- # if args.config_file:
173- # config = configparser.ConfigParser()
174- # config.read(args.config_file)
175- # defaults = {}
176- # defaults.update(dict(config.items("Defaults")))
177- # mainparser.set_defaults(**defaults)
178- # args = mainparser.parse_args() # Overwrite arguments
179-
180- args = mainparser .parse_args ()
181-
182- cfg .github_token_env = args .github_token_env
183- cfg .github_repo = args .github_repo
184-
185- if args .command == "check" :
186- cfg .datetime = args .datetime
187- cfg .packages = args .packages
188- cfg .chroot_pattern = args .chroot_pattern
189- cfg .build_strategy = args .build_strategy
190- cfg .maintainer_handle = args .maintainer_handle
191- cfg .copr_ownername = args .copr_ownername
192- cfg .copr_project_tpl = args .copr_project_tpl
193- cfg .copr_monitor_tpl = args .copr_monitor_tpl
194-
195- snapshot_manager .SnapshotManager (config = cfg ).check_todays_builds ()
196- elif args .command == "retest" :
197- snapshot_manager .SnapshotManager (config = cfg ).retest (
198- issue_number = args .issue_number ,
199- trigger_comment_id = args .trigger_comment_id ,
200- chroots = args .chroots ,
201- )
202- else :
203- logging .error (f"Unsupported argument: { args .command } " )
246+ add_strategy_argument (sp )
247+ add_yyyymmdd_argument (sp )
204248
205249
206250if __name__ == "__main__" :
0 commit comments