Пример #1
0
def _do_run_file(cli, conf_file):
    if not os.path.exists(conf_file):
        cli.err('Cannot open file "%s"' % conf_file)
        return

    xformed = sugar.xform_file(conf_file)

    new_globals = {
            '__builtins__': __builtins__,
            'softnic': cli.softnic,
            'ConfError': ConfError,
            '__bess_env__': __bess_env__,
            '__bess_module__': __bess_module__,
        }

    class_names = cli.softnic.list_mclasses()

    # Add the special Port class. TODO: per-driver classes
    new_globals['Port'] = type('Port', (Port,), 
            {'softnic': cli.softnic, 'choose_arg': _choose_arg})

    # Add SoftNIC module classes
    for name in class_names:
        if name in new_globals:
            raise cli.InternalError('Invalid module class name: %s' % name)

        new_globals[name] = type(name, (Module,),
                {'softnic': cli.softnic, 'choose_arg': _choose_arg})

    code = compile(xformed, conf_file, 'exec')

    cli.softnic.pause_all()
    try:
        exec(code, new_globals)
        cli.fout.write('Done.\n')
    except cli.softnic.Error:
        raise

    except cli.softnic.APIError:
        raise

    except ConfError as e:
        cli.err(e.message)

    except:
        cur_frame = inspect.currentframe()
        cur_func = inspect.getframeinfo(cur_frame).function
        t, v, tb = sys.exc_info()
        stack = traceback.extract_tb(tb)

        while len(stack) > 0 and stack.pop(0)[2] != cur_func:
            pass

        cli.err('Unhandled exception in the script (most recent call last)')
        cli.fout.write(''.join(traceback.format_list(stack)))
        cli.fout.write(''.join(traceback.format_exception_only(t, v)))

    finally:
        cli.softnic.resume_all()
Пример #2
0
def _do_run_file(cli, conf_file):
    if not os.path.exists(conf_file):
        cli.err('Cannot open file "%s"' % conf_file)
        return

    xformed = sugar.xform_file(conf_file)

    new_globals = {
        '__builtins__': __builtins__,
        'bess': cli.bess,
        'ConfError': ConfError,
        '__bess_env__': __bess_env__,
        '__bess_module__': __bess_module__,
    }

    class_names = cli.bess.list_mclasses().names
    driver_names = cli.bess.list_drivers().driver_names

    # Add BESS port classes
    for name in driver_names:
        if name in new_globals:
            raise cli.InternalError('Invalid driver name: %s' % name)

        new_globals[name] = type(str(name), (Port, ), {
            'bess': cli.bess,
            'choose_arg': _choose_arg
        })

    # Add BESS module classes
    for name in class_names:
        if name in new_globals:
            raise cli.InternalError('Invalid module class name: %s' % name)

        new_globals[name] = type(str(name), (Module, ), {
            'bess': cli.bess,
            'choose_arg': _choose_arg
        })

    code = compile(xformed, conf_file, 'exec')

    if is_pipeline_empty(cli):
        cli.bess.pause_all()
    else:
        ret = warn(cli, 'The current pipeline will be reset.', _clear_pipeline)
        if ret is False:
            return

    try:
        exec(code, new_globals)
        if cli.interactive:
            cli.fout.write('Done.\n')
    except:
        cur_frame = inspect.currentframe()
        cur_func = inspect.getframeinfo(cur_frame).function
        t, v, tb = sys.exc_info()
        stack = traceback.extract_tb(tb)

        while len(stack) > 0 and stack.pop(0)[2] != cur_func:
            pass

        errmsg = 'Unhandled exception in the configuration script'

        cli.err('%s (most recent call last)' % errmsg)
        cli.ferr.write(''.join(traceback.format_list(stack)))

        if isinstance(v, cli.bess.Error):
            raise
        else:
            cli.ferr.write(''.join(traceback.format_exception_only(t, v)))
            raise cli.HandledError()
    finally:
        cli.bess.resume_all()
