def rename_all(rootpath, top, path, name): """Rename all links to the file or directory. Attempt to rename all links to the target under the rootpath to the given name, finding a name as necessary. If there are multiple links in a directory, the first will be renamed and the rest unlinked. Args: rootpath: Base path for tag conversions. top: Path of search directory. path: Path to target. name: New filename. """ target = path newname = name seen = set() for filepath in base.list_links(top, target): dirname = posixpath.dirname(filepath) if dirname in seen: base.unlink(rootpath, filepath) continue # pylint: disable=cell-var-from-loop pathlib.free_name_do(dirname, newname, lambda dst: base.rename(rootpath, filepath, dst)) seen.add(dirname)
def unlink(args): rootpath = _tag_convert(args, 'files') for file in args.files: try: base.unlink(rootpath, file) except OSError as err: _LOGGER.error(err)
def untag(rootpath, path, directory): """Untag file from a directory. Args: rootpath: Rootpath to resolve tagnames. path: Path of file or directory to tag. directory: Directory path. """ target = path to_unlink = [] for filepath in pathlib.listdirpaths(directory): if posixpath.samefile(target, filepath): to_unlink.append(filepath) for filepath in to_unlink: base.unlink(rootpath, filepath)
def unlink_all(rootpath, top, path): """Unlink all links to the target file-or-directory. Unlink all links to the target under top. Args: rootpath: Base path for tag conversions and search. top: Path of search directory. path: Path to target. """ target = path if posixpath.isdir(target): target = pathlib.readlink(target) base.unload_dtags(rootpath, target) shutil.rmtree(target) else: for path in base.list_links(top, target): base.unlink(rootpath, path)
def test_unlink_dir(self): with patch('dantalian.dtags.remove_tag', autospec=True) as mock_func: base.unlink(self.root, 'bag/apple') self.assertFalse(posixpath.exists('bag/apple')) mock_func.assert_called_with('bag/apple', '//bag/apple')
def test_unlink(self): base.unlink(self.root, 'bag/apple') self.assertFalse(posixpath.exists('bag/apple'))