| 1 |
#!/usr/bin/python3 |
| 2 |
import logging |
| 3 |
import sys |
| 4 |
|
| 5 |
from mini_buildd import api, cli, client |
| 6 |
|
| 7 |
LOG = logging.getLogger("mini_buildd") |
| 8 |
|
| 9 |
#: Needed for man page hack in setup.py |
| 10 |
DESCRIPTION = "Run API calls against a mini-buildd instance" |
| 11 |
|
| 12 |
|
| 13 |
class CLI(cli.CLI): |
| 14 |
def __init__(self): |
| 15 |
super().__init__("mini-buildd-api", DESCRIPTION, |
| 16 |
"Note: Uses ``python3-keyring`` to persist passwords (see ``man 1 keyring``)") |
| 17 |
|
| 18 |
self.parser.add_argument("-J", "--json", action="store_true", |
| 19 |
help="use parsable json output") |
| 20 |
self.parser.add_argument("--auto-confirm", action="store_true", |
| 21 |
help="force-bypass extra confirmation (for confirmable calls)") |
| 22 |
self.parser.add_argument("--auto-save-passwords", action="store_true", |
| 23 |
help="don't ask before saving passwords (via python3-keyring)") |
| 24 |
self.parser.add_argument("--script-mode", action="store_true", |
| 25 |
help="force not to be interactive") |
| 26 |
|
| 27 |
# Unfortunately, we cannot group the calls (yet), see http://bugs.python.org/issue14037, https://bugs.python.org/issue9341 |
| 28 |
self.subparsers = self.parser.add_subparsers(title="API calls (run 'mini-buildd-api <call> --help' for full single call help)", |
| 29 |
required=True, |
| 30 |
metavar="<call> [options]") |
| 31 |
|
| 32 |
for call, call_cls in api.CALLS.items(): |
| 33 |
call_parser = self._add_subparser(self.subparsers, call, call_cls.doc() + "\n.") |
| 34 |
self._add_endpoint(call_parser) |
| 35 |
for argument in call_cls.iarguments(): |
| 36 |
call_parser.add_argument(*argument.id_list, **argument.argparse_kvsargs()) |
| 37 |
call_parser.set_defaults(call=call_cls) |
| 38 |
|
| 39 |
def runcli(self): |
| 40 |
clnt = client.Client(self.args.endpoint, |
| 41 |
auto_confirm=self.args.auto_confirm, |
| 42 |
auto_save_passwords=self.args.auto_save_passwords, |
| 43 |
interactive=(not self.args.script_mode) and sys.stdout.isatty() and sys.stdin.isatty()) |
| 44 |
call = self.args.call.from_sloppy_args(**vars(self.args)) |
| 45 |
result = clnt.api(call) |
| 46 |
print(api.Call.get_plain(result, force_json=self.args.json)) |
| 47 |
|
| 48 |
|
| 49 |
if __name__ == "__main__": |
| 50 |
CLI().run() |