Ejemplo n.º 1
0
    def testTarFileWithMultipleFiles(self):
        outfd = io.BytesIO()

        infd1 = io.BytesIO(b"this is a test string")
        st1 = os.stat_result(
            (0o644, 0, 0, 0, 0, 0, len(infd1.getvalue()), 0, 0, 0))

        infd2 = io.BytesIO(b"this is another test string")
        st2 = os.stat_result(
            (0o644, 0, 0, 0, 0, 0, len(infd2.getvalue()), 0, 0, 0))

        # Write the tar into a file like object.
        with utils.StreamingTarWriter(outfd, mode="w:gz") as writer:
            writer.WriteFromFD(infd1, "test1.txt", st=st1)
            writer.WriteFromFD(infd2, "subdir/test2.txt", st=st2)

        test_tar = tarfile.open(fileobj=io.BytesIO(outfd.getvalue()), mode="r")
        tinfos = sorted(test_tar.getmembers(), key=lambda tinfo: tinfo.name)

        self.assertEqual(len(tinfos), 2)
        self.assertEqual(tinfos[0].name, "subdir/test2.txt")
        self.assertEqual(tinfos[1].name, "test1.txt")

        fd = test_tar.extractfile(tinfos[0])
        self.assertEqual(fd.read(1024), infd2.getvalue())

        fd = test_tar.extractfile(tinfos[1])
        self.assertEqual(fd.read(1024), infd1.getvalue())
Ejemplo n.º 2
0
    def testTarFileWithSymlink(self):
        outfd = io.BytesIO()

        infd1 = io.BytesIO(b"this is a test string")
        st1 = os.stat_result(
            (0o644, 0, 0, 0, 0, 0, len(infd1.getvalue()), 0, 0, 0))

        infd2 = io.BytesIO(b"this is another test string")
        st2 = os.stat_result(
            (0o644, 0, 0, 0, 0, 0, len(infd2.getvalue()), 0, 0, 0))

        # Write the zip into a file like object.
        with utils.StreamingTarWriter(outfd, mode="w:gz") as writer:
            writer.WriteFromFD(infd1, "test1.txt", st=st1)
            writer.WriteFromFD(infd2, "subdir/test2.txt", st=st2)

            writer.WriteSymlink("test1.txt", "test1.txt.link")
            writer.WriteSymlink("subdir/test2.txt", "test2.txt.link")

        with tarfile.open(fileobj=io.BytesIO(outfd.getvalue()),
                          mode="r") as test_fd:
            test_fd.extractall(self.temp_dir)

            link_path = os.path.join(self.temp_dir, "test1.txt.link")
            self.assertTrue(os.path.islink(link_path))
            self.assertEqual(os.readlink(link_path), "test1.txt")

            link_path = os.path.join(self.temp_dir, "test2.txt.link")
            self.assertTrue(os.path.islink(link_path))
            self.assertEqual(os.readlink(link_path), "subdir/test2.txt")
Ejemplo n.º 3
0
  def testTarFileWithOneFile(self):
    infd = io.BytesIO(b"this is a test string")
    st = os.stat_result((0o644, 0, 0, 0, 0, 0, len(infd.getvalue()), 0, 0, 0))

    # Write the tar into a file like object.
    outfd = io.BytesIO()
    with utils.StreamingTarWriter(outfd, mode="w:gz") as writer:
      writer.WriteFromFD(infd, "test.txt", st=st)

    test_tar = tarfile.open(fileobj=io.BytesIO(outfd.getvalue()), mode="r")
    tinfos = list(test_tar.getmembers())

    self.assertEqual(len(tinfos), 1)
    self.assertEqual(tinfos[0].name, "test.txt")

    fd = test_tar.extractfile(tinfos[0])
    self.assertEqual(fd.read(1024), infd.getvalue())