Пример #1
0
def print_logs(paths, options, tree=None):
    for p in paths:
        path, port = rtctree.path.parse_path(p[1])
        if port:
            raise rts_exceptions.NotAComponentError(p[0])
        if not path[-1]:
            raise rts_exceptions.NotAComponentError(p[0])
        p.append(path)

    if not tree:
        parsed = [p[2] for p in paths]
        tree = rtctree.tree.RTCTree(paths=parsed, filter=parsed)

    filters = ','.join(options.filters)
    ids = []
    try:
        for p in paths:
            if not tree.has_path(p[2]):
                raise rts_exceptions.NoSuchObjectError(p[0])
            rtc = tree.get_node(p[2])
            if rtc.is_zombie:
                raise rts_exceptions.ZombieObjectError(p[0])
            if not rtc.is_component:
                raise rts_exceptions.NotAComponentError(p[0])

            id = rtc.add_logger(log_cb, level=options.level,
                    filters=filters)
            ids.append((rtc, id))
    except rtctree.exceptions.AddLoggerError, e:
        # Remove all the loggers that were added
        for i in ids:
            i[0].remove_logger(i[1])
        # Re-raise
        raise e
Пример #2
0
def print_logs(paths, options, tree=None):
    for p in paths:
        path, port = rtctree.path.parse_path(p[1])
        if port:
            raise rts_exceptions.NotAComponentError(p[0])
        if not path[-1]:
            raise rts_exceptions.NotAComponentError(p[0])
        p.append(path)

    if not tree:
        parsed = [p[2] for p in paths]
        tree = rtctree.tree.RTCTree(paths=parsed, filter=parsed)

    filters = ','.join(options.filters)
    ids = []
    try:
        for p in paths:
            if not tree.has_path(p[2]):
                raise rts_exceptions.NoSuchObjectError(p[0])
            rtc = tree.get_node(p[2])
            if rtc.is_zombie:
                raise rts_exceptions.ZombieObjectError(p[0])
            if not rtc.is_component:
                raise rts_exceptions.NotAComponentError(p[0])

            id = rtc.add_logger(log_cb, level=options.level, filters=filters)
            ids.append((rtc, id))
    except rtctree.exceptions.AddLoggerError, e:
        # Remove all the loggers that were added
        for i in ids:
            i[0].remove_logger(i[1])
        # Re-raise
        raise e
Пример #3
0
def manage_fsm(tgt_raw_path,
               tgt_full_path,
               command,
               argument,
               options,
               tree=None):
    path, port = rtctree.path.parse_path(tgt_full_path)
    if port:
        raise rts_exceptions.NotAComponentError(tgt_raw_path)
    if not path[-1]:
        raise rts_exceptions.NotAComponentError(tgt_raw_path)

    if not tree:
        tree = rtctree.tree.RTCTree(paths=path, filter=[path])

    if not tree.has_path(path):
        raise rts_exceptions.NoSuchObjectError(path)
    rtc = tree.get_node(path)
    if rtc.is_zombie:
        raise rts_exceptions.ZombieObjectError(path)
    if not rtc.is_component:
        raise rts_exceptions.NotAComponentError(path)

    fsm = rtc.get_extended_fsm_service()

    try:
        cmdfunc = FSM_CMDS[command]
        return cmdfunc(fsm, argument)
    except KeyError:
        raise Exception('unknown command: {0}'.format(command))
Пример #4
0
def manage_fsm(tgt_raw_path, tgt_full_path, command, argument, options, tree=None):
    path, port = rtctree.path.parse_path(tgt_full_path)
    if port:
        raise rts_exceptions.NotAComponentError(tgt_raw_path)
    if not path[-1]:
        raise rts_exceptions.NotAComponentError(tgt_raw_path)

    if not tree:
        tree = rtctree.tree.RTCTree(paths=path, filter=[path])

    if not tree.has_path(path):
        raise rts_exceptions.NoSuchObjectError(path)
    rtc = tree.get_node(path)
    if rtc.is_zombie:
        raise rts_exceptions.ZombieObjectError(path)
    if not rtc.is_component:
        raise rts_exceptions.NotAComponentError(path)

    fsm = rtc.get_extended_fsm_service()

    try:
        cmdfunc = FSM_CMDS[command]
        return cmdfunc(fsm, argument)
    except KeyError:
        raise Exception('unknown command: {0}'.format(command))
