Exemplo n.º 1
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)
Exemplo n.º 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
Exemplo n.º 3
0
def connect_ports(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 not p[1]:
            raise rts_exceptions.NotAPortError(cmd_paths[ii])
        if not p[0][-1]:
            raise rts_exceptions.NotAPortError(cmd_paths[ii])
    paths, ports = zip(*pathports)

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

    port_objs = []
    for ii, p in enumerate(pathports):
        obj = tree.get_node(p[0])
        if not obj:
            raise rts_exceptions.NoSuchObjectError(cmd_paths[ii])
        if obj.is_zombie:
            raise rts_exceptions.ZombieObjectError(cmd_paths[ii])
        if not obj.is_component:
            raise rts_exceptions.NotAComponentError(cmd_paths[ii])
        port_obj = obj.get_port_by_name(p[1])
        if not port_obj:
            raise rts_exceptions.PortNotFoundError(p[0], p[1])
        port_objs.append(port_obj)

    conn_name = options.name if options.name else None
    port_objs[0].connect(port_objs[1:],
                         name=conn_name,
                         id=options.id,
                         props=options.properties)
Exemplo n.º 4
0
def get_comp_objs(paths, tree):
    objs = {}
    for fp, cp, ports in paths:
        obj = tree.get_node(cp)
        if not obj:
            raise rts_exceptions.NoSuchObjectError(fp)
        if not obj.is_component:
            raise rts_exceptions.NotAComponentError(fp)
        objs[fp] = (obj, ports)
    return objs
Exemplo n.º 5
0
def get_comp(cmd_path, full_path, tree=None):
    path, port = rtctree.path.parse_path(full_path)
    if port:
        # Can't configure a port
        raise rts_exceptions.NotAComponentError(cmd_path)
    if not path[-1]:
        # There was a trailing slash
        raise rts_exceptions.NoSuchObjectError(cmd_path)

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

    comp = tree.get_node(path)
    if not comp:
        raise rts_exceptions.NoSuchObjectError(cmd_path)
    if comp.is_zombie:
        raise rts_exceptions.ZombieObjectError(cmd_path)
    if not comp.is_component:
        raise rts_exceptions.NotAComponentError(cmd_path)
    return tree, comp
Exemplo n.º 6
0
def disconnect_all(cmd_path, full_path, options, tree=None):
    path, port = rtctree.path.parse_path(full_path)
    if not path[-1]:
        raise rts_exceptions.NoSuchObjectError(cmd_path)
    if not tree:
        tree = rtctree.tree.RTCTree(paths=path, filter=[path])

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

    if port:
        # Disconnect a single port
        port_obj = object.get_port_by_name(port)
        if not port_obj:
            raise rts_exceptions.PortNotFoundError(path, port)
        if options.id:
            conn = port_obj.get_connection_by_id(options.id)
            if not conn:
                raise rts_exceptions.ConnectionIDNotFoundError(
                    options.id, cmd_path)
            conn.disconnect()
        else:
            port_obj.disconnect_all()
    else:
        if options.id:
            # Hunt through the ports for the connection ID
            for p in object.ports:
                conn = p.get_connection_by_id(options.id)
                if not conn:
                    continue
                conn.disconnect()
                return
            raise rts_exceptions.ConnectionIDNotFoundError(
                options.id, cmd_path)
        else:
            # Disconnect all ports
            object.disconnect_all()
Exemplo n.º 7
0
def disconnect_ports(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 not p[1]:
            raise rts_exceptions.NotAPortError(cmd_paths[ii])
        if not p[0][-1]:
            raise rts_exceptions.NotAPortError(cmd_paths[ii])
    paths, ports = zip(*pathports)

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

    port_objs = []
    for ii, p in enumerate(pathports):
        obj = tree.get_node(p[0])
        if not obj:
            raise rts_exceptions.NoSuchObjectError(cmd_paths[ii])
        if obj.is_zombie:
            raise rts_exceptions.ZombieObjectError(cmd_paths[ii])
        if not obj.is_component:
            raise rts_exceptions.NotAComponentError(cmd_paths[ii])
        port_obj = obj.get_port_by_name(p[1])
        if not port_obj:
            raise rts_exceptions.PortNotFoundError(p[0], p[1])
        port_objs.append(port_obj)
    if len(port_objs) < 2:
        raise rts_exceptions.NoDestPortError

    if options.id:
        all_conns = port_objs[0].get_connections_by_dests(port_objs[1:])
        conns = []
        for c in all_conns:
            if c.id == options.id:
                conns.append(c)
    else:
        conns = port_objs[0].get_connections_by_dests(port_objs[1:])

    if not conns:
        raise rts_exceptions.MultiConnectionNotFoundError
    for c in conns:
        c.disconnect()
Exemplo n.º 8
0
def get_manager(cmd_path, full_path, tree=None):
    path, port = rtctree.path.parse_path(full_path)
    if port:
        raise rts_exceptions.NotAManagerError(cmd_path)

    if not path[-1]:
        # There was a trailing slash - ignore it
        path = path[:-1]

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

    object = tree.get_node(path)
    if not object:
        raise rts_exceptions.NoSuchObjectError(cmd_path)
    if object.is_zombie:
        raise rts_exceptions.ZombieObjectError(cmd_path)
    if not object.is_manager:
        raise rts_exceptions.NotAManagerError(cmd_path)
    return tree, object
Exemplo n.º 9
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)
Exemplo n.º 10
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()
Exemplo n.º 11
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)
Exemplo n.º 12
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
Exemplo n.º 13
0
def manage_composition(tgt_raw_path, tgt_full_path, options, tree=None):
    # Parse paths of components to add/remove
    add_paths = parse_member_paths(options.add)
    rem_paths = parse_member_paths(options.remove)

    # The target, either a manager or a component
    tgt_path, tgt_suffix = rtctree.path.parse_path(tgt_full_path)

    # Make a tree
    if not tree:
        paths = [tgt_path] + [y for x, y, z in add_paths + rem_paths]
        tree = rtctree.tree.RTCTree(paths=paths, filter=paths)
    tgt_obj = tree.get_node(tgt_path)
    if not tgt_obj:
        raise rts_exceptions.NoSuchObjectError(tgt_raw_path)

    # Input sanity check: ensure all components and ports to be added exist
    add_rtcs = get_comp_objs(add_paths, tree)
    for rtc in add_rtcs:
        for p in add_rtcs[rtc][1]:
            if not add_rtcs[rtc][0].get_port_by_name(p):
                raise rts_exceptions.PortNotFoundError(rtc, p)
    # Ensure all ports to be removed that are on components that are alive
    # exist
    rem_rtcs, rem_zombies = get_potential_comp_objs(rem_paths, tree)
    for rtc in rem_rtcs:
        for p in rem_rtcs[rtc][1]:
            if not rem_rtcs[rtc][0].get_port_by_name(p):
                raise rts_exceptions.PortNotFoundError(rtc, p)

    if tgt_obj.is_manager:
        # Create composition
        if not tgt_suffix:
            tgt_suffix = 'CompositeRTC'
            tgt_raw_path += ':' + tgt_suffix
        # Check if this composition already exists
        comp = tgt_obj.get_node([tgt_obj.name, tgt_suffix + '.rtc'])
        if not comp:
            # Cannot remove components when creating a new composition
            if options.remove:
                raise rts_exceptions.CannotRemoveFromNewCompositionError()
            # No composition exists in this manager; make a new one
            if options.verbose:
                print >> sys.stderr, 'Creating new composition {0}'.format(
                    tgt_raw_path)
            comp = create_composition(tgt_obj, tgt_suffix, options.options,
                                      options.type)
        elif options.verbose:
            print >> sys.stderr, 'Editing existing composition {0}'.format(
                tgt_raw_path)
    elif tgt_obj.is_component:
        # Edit composition - there should be no suffix
        if tgt_suffix:
            raise rts_exceptions.NotAComponentError(tgt_raw_path)
        comp = tgt_obj
        if options.verbose:
            print >> sys.stderr, 'Editing existing composition {0}'.format(
                tgt_raw_path)
    else:
        raise rts_exceptions.NotAComponentOrManagerError(tgt_raw_path)
    if not comp.is_composite:
        raise rts_exceptions.NotACompositeComponentError(tgt_raw_path)

    if add_paths:
        add_to_composition(comp, add_rtcs, tree, options.verbose)
    if rem_paths:
        rem_rtcs.update(rem_zombies)
        rem_from_composition(comp, rem_rtcs, tree, options.verbose)
    if not comp.members[comp.organisations[0].org_id]:
        if options.verbose:
            print >> sys.stderr, 'Composition {0} has no members'.format(
                tgt_raw_path)
