示例#1
0
文件: entry.py 项目: klmitch/fstree
    def symlink(self, src, dst=os.curdir, outside=False):
        """
        Create a symlink to the designated source.

        :param src: The source file the symlink should point to.  If
                    ``outside`` is ``False``, the symlink must point
                    inside the tree, or a ``ValueError`` will be
                    raised.
        :param dst: The destination of the symlink.  If it is a
                    directory, the basename of the source file will be
                    added.
        :param outside: If ``True``, the source filename is not
                        interpreted (unless ``dst`` is a directory).
                        If ``False`` (the default), the source
                        filename must be inside the tree.

        :returns: An ``FSEntry`` instance representing the new
                  symlink.
        """

        # Determine the destination
        dst = self._rel(dst)
        full_dst = self.tree._full(dst)

        # If the destination is a directory, we need to add the source
        # basename
        if os.path.isdir(full_dst):
            basename = os.path.basename(src)
            dst = os.path.join(dst, basename)
            full_dst = os.path.join(full_dst, basename)

        # Figure out the source
        if not outside:
            # Resolve to a tree path
            if isinstance(src, FSEntry):
                src = utils.abspath(src.name, cwd=self.name)
            else:
                src = utils.deroot(utils.abspath(src, cwd=self.path),
                                   self.tree.path)

            # We now have a tree-relative path, convert it into a path
            # relative to the destination
            src = str(utils.RelPath(src, dst))

        # Create the symlink
        os.symlink(src, full_dst)

        # Return a reference to the new file
        return self.tree._get(dst)
示例#2
0
    def test_normal(self):
        result = utils.deroot('/foo/bar/baz/quux', '/foo/bar')

        self.assertEqual(result, '/baz/quux')
示例#3
0
    def test_exact(self):
        result = utils.deroot('/foo/bar', '/foo/bar')

        self.assertEqual(result, '/')
示例#4
0
    def test_slash(self):
        result = utils.deroot('/foo/bar')

        self.assertEqual(result, '/foo/bar')