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