Exemplo n.º 14
0
def search(cmd_path, full_path, options, tree=None):
    def get_result(node, args):
        if node.full_path_str.startswith(cmd_path):
            result = node.full_path_str[len(cmd_path):]
            if not result:
                # This will happen if the search root is a component
                return node.name
            return node.full_path_str
        else:
            return node.full_path_str

    def matches_search(node):
        # Filter out types
        if options.type:
            if node.is_component and 'c' not in options.type:
                return False
            if node.is_manager and 'm' not in options.type and \
                    'd' not in options.type:
                return False
            if node.is_nameserver and 'n' not in options.type and \
                    'd' not in options.type:
                return False
            if node.is_directory and 'd' not in options.type and \
                    (not node.is_nameserver and not node.is_manager):
                return False
            if node.is_zombie and 'z' not in options.type:
                return False
        # Filter out depth
        if max_depth > 0 and node.depth > max_depth:
            return False
        if not name_res:
            return True
        # Check for name matches
        for name_re in name_res:
            if name_re.search(node.full_path_str):
                return True
        return False

    path, port = rtctree.path.parse_path(full_path)
    if port:
        raise rts_exceptions.NotADirectoryError(cmd_path)

    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])

    # Find the root node of the search
    root = tree.get_node(path)
    if not root:
        raise rts_exceptions.NoSuchObjectError(cmd_path)
    if root.is_component and trailing_slash:
        raise rts_exceptions.NotADirectoryError(cmd_path)

    if options.max_depth:
        max_depth = options.max_depth + len(path) - 1  # The root is 1-long
    else:
        max_depth = 0

    name_res = []
    for name in options.name:
        # Replace regex special characters
        name = re.escape(name)
        # * goes to .*?
        name = name.replace(r'\*', '.*?')
        # ? goes to .
        name = name.replace(r'\?', r'.')
        name_res.append(re.compile(name))
    for name in options.iname:
        # Replace regex special characters
        name = re.escape(name)
        # * goes to .*?
        name = name.replace(r'\*', '.*?')
        # ? goes to .
        name = name.replace(r'\?', r'.')
        name_res.append(re.compile(name, re.IGNORECASE))

    return root.iterate(get_result, filter=[matches_search])
Exemplo n.º 15
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)