Пример #5
0
def cd(cmd_path, full_path):
    path, port = rtctree.path.parse_path(full_path)
    if port:
        raise rts_exceptions.NotADirectoryError(cmd_path)
    if not path[-1]:
        # Remove trailing slash part
        path = path[:-1]
    tree = rtctree.tree.RTCTree(paths=path)
    if not tree.has_path(path):
        raise rts_exceptions.NotADirectoryError(cmd_path)
    if not tree.is_directory(path):
        raise rts_exceptions.NotADirectoryError(cmd_path)
    return make_cmd_line(full_path)
Пример #6
0
def cd(cmd_path, full_path):
    path, port = rtctree.path.parse_path(full_path)
    if port:
        raise rts_exceptions.NotADirectoryError(cmd_path)
    if not path[-1]:
        # Remove trailing slash part
        path = path[:-1]
    tree = rtctree.tree.RTCTree(paths=path)
    if not tree.has_path(path):
        raise rts_exceptions.NotADirectoryError(cmd_path)
    if not tree.is_directory(path):
        raise rts_exceptions.NotADirectoryError(cmd_path)
    return make_cmd_line(full_path)
Пример #7
0
def cat_target(cmd_path, full_path, options, tree=None):
    use_colour = rtctree.utils.colour_supported(sys.stdout)

    path, port = rtctree.path.parse_path(full_path)
    if not path[-1]:
        # There was a trailing slash
        trailing_slash = True
        path = path[:-1]
    else:
        trailing_slash = False

    if not tree:
        if options.long > 0:
            # Longer output needs to look around the tree, so don't filter
            filter = []
        else:
            filter = [path]
        tree = rtctree.tree.RTCTree(paths=path, filter=filter)

    if not tree.has_path(path):
        raise rts_exceptions.NoSuchObjectError(cmd_path)
    object = tree.get_node(path)
    if port:
        if not object.is_component:
            raise rts_exceptions.NotAComponentError(cmd_path)
        if trailing_slash:
            raise rts_exceptions.NoSuchObjectError(cmd_path)
        p = object.get_port_by_name(port)
        if not p:
            raise rts_exceptions.PortNotFoundError(path, port)
        return format_port(p,
                           object,
                           start_indent=0,
                           use_colour=use_colour,
                           long=options.long)
    else:
        if object.is_component:
            if trailing_slash:
                raise rts_exceptions.NoSuchObjectError(cmd_path)
            return format_component(object,
                                    tree,
                                    use_colour=use_colour,
                                    long=options.long)
        elif object.is_manager:
            return format_manager(object,
                                  use_colour=use_colour,
                                  long=options.long)
        elif object.is_zombie:
            raise rts_exceptions.ZombieObjectError(cmd_path)
        else:
            raise rts_exceptions.NoSuchObjectError(cmd_path)
Пример #8
0
def print_logs(paths, options, tree=None):
    global counter
    for p in paths:
        path, port = rtctree.path.parse_path(p[1])
        if port:
            raise rts_exceptions.NotAComponentError(p[0])
        if not path[-1]:
            raise rts_exceptions.NotAComponentError(p[0])
        p.append(path)

    if not tree:
        parsed = [p[2] for p in paths]
        tree = rtctree.tree.RTCTree(paths=parsed, filter=parsed)

    rtcs = []
    event = Event()
    for p in paths:
        if not tree.has_path(p[2]):
            raise rts_exceptions.NoSuchObjectError(p[0])
        rtc = tree.get_node(p[2])
        if rtc.is_zombie:
            raise rts_exceptions.ZombieObjectError(p[0])
        if not rtc.is_component:
            raise rts_exceptions.NotAComponentError(p[0])

        rtc.dynamic = True
        if len(options.filters) == 0 or 'ALL' in options.filters:
            for (k,v) in filtermap.items():
                rtc.add_callback(v[0], v[1], [event])
        else:
            for f in options.filters:
                try:
                    v = filtermap[f]
                    rtc.add_callback(v[0], v[1], [event])
                except KeyError:
                    print('Unknown filter: {0}'.format(f))
        rtcs.append(rtc)

    # Wait for a keyboard interrupt
    counter = 0
    atexit.register(clean_events, rtcs)
    while True:
        if options.number > 0 and counter >= options.number:
            break
        try:
            event.wait()
        except KeyboardInterrupt:
            break
        event.clear()
    clean_events(rtcs)
