Esempio n. 1
0
def configurator_cli(argv):
    """configurator_cli [-s <script>] [-g] <device>

    Interact with a DUT configurator. If no device is specified use the testbed DUT.

    Options:
        -g Use paged output (like 'more')
        -s <script> Run a CLI script from the given file instead of entering
           interactive mode.

    """
    import os
    from pycopia.QA import configurator
    from pycopia import getopt
    from pycopia.storage import Storage

    paged = False
    script = None

    try:
        optlist, longopts, args = getopt.getopt(argv[1:], "s:?g")
    except GetoptError:
        print configurator_cli.__doc__
        return
    for opt, val in optlist:
        if opt == "-?":
            print configurator_cli.__doc__
            return
        elif opt == "-g":
            paged = True
        elif opt == "-s":
            script = val

    if not args:
        print configurator_cli.__doc__
        return

    if paged:
        from pycopia import tty
        io = tty.PagedIO()
    else:
        io = IO.ConsoleIO()

    # do runtime setup
    cf = Storage.get_config(initdict=longopts)
    cf.reportfile = "configurator_cli"
    cf.logbasename = "configurator_cli.log"
    cf.arguments = argv

    dev = cf.devices[args[0]]

    ctor = configurator.get_configurator(dev, logfile=cf.logfile)

    # construct the CLI
    theme = ConfiguratorTheme("Configurator> ")
    ui = UI.UserInterface(io, cf, theme)
    cmd = CLI.get_generic_cmd(ctor, ui, ConfiguratorShellCLI)
    cmd.device = dev  # stash actual device for future reference
    parser = CLI.CommandParser(
        cmd, historyfile=os.path.expandvars("$HOME/.hist_configurator"))

    if script:
        try:
            parser.parse(script)
        except KeyboardInterrupt:
            pass
    else:
        parser.interact()
    try:
        ctor.exit()
    except:
        pass