Пример #3
0
def _do_run_file(cli, conf_file):
    if not os.path.exists(conf_file):
        cli.err('Cannot open file "%s"' % conf_file)
        return

    xformed = sugar.xform_file(conf_file)

    new_globals = {
            '__builtins__': __builtins__,
            'bess': cli.bess,
            'ConfError': ConfError,
            '__bess_env__': __bess_env__,
            '__bess_module__': __bess_module__,
        }

    class_names = cli.bess.list_mclasses().names
    driver_names = cli.bess.list_drivers().driver_names

    # Add BESS port classes
    for name in driver_names:
        if name in new_globals:
            raise cli.InternalError('Invalid driver name: %s' % name)

        new_globals[name] = type(str(name), (Port,),
                                 {'bess': cli.bess})

    # Add BESS module classes
    for name in class_names:
        if name in new_globals:
            raise cli.InternalError('Invalid module class name: %s' % name)

        new_globals[name] = type(str(name), (Module,),
                                 {'bess': cli.bess})

    code = compile(xformed, conf_file, 'exec')

    if is_pipeline_empty(cli):
        cli.bess.pause_all()
    else:
        ret = warn(cli, 'The current pipeline will be reset.', _clear_pipeline)
        if ret is False:
            return

    try:
        exec(code, new_globals)
        if cli.interactive:
            cli.fout.write('Done.\n')
    except:
        cur_frame = inspect.currentframe()
        cur_func = inspect.getframeinfo(cur_frame).function
        t, v, tb = sys.exc_info()
        stack = traceback.extract_tb(tb)

        while len(stack) > 0 and stack.pop(0)[2] != cur_func:
            pass

        errmsg = 'Unhandled exception in the configuration script'

        cli.err('%s (most recent call last)' % errmsg)
        cli.fout.write(''.join(traceback.format_list(stack)))

        if isinstance(v, cli.bess.Error):
            raise
        else:
            cli.fout.write(''.join(traceback.format_exception_only(t, v)))
            raise cli.HandledError()
    finally:
        cli.bess.resume_all()
Пример #4
0
 def test(self):
     xformed = sugar.xform_file(path)
     code = compile(xformed, path, 'exec')
Пример #5
0
def _do_run_file(cli, conf_file):
    def clear_pipeline():
        cli.softnic.pause_all()
        cli.softnic.reset_all()

    if not os.path.exists(conf_file):
        cli.err('Cannot open file "%s"' % conf_file)
        return

    xformed = sugar.xform_file(conf_file)

    new_globals = {
            '__builtins__': __builtins__,
            'softnic': cli.softnic,
            'ConfError': ConfError,
            '__bess_env__': __bess_env__,
            '__bess_module__': __bess_module__,
        }

    class_names = cli.softnic.list_mclasses()

    # Add the special Port class. TODO: per-driver classes
    new_globals['Port'] = type('Port', (Port,), 
            {'softnic': cli.softnic, 'choose_arg': _choose_arg})

    # Add SoftNIC module classes
    for name in class_names:
        if name in new_globals:
            raise cli.InternalError('Invalid module class name: %s' % name)

        new_globals[name] = type(name, (Module,),
                {'softnic': cli.softnic, 'choose_arg': _choose_arg})

    code = compile(xformed, conf_file, 'exec')

    if is_pipeline_empty(cli):
        cli.softnic.pause_all()
    else:
        ret = warn(cli, 'The current pipeline will be reset.', clear_pipeline)
        if ret is False:
            return

    try:
        exec(code, new_globals)
        if cli.interactive:
            cli.fout.write('Done.\n')
    except cli.softnic.Error:
        raise

    except cli.softnic.APIError:
        raise

    except ConfError as e:
        cli.err(e.message)

    except:
        cur_frame = inspect.currentframe()
        cur_func = inspect.getframeinfo(cur_frame).function
        t, v, tb = sys.exc_info()
        stack = traceback.extract_tb(tb)

        while len(stack) > 0 and stack.pop(0)[2] != cur_func:
            pass

        cli.err('Unhandled exception in the script (most recent call last)')
        cli.fout.write(''.join(traceback.format_list(stack)))
        cli.fout.write(''.join(traceback.format_exception_only(t, v)))

    finally:
        cli.softnic.resume_all()