Пример #9
0
def print_logs(paths, options, tree=None):
    global counter
    for p in paths:
        path, port = rtctree.path.parse_path(p[1])
        if port:
            raise rts_exceptions.NotAComponentError(p[0])
        if not path[-1]:
            raise rts_exceptions.NotAComponentError(p[0])
        p.append(path)

    if not tree:
        parsed = [p[2] for p in paths]
        tree = rtctree.tree.RTCTree(paths=parsed, filter=parsed)

    rtcs = []
    event = Event()
    for p in paths:
        if not tree.has_path(p[2]):
            raise rts_exceptions.NoSuchObjectError(p[0])
        rtc = tree.get_node(p[2])
        if rtc.is_zombie:
            raise rts_exceptions.ZombieObjectError(p[0])
        if not rtc.is_component:
            raise rts_exceptions.NotAComponentError(p[0])

        rtc.dynamic = True
        if len(options.filters) == 0 or "ALL" in options.filters:
            for (k, v) in filtermap.items():
                rtc.add_callback(v[0], v[1], [event])
        else:
            for f in options.filters:
                try:
                    v = filtermap[f]
                    rtc.add_callback(v[0], v[1], [event])
                except KeyError:
                    print("Unknown filter: {0}".format(f))
        rtcs.append(rtc)

    # Wait for a keyboard interrupt
    counter = 0
    atexit.register(clean_events, rtcs)
    while True:
        if options.number > 0 and counter >= options.number:
            break
        try:
            event.wait()
        except KeyboardInterrupt:
            break
        event.clear()
    clean_events(rtcs)
Пример #10
0
def print_logs(paths, options, tree=None):
    for p in paths:
        path, port = rtctree.path.parse_path(p[1])
        if port:
            raise rts_exceptions.NotAComponentError(p[0])
        if not path[-1]:
            raise rts_exceptions.NotAComponentError(p[0])
        p.append(path)

    if not tree:
        parsed = [p[2] for p in paths]
        tree = rtctree.tree.RTCTree(paths=parsed, filter=parsed)

    filters = ",".join(options.filters)
    ids = []
    try:
        for p in paths:
            if not tree.has_path(p[2]):
                raise rts_exceptions.NoSuchObjectError(p[0])
            rtc = tree.get_node(p[2])
            if rtc.is_zombie:
                raise rts_exceptions.ZombieObjectError(p[0])
            if not rtc.is_component:
                raise rts_exceptions.NotAComponentError(p[0])

            id = rtc.add_logger(log_cb, level=options.level, filters=filters)
            ids.append((rtc, id))
    except rtctree.exceptions.AddLoggerError as e:
        # Remove all the loggers that were added
        for i in ids:
            i[0].remove_logger(i[1])
        # Re-raise
        raise e

    # Wait for a keyboard interrupt
    try:
        while True:
            if sys.version_info[0] == 3:
                input()
            else:
                raw_input("")
    except KeyboardInterrupt:
        pass
    # Remove all the loggers that were added
    for i in ids:
        i[0].remove_logger(i[1])
