Esempio n. 1
0
def swap_dir(rootpath, path):
    """Swap a symlink with its target directory.

    Args:
        rootpath: Rootpath for tag conversions.
        path: Path of target symlink.

    """
    target = path
    if posixpath.islink(target) and posixpath.isdir(target):
        here = target
        there = pathlib.readlink(target)
        # here is the symlink
        # there is the dir
        here_tag = tagnames.path2tag(rootpath, here)
        there_tag = tagnames.path2tag(rootpath, there)
        dtags.remove_tag(here, here_tag)
        dtags.add_tag(here, there_tag)
        os.unlink(here)
        # here is now nothing
        # there is now the dir
        os.rename(there, here)
        # here is now the dir
        # there is now nothing
        os.symlink(here, there)
    else:
        raise ValueError('{} is not a symlink to a directory'.format(target))
Esempio n. 2
0
def save_dtags(rootpath, top, dirpath):
    """Save symlinks to a directory's dtags, overwriting it.

    Args:
        rootpath: Path for tag conversions.
        top: Path of directory in which to search.
        dirpath: Path of directory whose dtags to update.

    """
    dirpath = pathlib.readlink(dirpath)
    tags = [tagnames.path2tag(rootpath, path)
            for path in list_links(top, dirpath)]
    dir_tagname = tagnames.path2tag(rootpath, dirpath)
    tags = [tagname
            for tagname in tags
            if tagname != dir_tagname]
    dtags.set_tags(dirpath, tags)
Esempio n. 3
0
def link(rootpath, src, dst):
    """Link src to dst.

    Args:
        rootpath: Path for tagname conversions.
        src: Source path.
        dst: Destination path.

    """
    if posixpath.isdir(src):
        src = pathlib.readlink(src)
        os.symlink(posixpath.abspath(src), dst)
        dtags.add_tag(src, tagnames.path2tag(rootpath, dst))
    else:
        os.link(src, dst)
Esempio n. 4
0
def _export_stat_map(rootpath, top):
    """Export a map of stat objects to sets of tags.

    Args:
        rootpath: Base path for tag conversions.
        top: Top of directory tree to export.
    Returns:
        Dictionary mapping stat objects to sets of tagnames.

    """
    stat_tag_map = defaultdict(set)
    for dirpath, dirnames, filenames in os.walk(top):
        for filename in chain(dirnames, filenames):
            path = posixpath.join(dirpath, filename)
            stat = os.stat(path)
            tagname = tagnames.path2tag(rootpath, path)
            stat_tag_map[stat].add(tagname)
    return stat_tag_map
Esempio n. 5
0
def _export_stat_map(rootpath, top):
    """Export a map of stat objects to sets of tags.

    Args:
        rootpath: Base path for tag conversions.
        top: Top of directory tree to export.
    Returns:
        Dictionary mapping stat objects to sets of tagnames.

    """
    stat_tag_map = defaultdict(set)
    for dirpath, dirnames, filenames in os.walk(top):
        for filename in chain(dirnames, filenames):
            path = posixpath.join(dirpath, filename)
            stat = os.stat(path)
            tagname = tagnames.path2tag(rootpath, path)
            stat_tag_map[stat].add(tagname)
    return stat_tag_map
Esempio n. 6
0
def unlink(rootpath, path):
    """Unlink given path.

    If the target is a directory without any other links, raise OSError.

    """
    target = path
    # We unlink the target.  However, if it is a directory, we want to swap it
    # out for one of its symlinks, then unlink the symlink.  If the directory
    # doesn't have any tags, then we fail.
    if posixpath.isdir(target):
        if not posixpath.islink(target):
            tags = dtags.list_tags(target)
            if not tags:
                raise oserrors.is_a_directory(target)
            swap_candidate = tagnames.tag2path(rootpath, tags[0])
            swap_dir(rootpath, swap_candidate)
            assert posixpath.islink(target)
        dtags.remove_tag(target, tagnames.path2tag(rootpath, target))
    os.unlink(target)
Esempio n. 7
0
 def test_path2tag(self):
     self.assertEqual(tagnames.path2tag('/foo', '/foo/bar'), '//bar')
Esempio n. 8
0
 def test_path2tag(self):
     self.assertEqual(tagnames.path2tag('/foo', '/foo/bar'), '//bar')