|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# interactive console with easy access to krb5 test harness stuff |
| 4 | + |
| 5 | +import argparse |
| 6 | +import os |
| 7 | +import sys |
| 8 | +import copy |
| 9 | + |
| 10 | +import pkg_resources as pkgres |
| 11 | + |
| 12 | +from gssapi_console import GSSAPIConsole |
| 13 | + |
| 14 | + |
| 15 | +parser = argparse.ArgumentParser( |
| 16 | + description='An interactive Python console with krb5 setup') |
| 17 | +parser.add_argument('file', metavar='FILE', nargs='?', |
| 18 | + help='a file to run', default=None) |
| 19 | +parser.add_argument('-i', default=False, dest='force_interactive', |
| 20 | + action='store_const', const=True, |
| 21 | + help='Force interactive mode when running with a file') |
| 22 | +parser.add_argument('--realm-args', default=None, |
| 23 | + help='A comma-separated list of key=(true|false) values ' |
| 24 | + 'to pass to the realm constructor') |
| 25 | +parser.add_argument('--mech', default='krb5', |
| 26 | + help='Which environment to setup up ' |
| 27 | + '(supports krb5 [default])') |
| 28 | + |
| 29 | +PARSED_ARGS = parser.parse_args() |
| 30 | + |
| 31 | +realm_args = {} |
| 32 | +if PARSED_ARGS.realm_args: |
| 33 | + for arg in PARSED_ARGS.realm_args.split(','): |
| 34 | + key, raw_val = arg.split('=') |
| 35 | + realm_args[key] = (raw_val.lower() == 'true') |
| 36 | + |
| 37 | +try: |
| 38 | + mech_cls_loader = next( |
| 39 | + pkgres.iter_entry_points('gssapi_console.drivers', |
| 40 | + name=PARSED_ARGS.mech)) |
| 41 | +except StopIteration: |
| 42 | + sys.exit('The %s environment is not supported by the ' |
| 43 | + 'GSSAPI console' % PARSED_ARGS.mech) |
| 44 | + |
| 45 | +mech_cls = mech_cls_loader.load() |
| 46 | +console = GSSAPIConsole(mech_cls, realm_args=realm_args) |
| 47 | + |
| 48 | +SAVED_ENV = None |
| 49 | + |
| 50 | +try: |
| 51 | + # import the env |
| 52 | + SAVED_ENV = copy.deepcopy(os.environ) |
| 53 | + for k, v in console.realm.env.items(): |
| 54 | + os.environ[k] = v |
| 55 | + |
| 56 | + INTER = True |
| 57 | + # run the interactive interpreter |
| 58 | + if PARSED_ARGS.file is not None: |
| 59 | + if not PARSED_ARGS.force_interactive: |
| 60 | + INTER = False |
| 61 | + |
| 62 | + with open(PARSED_ARGS.file) as src: |
| 63 | + console.runsource(src.read(), src.name, 'exec') |
| 64 | + |
| 65 | + if INTER: |
| 66 | + console.interact() |
| 67 | + |
| 68 | +except (KeyboardInterrupt, EOFError): |
| 69 | + pass |
| 70 | +finally: |
| 71 | + # restore the env |
| 72 | + if SAVED_ENV is not None: |
| 73 | + for k in copy.deepcopy(os.environ): |
| 74 | + if k in SAVED_ENV: |
| 75 | + os.environ[k] = SAVED_ENV[k] |
| 76 | + else: |
| 77 | + del os.environ[k] |
| 78 | + |
| 79 | + console.realm.stop() |
0 commit comments