Example #1
0
File: cli.py Project: mfiers/kea2
def k2_manage():
    """
    Manage k2
    """

    meta = get_recursive_dict()
    meta['_conf'] = util.getconf()
    meta['_parser'] = argparse.ArgumentParser()
    meta['_kea2_subparser'] = meta['_parser'].add_subparsers(dest='command')

    util.load_plugins(meta, 'manage_plugin')

    meta['_args'] = meta['_parser'].parse_args()
    command = meta['_args'].command

    if command is None:
        meta['_parser'].print_help()
        exit()

    #RUN
    meta['_commands'][command](meta)
Example #2
0
File: cli.py Project: mfiers/kea2
def _get_base_argsparse(add_template=True):
    parser = argparse.ArgumentParser()
    parser.add_argument('-x', '--executor', default='simple')
    parser.add_argument('-v', '--verbose', action='count', default=0)
    parser.add_argument('-q', '--quiet', action='count', default=0)
    parser.add_argument('-X', '--execute', action='store_const', help='run immediately',
                        const='run', dest='execute')
    parser.add_argument('-N', '--do-not-execute', action='store_const', help='do not run',
                        const='notrun', dest='execute')

    #register shortcuts to executors
    cnf = util.getconf()
    for xname, xconf in cnf['executor'].items():
        altflag = xconf.get('altflag')
        if altflag is None:
            continue

        parser.add_argument('--' + altflag, dest='executor', action='store_const',
                            const=xname, help='use %s executor' % xname)
    if add_template:
        parser.add_argument('template')
    return parser
Example #3
0
File: cli.py Project: mfiers/kea2
def k2():

    meta = get_recursive_dict()
    meta['_conf'] = util.getconf()
    meta['_original_commandline'] = " ".join(sys.argv)

    util.load_plugins(meta, 'plugin')

    # Phase one - PREPARG - preparse arguments
    phase_one(meta)
    
    loglevel = 30\
      + 10 * meta['_preargs'].quiet \
      - 10 * meta['_preargs'].verbose

    lg.setLevel(loglevel)
    
    
    # Load proper executor
    executor = meta['_preargs'].executor
    lg.info("Executor: %s", executor)
    edata = meta['_conf']['executor'][executor]
    modname = edata['module']
    try:
        module = __import__(modname, fromlist=[''])
    except:
        lg.critical("error importing module %s", modname)
        lg.critical("executor conf: %s", str(meta['_conf']['executor']))
        raise
    meta['_conf'][executor]['_mod'] = module
    module.init(meta)

    # Phase two - parse command line parameters
    parameter_parse(meta)

    # Phase three - split templates in prolog, epilogs & main
    template_splitter(meta)

    global_meta = meta.copy()
    meta['_global_meta'] = global_meta

    # Phase three - expand templates
    run_hook('pre_expand', meta)

    def expander(meta):
        for pname, pdata in meta['_parameters'].items():
            pvalue = meta[pname]
            if isinstance(pvalue, list) and not 'multi' in pdata['flags']:
                for pvar in pvalue:
                    assert isinstance(pvar, dict)
                    meta = copy.copy(meta)
                    meta.update(pvar)
                    yield from expander(meta)
                break
        else:
            uid = '000'
            if meta['_expanded_parameters']:
                ep = sorted(meta['_expanded_parameters'])
                def _fix(v):
                    if v.startswith('.'):
                        v = os.path.basename(v)
                    return v
                uid = "-".join([_fix(meta[x]) for x in ep]).replace(' ', '')
            meta['_uid'] = uid
            yield copy.copy(meta)


    for i, meta in enumerate(expander(meta)):
        meta['i'] = i
        meta['_blocks']['main'] = meta['_src']
        for bname, block in meta['_blocks'].items():
            try:
                template = JENV.from_string(block)
            except:
                lg.critical("Invalid Template in %s", bname)
                lg.critical(block)
                raise

            try:
                lastblock = False
                while (lastblock is None) or (lastblock != block):
                    lastblock = block
                    block = template.render(_dictify(meta))
                    template = JENV.from_string(block)
            except:
                lg.critical("Template render problem in %s", bname)
                lg.critical(block)
                raise
            meta['_blocks'][bname] = block
        meta['_src'] = meta['_blocks']['main']

        run_hook('check_execute', meta)

        if meta.get('_skip', False):
            lg.debug("skipping")
        else:
            run_hook('to_execute', meta)


    run_hook('pre_execute')

    if meta['_args'].execute == 'run' or \
      (global_meta.get('_src_in_argv') and not  global_meta['_args'].execute == 'notrun'):
        lg.info("start execution")
        run_hook('execute')


    run_hook('post_execute')