def test_common_path_when_duplicate_entries_of_single_path(self):
     d = PathList()
     files = [
         "/users/joebloggs/tmp/foo.txt", "/users/joebloggs/tmp/foo.txt"
     ]
     d.add(*files)
     self.assertEqual(d.common_path(), Path("/users/joebloggs/tmp/foo.txt"))
 def test_next_reset_after_add(self):
     d = PathList()
     d.add("/file1", "/file2", "/file3")
     next(d)
     next(d)
     d.add("/file4")
     self.assertEqual(next(d), Path("/file1"))
Esempio n. 3
0
    def glob(self):
        """Glob expansion for entries containing globbable characters.

        We don't simply glob every entry since that would remove entries
        that don't yet exist. And we can't just rely on zero glob
        results because it may have been a legitimate zero result if it
        was globbable but matched nothing. So we test for glob
        characters (*|?|[) to determine whether to attempt a glob.

        However, if it looks like a glob, but the glob library can't
        handle it, then we have to assume it really is a filename with 
        glob-like characters, and then we just add the literal path 
        unchanged. See the test: test_ignore_invalid_glob().
        """
        self._deduplicate()
        result = []
        for entry in self._entries:
            pp = entry.fslash()
            if GLOBBABLE_REGEX.search(pp):
                try:
                    globs = glob.glob(entry.fslash())
                    result += globs
                except re.error:
                    result.append(pp)
            else:
                result.append(pp)
        self._entries = [Path(g) for g in result]
        self._clean = False
        self._current = 0
 def test_common_path_when_common_prefix_in_filename(self):
     d = PathList()
     files = [
         "/users/joebloggs/tmp/dissention/perfect",
         "/users/joebloggs/tmp/disagreement/crimson",
         "/users/joebloggs/tmp/diatribe/belew",
     ]
     d.add(*files)
     self.assertEqual(d.common_path(), Path("/users/joebloggs/tmp"))
 def test_common_path(self):
     d = PathList()
     files = [
         "/users/joebloggs/tmp/foobar/test",
         "/users/joebloggs/tmp/baz/fripp",
         "/users/joebloggs/tmp/elephant/corner",
     ]
     d.add(*files)
     self.assertEqual(d.common_path(), Path("/users/joebloggs/tmp"))
 def test_common_path_when_one_path_is_the_common_path(self):
     d = PathList()
     files = [
         "/users/joebloggs/tmp",
         "/users/joebloggs/tmp/bolly/operation",
         "/users/joebloggs/tmp/stay/go",
     ]
     d.add(*files)
     self.assertEqual(d.common_path(), Path("/users/joebloggs/tmp"))
 def test_common_path_when_lowest_path_is_the_common_path(self):
     d = PathList()
     files = [
         "/users/joebloggs/tmp/foo.txt",
         "/users/joebloggs/tmp/modelman.jpg",
         "/users/joebloggs/tmp/ration.cpp",
         "/users/joebloggs/tmp/bill.project",
     ]
     d.add(*files)
     self.assertEqual(d.common_path(), Path("/users/joebloggs/tmp"))
 def test_common_different_drive_letter(self):
     d = PathList()
     files = [
         "D:/users/joebloggs/tmp/foo.txt",
         "D:/users/joebloggs/tmp/modelman.jpg",
         "C:/users/joebloggs/tmp/ration.cpp",
         "C:/users/joebloggs/tmp/bill.project",
     ]
     d.add(*files)
     self.assertEqual(d.common_path(), Path("/"))
Esempio n. 9
0
    def _add_one(self, path):
        """Add a single file.

        Note that when an element is added, it may cause the list to
        change next time it is deduplicated, which includes getting
        shorter. This could happen if a containing directory is added.
        Therefore we have to set the peg position to zero.
        """

        if not type(path).__name__ == "Path":
            path = Path(path)
        self._entries.append(path)
        self._clean = False
        self._current = 0
