|
22 | 22 |
|
23 | 23 | import gettext |
24 | 24 | import logging |
25 | | -import os |
26 | | -import subprocess |
27 | 25 | import sys |
28 | 26 |
|
| 27 | +import checkbox_ng |
| 28 | + |
29 | 29 | from plainbox.impl.jobcache import ResourceJobCache |
30 | 30 | from plainbox.impl.session.assistant import SessionAssistant |
31 | 31 |
|
32 | | -from checkbox_ng.config import load_configs |
| 32 | +from checkbox_ng.utils import set_all_loggers_level |
33 | 33 | from checkbox_ng.launcher.subcommands import ( |
34 | 34 | Launcher, |
35 | 35 | List, |
@@ -62,6 +62,35 @@ def reset_sa(self): |
62 | 62 | self.sa = SessionAssistant() |
63 | 63 |
|
64 | 64 |
|
| 65 | +def handle_top_parser(args, ctx): |
| 66 | + """ |
| 67 | + The top level parser may not contain all args as "launcher" is inserted |
| 68 | + to get a default command. Lets handle the args as stings here and unify the |
| 69 | + args (from the top level parser) and ctx.args (from the sub parser) |
| 70 | + """ |
| 71 | + if "--debug" in sys.argv: |
| 72 | + logging_level = logging.DEBUG |
| 73 | + logging.basicConfig(level=logging_level) |
| 74 | + set_all_loggers_level(logging.DEBUG) |
| 75 | + ctx.args.debug = True |
| 76 | + elif "--verbose" in sys.argv or "-v" in sys.argv: |
| 77 | + logging_level = logging.INFO |
| 78 | + logging.basicConfig(level=logging_level) |
| 79 | + set_all_loggers_level(logging.INFO) |
| 80 | + ctx.args.verbose = True |
| 81 | + if "--clear-cache" in sys.argv: |
| 82 | + ResourceJobCache().clear() |
| 83 | + ctx.args.clear_cache = True |
| 84 | + if "--clear-old-sessions" in sys.argv: |
| 85 | + old_sessions = [s[0] for s in ctx.sa.get_old_sessions()] |
| 86 | + ctx.sa.delete_sessions(old_sessions) |
| 87 | + ctx.args.clear_old_sessions = True |
| 88 | + if "--version" in sys.argv: |
| 89 | + print(checkbox_ng.__version__) |
| 90 | + raise SystemExit(0) |
| 91 | + return ctx |
| 92 | + |
| 93 | + |
65 | 94 | def main(): |
66 | 95 | import argparse |
67 | 96 |
|
@@ -105,6 +134,7 @@ def main(): |
105 | 134 | ) |
106 | 135 |
|
107 | 136 | top_parser = argparse.ArgumentParser() |
| 137 | + # You must handle these args in the function above, see docstring |
108 | 138 | top_parser.add_argument( |
109 | 139 | "-v", |
110 | 140 | "--verbose", |
@@ -148,21 +178,7 @@ def main(): |
148 | 178 | sub_args = subcmd_parser.parse_args(sys.argv[subcmd_index + 1 :]) |
149 | 179 | sa = SessionAssistant() |
150 | 180 | ctx = Context(sub_args, sa) |
151 | | - try: |
152 | | - socket.getaddrinfo("localhost", 443) # 443 for HTTPS |
153 | | - except Exception: |
154 | | - pass |
155 | | - if "--clear-cache" in sys.argv: |
156 | | - ResourceJobCache().clear() |
157 | | - if "--clear-old-sessions" in sys.argv: |
158 | | - old_sessions = [s[0] for s in sa.get_old_sessions()] |
159 | | - sa.delete_sessions(old_sessions) |
160 | | - if args.verbose: |
161 | | - logging_level = logging.INFO |
162 | | - logging.basicConfig(level=logging_level) |
163 | | - if args.debug: |
164 | | - logging_level = logging.DEBUG |
165 | | - logging.basicConfig(level=logging_level) |
| 181 | + ctx = handle_top_parser(args, ctx) |
166 | 182 | return subcmd.invoked(ctx) |
167 | 183 |
|
168 | 184 |
|
|
0 commit comments