Пример #11
0
def cat_target(cmd_path, full_path, options, tree=None):
    use_colour = sys.stdout.isatty()

    path, port = rtctree.path.parse_path(full_path)
    if not path[-1]:
        # There was a trailing slash
        trailing_slash = True
        path = path[:-1]
    else:
        trailing_slash = False

    if not tree:
        if options.long > 0:
            # Longer output needs to look around the tree, so don't filter
            filter = []
        else:
            filter = [path]
        tree = rtctree.tree.RTCTree(paths=path, filter=filter)

    if not tree.has_path(path):
        raise rts_exceptions.NoSuchObjectError(cmd_path)
    object = tree.get_node(path)
    if port:
        if not object.is_component:
            raise rts_exceptions.NotAComponentError(cmd_path)
        if trailing_slash:
            raise rts_exceptions.NoSuchObjectError(cmd_path)
        p = object.get_port_by_name(port)
        if not p:
            raise rts_exceptions.PortNotFoundError(path, port)
        return format_port(p, object, start_indent=0,
                use_colour=sys.stdout.isatty(), long=options.long)
    else:
        if object.is_component:
            if trailing_slash:
                raise rts_exceptions.NoSuchObjectError(cmd_path)
            return format_component(object, tree, use_colour=sys.stdout.isatty(),
                    long=options.long)
        elif object.is_manager:
            return format_manager(object, use_colour=sys.stdout.isatty(),
                    long=options.long)
        elif object.is_zombie:
            raise rts_exceptions.ZombieObjectError(cmd_path)
        else:
            raise rts_exceptions.NoSuchObjectError(cmd_path)
Пример #12
0
def exit_target(cmd_path, full_path, options, tree=None):
    path, port = rtctree.path.parse_path(full_path)
    if port:
        raise rts_exceptions.NotAComponentError(cmd_path)

    trailing_slash = False
    if not path[-1]:
        raise rts_exceptions.NotAComponentError(cmd_path)

    if not tree:
        tree = rtctree.tree.RTCTree(paths=path, filter=[path])

    if not tree.has_path(path):
        raise rts_exceptions.NoSuchObjectError(cmd_path)
    object = tree.get_node(path)
    if object.is_zombie:
        raise rts_exceptions.ZombieObjectError(cmd_path)
    if not object.is_component:
        raise rts_exceptions.NotAComponentError(cmd_path)

    object.exit()
Пример #13
0
def exit_target(cmd_path, full_path, options, tree=None):
    path, port = rtctree.path.parse_path(full_path)
    if port:
        raise rts_exceptions.NotAComponentError(cmd_path)

    trailing_slash = False
    if not path[-1]:
        raise rts_exceptions.NotAComponentError(cmd_path)

    if not tree:
        tree = rtctree.tree.RTCTree(paths=path, filter=[path])

    if not tree.has_path(path):
        raise rts_exceptions.NoSuchObjectError(cmd_path)
    object = tree.get_node(path)
    if object.is_zombie:
        raise rts_exceptions.ZombieObjectError(cmd_path)
    if not object.is_component:
        raise rts_exceptions.NotAComponentError(cmd_path)

    object.exit()
Пример #14
0
def get_docs(cmd_path, full_path, options, tree=None):
    path, port = rtctree.path.parse_path(full_path)
    if not path[-1]:
        # There was a trailing slash
        raise rts_exceptions.NotAComponentError(cmd_path)
    if port:
        raise rts_exceptions.NotAComponentError(cmd_path)

    if not tree:
        tree = rtctree.tree.RTCTree(paths=path)

    if not tree.has_path(path):
        raise rts_exceptions.NoSuchObjectError(cmd_path)
    object = tree.get_node(path)

    if object.is_component:
        return get_comp_docs(object, tree, options)
    elif object.is_zombie:
        raise rts_exceptions.ZombieObjectError(cmd_path)
    else:
        raise rts_exceptions.NotAComponentError(cmd_path)
Пример #15
0
def get_docs(cmd_path, full_path, options, tree=None):
    path, port = rtctree.path.parse_path(full_path)
    if not path[-1]:
        # There was a trailing slash
        raise rts_exceptions.NotAComponentError(cmd_path)
    if port:
        raise rts_exceptions.NotAComponentError(cmd_path)

    if not tree:
        tree = rtctree.tree.RTCTree(paths=path)

    if not tree.has_path(path):
        raise rts_exceptions.NoSuchObjectError(cmd_path)
    object = tree.get_node(path)

    if object.is_component:
        return get_comp_docs(object, tree, options)
    elif object.is_zombie:
        raise rts_exceptions.ZombieObjectError(cmd_path)
    else:
        raise rts_exceptions.NotAComponentError(cmd_path)