Esempio n. 10
0
    def common_path(self):
        """Find the common path among entries.

        This is useful for determining output directory when many renders are
        rendering to different places.

        In the case where only single path exists, it is not possible to tell
        from its name whether it is a file or directory. We don't want this
        method to touch the filesystem, that should be someone else's problem. A
        trailing slash would be a hint, but the absence of a trailing slash does
        not mean its a regular file. Therefore, in the case of a single file we
        return it AS-IS and the caller can then stat to find out for sure.

        If no files exist return None.

        If the filesystem root is the common path, return root path, which is
        not entirely correct on windows with drive letters.
        """
        if not self._entries:
            return None

        absolute = list(self._entries)[0].absolute

        def _all_the_same(rhs):
            return all(n == rhs[0] for n in rhs[1:])

        levels = zip(*[p.all_components for p in self._entries])

        common = [x[0] for x in takewhile(_all_the_same, levels)]

        if not len(common):
            return Path("/")
        elif common[0].endswith(":"):
            return Path("/".join(common))
        elif absolute:
            return Path("/" + "/".join(common))
        return Path("/".join(common))
 def test_common_path_is_slash_when_root(self):
     d = PathList()
     files = ["/users/joebloggs/tmp/foo.txt", "/dev/joebloggs/tmp/foo.txt"]
     d.add(*files)
     self.assertEqual(d.common_path(), Path("/"))
Esempio n. 12
0
 def test_is_unc(self):
     p = Path("\\\\a\\b\\c")
     self.assertTrue(p.is_unc)
     p = Path("//a/b/c")
     self.assertTrue(p.is_unc)
Esempio n. 13
0
 def test_unc_root_with_forward(self):
     p = Path("//a/b/c")
     self.assertEqual(p.fslash(with_drive=True), "//a/b/c")
Esempio n. 14
0
 def test_relative_is_not_unc(self):
     p = Path(["a/b/c"])
     self.assertFalse(p.is_unc)
Esempio n. 15
0
 def test_posix_abs_is_not_unc(self):
     p = Path(["/a/b/c"])
     self.assertFalse(p.is_unc)
Esempio n. 16
0
 def test_remove_from_root_path(self):
     self.p = Path("C:\\")
     self.assertEqual(self.p.fslash(with_drive=False), "/")
     self.assertEqual(self.p.bslash(with_drive=False), "\\")
Esempio n. 17
0
 def test_drive_letter_is_not_unc(self):
     p = Path("C:\\aaa\\bbb\\c")
     self.assertFalse(p.is_unc)
 def test_next(self):
     d = PathList()
     d.add("/file1", "/file2", "/file3")
     self.assertEqual(next(d), Path("/file1"))
     self.assertEqual(next(d), Path("/file2"))
Esempio n. 19
0
 def setUp(self):
     self.p = Path("/a/b/c")
Esempio n. 20
0
 def test_remove_from_path(self):
     self.p = Path("C:\\a\\b\\c")
     self.assertEqual(self.p.fslash(with_drive=False), "/a/b/c")
     self.assertEqual(self.p.bslash(with_drive=False), "\\a\\b\\c")
 def test_dedup_same_paths(self):
     d = PathList()
     d.add(Path("/file1"), Path("/file2"), Path("/file2"))
     self.assertEqual(len(d), 2)
     self.assertIn(Path("/file1"), d)
     self.assertIn(Path("/file2"), d)
 def test_adds_mix(self):
     d = PathList()
     d.add("/a/file1", "/a/file2", Path("/a/file3"))
     self.assertEqual(len(d), 3)
 def test_unpacking(self):
     d = PathList()
     d.add(Path("/a/file1"), Path("/a/file2"))
     a, b = d
     self.assertEqual(type(a), Path)
Esempio n. 24
0
 def setUp(self):
     self.p = Path("C:\\a\\b\\c")
Esempio n. 25
0
 def test_path_emits_string_relative(self):
     input_file = "path/to/thefile.jpg"
     p = Path(input_file)
     self.assertEqual(str(p), input_file)
Esempio n. 26
0
 def test_path_emits_string_posix(self):
     input_file = "/path/to/thefile.jpg"
     p = Path(input_file)
     self.assertEqual(str(p), input_file)
 def test_adds_paths(self):
     d = PathList()
     d.add(Path("/a/file1"), Path("/a/file2"))
     self.assertEqual(len(d), 2)
Esempio n. 28
0
 def __contains__(self, key):
     if not isinstance(key, Path):
         key = Path(key)
     return key in self._entries
 def test_removes_path(self):
     d = PathList()
     d.add("/a/file1", "/a/file2", "/a/file3")
     d.remove(Path("/a/file2"))
     self.assertEqual(len(d), 2)
     self.assertEqual(list(d)[1], Path("/a/file3"))
Esempio n. 30
0
 def test_unc_root_without_drive(self):
     p = Path("\\\\a\\b\\c")
     self.assertEqual(p.fslash(with_drive=False), "/a/b/c")