Exemple #1
0
    def testTimestamp(self):
        with temp.AutoTempDirPath(remove_non_empty=True) as dirpath:
            filepath = os.path.join(dirpath, "foo")

            with open(filepath, mode="wb"):
                pass

            with open(filepath, mode="wb") as filedesc:
                filedesc.write(b"quux")

            with open(filepath, mode="rb") as filedesc:
                _ = filedesc.read()

            now_ns = time.time_ns()

            entries = list(timeline.Walk(dirpath.encode("utf-8")))
            self.assertLen(entries, 2)

            self.assertEqual(entries[0].path, dirpath.encode("utf-8"))
            self.assertEqual(entries[1].path, filepath.encode("utf-8"))

            self.assertGreater(entries[1].ctime_ns, 0)
            self.assertGreaterEqual(entries[1].mtime_ns, entries[1].ctime_ns)
            self.assertGreaterEqual(entries[1].atime_ns, entries[1].mtime_ns)
            self.assertLess(entries[1].atime_ns, now_ns)
Exemple #2
0
    def testNestedDirectories(self):
        with temp.AutoTempDirPath(remove_non_empty=True) as root_dirpath:
            foobar_dirpath = os.path.join(root_dirpath, "foo", "bar")
            os.makedirs(foobar_dirpath)

            foobaz_dirpath = os.path.join(root_dirpath, "foo", "baz")
            os.makedirs(foobaz_dirpath)

            quuxnorfthud_dirpath = os.path.join(root_dirpath, "quux", "norf",
                                                "thud")
            os.makedirs(quuxnorfthud_dirpath)

            entries = list(timeline.Walk(root_dirpath.encode("utf-8")))
            self.assertLen(entries, 7)

            paths = [_.path.decode("utf-8") for _ in entries]
            self.assertCountEqual(paths, [
                os.path.join(root_dirpath),
                os.path.join(root_dirpath, "foo"),
                os.path.join(root_dirpath, "foo", "bar"),
                os.path.join(root_dirpath, "foo", "baz"),
                os.path.join(root_dirpath, "quux"),
                os.path.join(root_dirpath, "quux", "norf"),
                os.path.join(root_dirpath, "quux", "norf", "thud"),
            ])

            for entry in entries:
                self.assertTrue(stat_mode.S_ISDIR(entry.mode))
Exemple #3
0
    def testIncorrectPath(self):
        with temp.AutoTempDirPath(remove_non_empty=True) as dirpath:
            not_existing_path = os.path.join(dirpath, "not", "existing",
                                             "path")

            with self.assertRaises(OSError):
                timeline.Walk(not_existing_path.encode("utf-8"))
Exemple #4
0
    def testPathWithTrailingSeparator(self):
        with temp.AutoTempDirPath(remove_non_empty=True) as dirpath:
            seppath = dirpath + os.path.sep

            entries = list(timeline.Walk(seppath.encode("utf-8")))
            self.assertLen(entries, 1)
            self.assertEqual(entries[0].path, dirpath.encode("utf-8"))
Exemple #5
0
  def testPathWithRedundantComponents(self):
    with temp.AutoTempDirPath(remove_non_empty=True) as dirpath:
      os.makedirs(os.path.join(dirpath, "foo", "bar"))
      redpath = os.path.join(dirpath, "foo", ".", "bar", "..", ".", "bar", "..")

      entries = list(timeline.Walk(redpath.encode("utf-8")))
      paths = [entry.path.decode("utf-8") for entry in entries]

      self.assertLen(paths, 2)
      self.assertEqual(paths[0], os.path.join(dirpath, "foo"))
      self.assertEqual(paths[1], os.path.join(dirpath, "foo", "bar"))
Exemple #6
0
    def testSingleFile(self):
        with temp.AutoTempDirPath(remove_non_empty=True) as dirpath:
            filepath = os.path.join(dirpath, "foo")
            _Touch(filepath, content=b"foobar")

            entries = list(timeline.Walk(dirpath.encode("utf-8")))
            self.assertLen(entries, 2)

            self.assertTrue(stat_mode.S_ISDIR(entries[0].mode))
            self.assertEqual(entries[0].path, dirpath.encode("utf-8"))

            self.assertTrue(stat_mode.S_ISREG(entries[1].mode))
            self.assertEqual(entries[1].path, filepath.encode("utf-8"))
            self.assertEqual(entries[1].size, 6)
Exemple #7
0
    def testMultipleFiles(self):
        with temp.AutoTempDirPath(remove_non_empty=True) as dirpath:
            foo_filepath = os.path.join(dirpath, "foo")
            bar_filepath = os.path.join(dirpath, "bar")
            baz_filepath = os.path.join(dirpath, "baz")

            _Touch(foo_filepath)
            _Touch(bar_filepath)
            _Touch(baz_filepath)

            entries = list(timeline.Walk(dirpath.encode("utf-8")))
            self.assertLen(entries, 4)

            paths = [_.path for _ in entries[1:]]
            self.assertIn(foo_filepath.encode("utf-8"), paths)
            self.assertIn(bar_filepath.encode("utf-8"), paths)
            self.assertIn(baz_filepath.encode("utf-8"), paths)
Exemple #8
0
    def testBirthTimestamp(self):
        if platform.system() == "Linux":
            try:
                ctypes.CDLL("libc.so.6").statx
            except AttributeError:
                raise absltest.SkipTest("`statx` not available")

        with temp.AutoTempDirPath(remove_non_empty=True) as dirpath:
            filepath = os.path.join(dirpath, "foo")

            with open(filepath, mode="wb") as filedesc:
                filedesc.write(b"foobar")

            entries = list(timeline.Walk(dirpath.encode("utf-8")))
            self.assertLen(entries, 2)

            self.assertEqual(entries[0].path, dirpath.encode("utf-8"))
            self.assertEqual(entries[1].path, filepath.encode("utf-8"))

            now_ns = time.time_ns()
            self.assertBetween(entries[1].btime_ns, 0, now_ns)
Exemple #9
0
    def testSymlinks(self):
        with temp.AutoTempDirPath(remove_non_empty=True) as root_dirpath:
            sub_dirpath = os.path.join(root_dirpath, "foo", "bar", "baz")
            link_path = os.path.join(sub_dirpath, "quux")

            # This creates a cycle, walker should be able to cope with that.
            os.makedirs(sub_dirpath)
            os.symlink(root_dirpath, os.path.join(sub_dirpath, link_path))

            entries = list(timeline.Walk(root_dirpath.encode("utf-8")))
            self.assertLen(entries, 5)

            paths = [_.path.decode("utf-8") for _ in entries]
            self.assertEqual(paths, [
                os.path.join(root_dirpath),
                os.path.join(root_dirpath, "foo"),
                os.path.join(root_dirpath, "foo", "bar"),
                os.path.join(root_dirpath, "foo", "bar", "baz"),
                os.path.join(root_dirpath, "foo", "bar", "baz", "quux")
            ])

            for entry in entries[:-1]:
                self.assertTrue(stat_mode.S_ISDIR(entry.mode))
            self.assertTrue(stat_mode.S_ISLNK(entries[-1].mode))
Exemple #10
0
    def testIncorrectPath(self):
        not_existing_path = os.path.join("some", "not", "existing", "path")

        entries = list(timeline.Walk(not_existing_path.encode("utf-8")))
        self.assertEmpty(entries)
Exemple #11
0
    def testRelativePath(self):
        relpath = os.path.join("foo", "bar", "baz")

        with self.assertRaises(ValueError):
            timeline.Walk(relpath.encode("utf-8"))