Пример #16
0
def alter_component_states(action, paths, options, tree=None):
    cmd_paths, fps = zip(*paths)
    pathports = [rtctree.path.parse_path(fp) for fp in fps]
    for ii, p in enumerate(pathports):
        if p[1]:
            raise rts_exceptions.NotAComponentError(cmd_paths[ii])
        if not p[0][-1]:
            raise rts_exceptions.NotAComponentError(cmd_paths[ii])
    paths, ports = zip(*pathports)

    if not tree:
        tree = rtctree.tree.RTCTree(paths=paths, filter=paths)

    for ii, p in enumerate(paths):
        if not tree.has_path(p):
            raise rts_exceptions.NoSuchObjectError(cmd_paths[ii])
        object_ = tree.get_node(p)
        if object_.is_zombie:
            raise rts_exceptions.ZombieObjectError(cmd_paths[ii])
        if not object_.is_component:
            raise rts_exceptions.NotAComponentError(cmd_paths[ii])
        action(object_, options.ec_index)
Пример #17
0
def get_comp(rtc, tree=None, orb=None):
    '''Get a rtctree.Component object from an rtctree.RTCTree.

    Get the component object by searching the RTCTree for the specified RTC.

    @param rtc Path to the component. This should be in
               the format used by rtctree, i.e. a list of path entries, with
               the first being a /. e.g. ['/', 'localhost', 'comp0.rtc'].
    @param tree An already-populated rtctree.RTCTree object, or None if one
                should be created.
    @param orb An ORB to use if the tree must be created, or None to make one.

    '''
    if not tree:
        tree = rtctree.tree.RTCTree(paths=rtc, orb=orb, filter=[rtc])

    if not tree.has_path(rtc):
        raise rts_exceptions.NoSuchObjectError(rtc)
    comp = tree.get_node(rtc)
    if not comp.is_component:
        raise rts_exceptions.NotAComponentError(rtc)
    return comp
Пример #18
0
def get_comp(rtc, tree=None, orb=None):
    '''Get a rtctree.Component object from an rtctree.RTCTree.

    Get the component object by searching the RTCTree for the specified RTC.

    @param rtc Path to the component. This should be in
               the format used by rtctree, i.e. a list of path entries, with
               the first being a /. e.g. ['/', 'localhost', 'comp0.rtc'].
    @param tree An already-populated rtctree.RTCTree object, or None if one
                should be created.
    @param orb An ORB to use if the tree must be created, or None to make one.

    '''
    if not tree:
        tree = rtctree.tree.RTCTree(paths=rtc, orb=orb, filter=[rtc])

    if not tree.has_path(rtc):
        raise rts_exceptions.NoSuchObjectError(rtc)
    comp = tree.get_node(rtc)
    if not comp.is_component:
        raise rts_exceptions.NotAComponentError(rtc)
    return comp
