def cd(cmd_path, full_path): path, port = parse_path(full_path) if port: # Can't change dir to a port print >> sys.stderr, "rtcd: {0}: Not a \ directory".format( cmd_path ) return 1 if not path[-1]: # Remove trailing slash part path = path[:-1] tree = create_rtctree(paths=path) if not tree: return 1 if not tree.has_path(path): print >> sys.stderr, "rtcd: {0}: No such directory or \ object".format( cmd_path ) return 1 if not tree.is_directory(path): print >> sys.stderr, "rtcd: {0}: Not a directory".format(cmd_path) return 1 print make_cmd_line(full_path) return 0
def get_manager(cmd_path, full_path, tree=None): path, port = parse_path(full_path) if port: # Can't configure a port print >>sys.stderr, '{0}: Cannot access {1}: No such \ object.'.format(sys.argv[0], cmd_path) return None if not path[-1]: # There was a trailing slash - ignore it path = path[:-1] if not tree: tree = create_rtctree(paths=path) if not tree: return None object = tree.get_node(path) if not object: print >>sys.stderr, '{0}: Cannot access {1}: No such \ object.'.format(sys.argv[0], cmd_path) return tree, None if not object.is_manager: print >>sys.stderr, '{0}: Cannot access {1}: Not a \ manager.'.format(sys.argv[0], cmd_path) return tree, None return tree, object
def get_comp_obj(cmd_path, path, options, tree=None, orb=None): if not tree: tree = create_rtctree(paths=path, orb=orb) if not tree: return 1, (None, None) if not tree.has_path(path): print >>sys.stderr, '{0}: Cannot access {1}: No such \ object.'.format(sys.argv[0], cmd_path) return 1, (None, None) comp = tree.get_node(path) if not comp or not comp.is_component: print >>sys.stderr, '{0}: Cannot access {1}: No such \ object'.format(sys.argv[0], cmd_path) return 1, None return 0, (tree, comp)
def get_our_listener(options, tree=None, orb=None): if not tree: tree = create_rtctree(paths=['/', 'localhost'], orb=orb) if not tree: return 1, None listener_name = listener_reg_name(index) def get_result(node, args): return node def is_our_listener(node): if node.name == listener_name: return True matches = tree.iterate(get_result, filter=['is_component', is_our_listener]) if not matches or len(matches) != 1: print >>sys.stderr, '{0}: Could not find listener component.'.format(\ sys.argv[0]) return 1, None return 0, matches[0]
def select_index(path, tree=None): if not tree: tree = create_rtctree(paths=path) if not tree: return 1, 0 listener_re = re.compile('{0}(\d+)0.rtc'.format(listener_name_base())) def get_result(node, args): return int(listener_re.match(node.name).group(1)) def is_listener(node): if listener_re.match(node.name): return True matches = tree.iterate(get_result, filter=['is_component', is_listener]) if not matches: # No existing listeners, so claim the first spot return 0, 0 matches.sort () return 0, matches[-1] + 1
def delete_object_reference(cmd_path, full_path, options, tree=None): path, port = parse_path(full_path) if port: # Can't delete a port print >>sys.stderr, '{0}: Cannot access {1}: Cannot delete \ ports.'.format(sys.argv[0], cmd_path) return 1 trailing_slash = False if not path[-1]: path = path[:-1] # Cannot delete name servers if len(path) == 2: print >>sys.stderr, '{0}: {1}: Cannot delete name servers.'.format(\ sys.argv[0], cmd_path) return 1 if not tree: tree = create_rtctree(paths=path) if not tree: return 1 # There is no point in doing path checks for the path, as the path we are # deleting may not be in the tree if it's a zombie. Instead, we need to # find its parent, and use that to remove the name. parent = tree.get_node(path[:-1]) if parent.is_manager: print >>sys.stderr, '{0}: {1}: Use rtmgr to delete components from \ managers.'.format(sys.argv[0], cmd_path) return 1 if not parent.is_directory: print >>sys.stderr, '{0}: {1}: Parent is not a directory.'.format(\ sys.argv[0], cmd_path) return 1 try: parent.unbind(path[-1]) except BadPathError: print >>sys.stderr, '{0}: {1}: No such name registered.'.format(\ sys.argv[0], cmd_path) return 1 return 0
def cat_target(cmd_path, full_path, options, tree=None): use_colour = sys.stdout.isatty() path, port = parse_path(full_path) if port: # Can't cat a port print >>sys.stderr, '{0}: Cannot access {1}: No such \ object.'.format(sys.argv[0], cmd_path) return 1 trailing_slash = False if not path[-1]: # There was a trailing slash print >>sys.stderr, '{0}: {1}: Not an \ object'.format(sys.argv[0], cmd_path) return 1 if not tree: tree = create_rtctree(paths=path) if not tree: return tree if not tree.has_path(path): print >>sys.stderr, '{0}: Cannot access {1}: No such \ object.'.format(sys.argv[0], cmd_path) return 1 object = tree.get_node(path) if object.is_component: for l in format_component(object, use_colour=sys.stdout.isatty(), long=options.long, really_long=options.really_long): print l elif object.is_manager: for l in format_manager(object, use_colour=sys.stdout.isatty(), long=options.long): print l else: print >>sys.stderr, '{0}: Cannot access {1}: No such \ object.'.format(sys.argv[0], cmd_path) return 1 return 0
def set_conf_value(set, param, new_value, cmd_path, full_path, options, tree=None): path, port = parse_path(full_path) if port: # Can't configure a port print >>sys.stderr, '{0}: Cannot access {1}: No such \ object.'.format(sys.argv[0], cmd_path) return 1 trailing_slash = False if not path[-1]: # There was a trailing slash print >>sys.stderr, '{0}: {1}: Not an \ object'.format(sys.argv[0], cmd_path) return 1 if not tree: tree = create_rtctree(paths=path) if not tree: return 1 object = tree.get_node(path) if not object: print >>sys.stderr, '{0}: Cannot access {1}: No such \ object.'.format(sys.argv[0], cmd_path) return 1 if not object.is_component: print >>sys.stderr, '{0}: Cannot access {1}: Not a \ component.'.format(sys.argv[0], cmd_path) return 1 if not set: set = object.active_conf_set_name try: object.set_conf_set_value(set, param, new_value) except NoSuchConfSetError, e: print >>sys.stderr, '{0}: {1}: No such configuration \ set'.format(sys.argv[0], e) return 1
def disconnect_all(cmd_path, full_path, options, tree=None): path, port = parse_path(full_path) if not path[-1]: # Trailing slashes are bad print >>sys.stderr, '{0}: Not a directory: {1}'.format(sys.argv[0], cmd_path) return 1 if not tree: tree = create_rtctree(paths=path) if not tree: return 1 object = tree.get_node(path) if not object: print >>sys.stderr, '{0}: Cannot access {1}: No such \ object.'.format(sys.argv[0], cmd_path) return 1 if not object.is_component: print >>sys.stderr, '{0}: Cannot access {1}: No such \ object'.format(sys.argv[0], cmd_path) return 1 if port: # Disconnect a single port port_obj = object.get_port_by_name(port) if not port_obj: print >>sys.stderr, '{0}: Cannot access {1}: No such \ port'.format(sys.argv[0], cmd_path) return 1 port_obj.disconnect_all() else: # Disconnect all ports object.disconnect_all() pass return 0
def print_conf_sets(cmd_path, full_path, options, tree=None): use_colour = sys.stdout.isatty() path, port = parse_path(full_path) if port: # Can't configure a port print >>sys.stderr, '{0}: Cannot access {1}: No such \ object.'.format(sys.argv[0], cmd_path) return 1 trailing_slash = False if not path[-1]: # There was a trailing slash print >>sys.stderr, '{0}: {1}: Not an \ object'.format(sys.argv[0], cmd_path) return 1 if not tree: tree = create_rtctree(paths=path) if not tree: return 1 object = tree.get_node(path) if not object: print >>sys.stderr, '{0}: Cannot access {1}: No such \ object.'.format(sys.argv[0], cmd_path) return 1 if not object.is_component: print >>sys.stderr, '{0}: Cannot access {1}: Not a \ component.'.format(sys.argv[0], cmd_path) return 1 for l in format_conf_sets(object.conf_sets, object.active_conf_set_name, use_colour, options.long): print l return 0
def alter_component_state(action, cmd_path, full_path, options, tree=None): path, port = parse_path(full_path) if port: # Can't cat a port print >>sys.stderr, '{0}: Cannot access {1}: No such \ object.'.format(sys.argv[0], cmd_path) return 1 trailing_slash = False if not path[-1]: # There was a trailing slash print >>sys.stderr, '{0}: {1}: Not an \ object'.format(sys.argv[0], cmd_path) return 1 if not tree: tree = create_rtctree(paths=path) if not tree: return 1 if not tree.has_path(path): print >>sys.stderr, '{0}: Cannot access {1}: No such \ object.'.format(sys.argv[0], cmd_path) return 1 object = tree.get_node(path) if not object.is_component: print >>sys.stderr, '{0}: Cannot access {1}: Not an \ object'.format(sys.argv[0], cmd_path) return 1 try: action(object, options.ec_index) except BadECIndexError, e: print >>sys.stderr, '{0}: No execution context at index \ {1}'.format(sys.argv[0], e.args[0]) return 1
def connect_ports(source_cmd_path, source_full_path, dest_cmd_path, dest_full_path, options, tree=None): source_path, source_port = parse_path(source_full_path) if not source_port: # Need a port to connect to print >>sys.stderr, '{0}: No source port specified'.format(sys.argv[0]) return 1 if not source_path[-1]: # Trailing slashes are bad print >>sys.stderr, '{0}: Bad source path'.format(sys.argv[0]) return 1 dest_path, dest_port = parse_path(dest_full_path) if not dest_port: # Need a port to connect to print >>sys.stderr, '{0}: No destination port \ specified'.format(sys.argv[0]) return 1 if not dest_path[-1]: # Trailing slashes are bad print >>sys.stderr, '{0}: Bad destination path'.format(sys.argv[0]) return 1 if not tree: tree = create_rtctree(paths=[source_path, dest_path]) if not tree: return 1 if not tree.has_path(source_path): print >>sys.stderr, '{0}: Cannot access {1}: No such \ object.'.format(sys.argv[0], source_cmd_path) return 1 if not tree.has_path(dest_path): print >>sys.stderr, '{0}: Cannot access {1}: No such \ object.'.format(sys.argv[0], dest_cmd_path) return 1 source_comp = tree.get_node(source_path) if not source_comp or not source_comp.is_component: print >>sys.stderr, '{0}: Cannot access {1}: No such \ object'.format(sys.argv[0], source_cmd_path) return 1 source_port_obj = source_comp.get_port_by_name(source_port) if not source_port_obj: print >>sys.stderr, '{0}: Cannot access {1}: No such \ port'.format(sys.argv[0], source_cmd_path) return 1 dest_comp = tree.get_node(dest_path) if not dest_comp or not dest_comp.is_component: print >>sys.stderr, '{0}: Cannot access {1}: No such \ object'.format(sys.argv[0], dest_cmd_path) return 1 dest_port_obj = dest_comp.get_port_by_name(dest_port) if not dest_port_obj: print >>sys.stderr, '{0}: Cannot access {1}: No such \ port'.format(sys.argv[0], dest_cmd_path) return 1 conn_name = options.name if options.name else None try: source_port_obj.connect(dest_port_obj, name=conn_name, id=options.id, props=options.properties) except IncompatibleDataPortConnectionPropsError: print >>sys.stderr, '{0}: An incompatible data port property or \ property value was given.'.format(sys.argv[0]) return 1 except WrongPortTypeError: print >>sys.stderr, '{0}: Mismatched port types.'.format(sys.argv[0]) return 1 except MismatchedPolarityError: print >>sys.stderr, '{0}: Service port polarities do not \ match.'.format(sys.argv[0]) return 1 except MismatchedInterfacesError: print >>sys.stderr, '{0}: Service port interfaces do not \ match.'.format(sys.argv[0]) return 1 except FailedToConnectError: print >>sys.stderr, '{0}: Failed to connect.'.format(sys.argv[0]) return 1 return 0
def disconnect_ports(source_cmd_path, source_full_path, dest_cmd_path, dest_full_path, options, tree=None): source_path, source_port = parse_path(source_full_path) if not source_port: # Need a port to disconnect print >>sys.stderr, '{0}: No source port specified'.format(sys.argv[0]) return 1 if not source_path[-1]: # Trailing slashes are bad print >>sys.stderr, '{0}: Bad source path'.format(sys.argv[0]) return 1 dest_path, dest_port = parse_path(dest_full_path) if not dest_port: # Need a port to disconnect print >>sys.stderr, '{0}: No destination port \ specified'.format(sys.argv[0]) return 1 if not dest_path[-1]: # Trailing slashes are bad print >>sys.stderr, '{0}: Bad destination path'.format(sys.argv[0]) return 1 if not tree: tree = create_rtctree(paths=[source_path, dest_path]) if not tree: return 1 source_comp = tree.get_node(source_path) if not source_comp: print >>sys.stderr, '{0}: Cannot access {1}: No such \ object.'.format(sys.argv[0], source_cmd_path) return 1 if not source_comp.is_component: print >>sys.stderr, '{0}: Cannot access {1}: No such \ object'.format(sys.argv[0], source_cmd_path) return 1 source_port_obj = source_comp.get_port_by_name(source_port) if not source_port_obj: print >>sys.stderr, '{0}: Cannot access {1}: No such \ port'.format(sys.argv[0], source_cmd_path) return 1 dest_comp = tree.get_node(dest_path) if not dest_comp: print >>sys.stderr, '{0}: Cannot access {1}: No such \ object.'.format(sys.argv[0], dest_cmd_path) return 1 if not dest_comp.is_component: print >>sys.stderr, '{0}: Cannot access {1}: No such \ object'.format(sys.argv[0], dest_cmd_path) return 1 dest_port_obj = dest_comp.get_port_by_name(dest_port) if not dest_port_obj: print >>sys.stderr, '{0}: Cannot access {1}: No such \ port'.format(sys.argv[0], dest_cmd_path) return 1 conn = source_port_obj.get_connection_by_dest(dest_port_obj) if not conn: print >>sys.stderr, '{0}: No connection between {1} and \ {2}'.format(sys.argv[0], source_cmd_path, dest_cmd_path) return 1 conn.disconnect() return 0
def search(cmd_path, full_path, options, tree=None, returnvalue=None): path, port = parse_path(full_path) if port: # Can't search in a port print >>sys.stderr, '{0}: Cannot access {1}: No such directory or \ object.'.format(sys.argv[0], cmd_path) if returnvalue == 'list': return None else: return 1 trailing_slash = False if not path[-1]: # There was a trailing slash trailing_slash = True path = path[:-1] if not tree: tree = create_rtctree(paths=path) if not tree: if returnvalue == 'list': return None else : return 1 # Find the root node of the search root = tree.get_node(path) if not root: print >>sys.stderr, '{0}: Cannot access {1}: No such directory or \ object.'.format(sys.argv[0], cmd_path) if returnvalue == 'list': return None else : return 1 if root.is_component and trailing_slash: # If there was a trailing slash, complain that a component is not a # directory. print >>sys.stderr, '{0}: cannot access {1}: Not a directory.'.format(\ sys.argv[0], address) if returnvalue == 'list': return None else : return 1 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)) def get_result(node, args): if node.full_path.startswith(cmd_path): result = node.full_path[len(cmd_path):] if not result: # This will happen if the search root is a component return node.name return node.full_path else: return node.full_path def matches_search(node): # Filter out types 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 not name_res: return True # Check for name matches for name_re in name_res: if name_re.search(node.full_path): return True return False matches = root.iterate(get_result, filter=[matches_search]) if returnvalue == 'list': return matches else : for m in matches: print m return 0
def list_target(cmd_path, full_path, options, tree=None): path, port = parse_path(full_path) if port: # Can't list a port print >>sys.stderr, '{0}: Cannot access {1}: No such directory or \ object.'.format(sys.argv[0], cmd_path) return 1 trailing_slash = False if not path[-1]: # There was a trailing slash trailing_slash = True path = path[:-1] if not tree: tree = create_rtctree(paths=path) if not tree: return 1 if not tree.has_path(path): print >>sys.stderr, '{0}: Cannot access {1}: No such directory or \ object.'.format(sys.argv[0], cmd_path) return 1 if tree.is_component(path): # Path points to a single component: print it like 'ls <file>'. if trailing_slash: # If there was a trailing slash, complain that a component is not a # directory. print >>sys.stderr, '{0}: cannot access {1}: Not a \ directory.'.format(sys.argv[0], address) return 1 if options.long: lines = get_node_long_lines([tree.get_node(path)], sys.stdout.isatty()) for l in lines: print l else: print path[-1] 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 def get_name(node, args): if node.full_path.startswith(recurse_root_path): result = node.full_path[len(recurse_root_path):] else: result = node.full_path 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']) for dir, listing in zip(dir_names, listings): if dir == '.': print '.:' else: print './' + dir + ':' for l in listing: print l print else: dir_node = tree.get_node(path) lines = list_directory(dir_node, options.long) for l in lines: print l else: print >>sys.stderr, '{0}: cannot access {1}: Unknown object \ type.'.format(sys.argv[0], cmd_path) return 1 return 0
def inject_data(cmd_path, full_path, options, data, tree): path, port = parse_path(full_path) if not port: # Need a port to inject to print >>sys.stderr, '{0}: Cannot access {1}: Not a port'.format(\ sys.argv[0], cmd_path) return 1 trailing_slash = False if not path[-1]: # There was a trailing slash print >>sys.stderr, '{0}: {1}: Not a port'.format(\ sys.argv[0], cmd_path) return 1 if not tree: tree = create_rtctree(paths=path) if not tree: return tree comp = tree.get_node(path) if not comp or not comp.is_component: print >>sys.stderr, '{0}: Cannot access {1}: No such \ component.'.format(sys.argv[0], cmd_path) return 1 port = comp.get_port_by_name(port) if not port: print >>sys.stderr, '{0}: Cannot access {1}: No such \ port'.format(sys.argv[0], cmd_path) return 1 if not port.porttype == 'DataInPort': print >>sys.stderr, '{0}: Can only inject to DataInPort port \ type.'.format(sys.argv[0]) return 1 # Create a dummy connector on the port datatype = str(data.__class__).rpartition('.')[2] props = [] props.append(SDOPackage.NameValue('dataport.dataflow_type', any.to_any('push'))) props.append(SDOPackage.NameValue('dataport.interface_type', any.to_any('corba_cdr'))) props.append(SDOPackage.NameValue('dataport.subscription_type', any.to_any('flush'))) props.append(SDOPackage.NameValue('dataport.data_type', any.to_any(datatype))) profile = RTC.ConnectorProfile(path[-1] + '.inject_conn', path[-1] + '.temp', [port.object], props) return_code, profile = port.object.connect(profile) port.reparse_connections() if return_code != RTC.RTC_OK: print >>sys.stderr, '{0}: Failed to create local connection. Check \ your data type matches the port.'.format(sys.argv[0]) return 1 # Get the connection's IOR and narrow it to an InPortCdr object conn = port.get_connection_by_name(path[-1] + '.inject_conn') ior = conn.properties['dataport.corba_cdr.inport_ior'] object = comp.orb.string_to_object(ior) if CORBA.is_nil(object): print >>sys.stderr, '{0}: Failed to get inport object.'.format(\ sys.argv[0]) return 1 object = object._narrow(InPortCdr) # Inject the data cdr = cdrMarshal(any.to_any(data).typecode(), data, True) if object.put(cdr) != PORT_OK: print >>sys.stderr, '{0}: Failed to inject data.'.format(sys.argv[0]) return 1 # Remove the dummy connection conn.disconnect() return 0