def test_path_expansion(self): paths = [ '/foo/bar', '/foo/{bar,baz}', '/foo/{bar,baz}/{quz,quux}', '/foo/{bar,baz}/{quz,quux}/{aap}' ] expected = [ '/foo/bar', '/foo/bar', '/foo/baz', '/foo/bar/quz', '/foo/bar/quux', '/foo/baz/quz', '/foo/baz/quux', '/foo/bar/quz/aap', '/foo/bar/quux/aap', '/foo/baz/quz/aap', '/foo/baz/quux/aap' ] new_paths = glob.expand_paths(paths) self.assertEqual(new_paths, expected)
def _find_items(self, paths, processor, include_toplevel=False, include_children=False, recurse=False, check_nonexistence=False): ''' Request file info from the NameNode and call the processor on the node(s) returned :param paths: A list of paths that need to be processed :param processor: Method that is called on an node. Method signature should be foo(path, node). For additional (static) params, use a lambda. :param include_toplevel: Boolean to enable the inclusion of the first node found. Example: listing a directory should not include the toplevel, but chmod should only operate on the path that is input, so it should include the toplevel. :param include_children: Include children (when the path is a directory) in processing. Recurse will always include children. Example: listing a directory should include children, but chmod shouldn't. :param recurse: Recurse into children if they are directories. ''' #collection = [] if not paths: paths = [os.path.join("/user", pwd.getpwuid(os.getuid())[0])] # Expand paths if necessary (/foo/{bar,baz} --> ['/foo/bar', '/foo/baz']) paths = glob.expand_paths(paths) for path in paths: if not path.startswith("/"): path = self._join_user_path(path) log.debug("Trying to find path %s" % path) if glob.has_magic(path): log.debug("Dealing with globs in %s" % path) for item in self._glob_find(path, processor, include_toplevel): yield item else: fileinfo = self._get_file_info(path) if not fileinfo and not check_nonexistence: raise FileNotFoundException("`%s': No such file or directory" % path) elif not fileinfo and check_nonexistence: yield processor(path, None) return if (include_toplevel and fileinfo) or not self._is_dir(fileinfo.fs): # Construct the full path before processing full_path = self._get_full_path(path, fileinfo.fs) log.debug("Added %s to to result set" % full_path) entry = processor(full_path, fileinfo.fs) yield entry if self._is_dir(fileinfo.fs) and (include_children or recurse): for node in self._get_dir_listing(path): full_path = self._get_full_path(path, node) last_entry_path = node.path entry = processor(full_path, node) yield entry # Recurse into directories if recurse and self._is_dir(node): # Construct the full path before processing full_path = os.path.join(path, node.path) for item in self._find_items([full_path], processor, include_toplevel=False, include_children=False, recurse=recurse): yield item
def test_path_expansion(self): paths = ['/foo/bar', '/foo/{bar,baz}', '/foo/{bar,baz}/{quz,quux}', '/foo/{bar,baz}/{quz,quux}/{aap}'] expected = ['/foo/bar', '/foo/bar', '/foo/baz', '/foo/bar/quz', '/foo/bar/quux', '/foo/baz/quz', '/foo/baz/quux', '/foo/bar/quz/aap', '/foo/bar/quux/aap', '/foo/baz/quz/aap', '/foo/baz/quux/aap'] new_paths = glob.expand_paths(paths) self.assertEqual(new_paths, expected)