Пример #19
0
def list_target(cmd_path, full_path, options, tree=None):
    use_colour = rtctree.utils.colour_supported(sys.stdout)

    path, port = rtctree.path.parse_path(full_path)
    if port:
        raise rts_exceptions.CannotDoToPortError('list')

    trailing_slash = False
    if not path[-1]:
        # There was a trailing slash
        trailing_slash = True
        path = path[:-1]

    if not tree:
        tree = rtctree.tree.RTCTree(paths=path, filter=[path])

    if not tree.has_path(path):
        raise rts_exceptions.NoSuchObjectError(cmd_path)
    if tree.is_component(path) or tree.is_unknown(path) or \
            tree.is_zombie(path):
        # Path points to a single object: print it like 'ls <file>'.
        if trailing_slash:
            # If there was a trailing slash, complain that the object is not a
            # directory.
            raise rts_exceptions.NoSuchObjectError(cmd_path)
        if options.long:
            return get_node_long_lines([tree.get_node(path)], use_colour)
        else:
            if tree.is_component(path):
                return [path[-1]]
            elif tree.is_zombie(path):
                return [(rtctree.utils.build_attr_string(['faint', 'white'],
                        supported=use_colour) + '*' + path[-1] +
                        rtctree.utils.build_attr_string(['reset'],
                        supported=use_colour))]
            else:
                # Assume unknown
                return [(rtctree.utils.build_attr_string(['faint', 'white'],
                        supported=use_colour) + path[-1] +
                        rtctree.utils.build_attr_string(['reset'],
                        supported=use_colour))]
    elif tree.is_directory(path):
        # If recursing, need to list this directory and all its children
        if options.recurse:
            recurse_root = tree.get_node(path)
            recurse_root_path = recurse_root.full_path_str
            def get_name(node, args):
                if node.full_path_str.startswith(recurse_root_path):
                    result = node.full_path_str[len(recurse_root_path):]
                else:
                    result = node.full_path_str
                return result.lstrip('/')
            dir_names = ['.'] + recurse_root.iterate(get_name,
                    args=options.long, filter=['is_directory'])[1:]
            listings = recurse_root.iterate(list_directory,
                    args=options.long, filter=['is_directory'])
            result = []
            for dir, listing in zip(dir_names, listings):
                if dir == '.':
                    result.append('.:')
                else:
                    result.append('./' + dir + ':')
                result += listing
                result.append('')
            return result
        else:
            dir_node = tree.get_node(path)
            return list_directory(dir_node, options.long)
    else:
        raise rts_exceptions.UnknownObjectError(cmd_path)
Пример #20
0
def list_target(cmd_path, full_path, options, tree=None):
    use_colour = rtctree.utils.colour_supported(sys.stdout)

    path, port = rtctree.path.parse_path(full_path)
    if port:
        raise rts_exceptions.CannotDoToPortError('list')

    trailing_slash = False
    if not path[-1]:
        # There was a trailing slash
        trailing_slash = True
        path = path[:-1]

    if not tree:
        tree = rtctree.tree.RTCTree(paths=path, filter=[path])

    if not tree.has_path(path):
        raise rts_exceptions.NoSuchObjectError(cmd_path)
    if tree.is_component(path) or tree.is_unknown(path) or \
            tree.is_zombie(path):
        # Path points to a single object: print it like 'ls <file>'.
        if trailing_slash:
            # If there was a trailing slash, complain that the object is not a
            # directory.
            raise rts_exceptions.NoSuchObjectError(cmd_path)
        if options.long:
            return get_node_long_lines([tree.get_node(path)], use_colour)
        else:
            if tree.is_component(path):
                return [path[-1]]
            elif tree.is_zombie(path):
                return [(rtctree.utils.build_attr_string(
                    ['faint', 'white'], supported=use_colour) + '*' +
                         path[-1] + rtctree.utils.build_attr_string(
                             ['reset'], supported=use_colour))]
            else:
                # Assume unknown
                return [(rtctree.utils.build_attr_string(
                    ['faint', 'white'], supported=use_colour) + path[-1] +
                         rtctree.utils.build_attr_string(
                             ['reset'], supported=use_colour))]
    elif tree.is_directory(path):
        # If recursing, need to list this directory and all its children
        if options.recurse:
            recurse_root = tree.get_node(path)
            recurse_root_path = recurse_root.full_path_str

            def get_name(node, args):
                if node.full_path_str.startswith(recurse_root_path):
                    result = node.full_path_str[len(recurse_root_path):]
                else:
                    result = node.full_path_str
                return result.lstrip('/')

            dir_names = ['.'] + recurse_root.iterate(
                get_name, args=options.long, filter=['is_directory'])[1:]
            listings = recurse_root.iterate(list_directory,
                                            args=options.long,
                                            filter=['is_directory'])
            result = []
            for dir, listing in zip(dir_names, listings):
                if dir == '.':
                    result.append('.:')
                else:
                    result.append('./' + dir + ':')
                result += listing
                result.append('')
            return result
        else:
            dir_node = tree.get_node(path)
            return list_directory(dir_node, options.long)
    else:
        raise rts_exceptions.UnknownObjectError(cmd_path)