Exemplo n.º 1
0
    def test_copy(self):
        src_path = os.path.join(test_files, "test_file_01.aaf")
        dst_path = os.path.join(test_dir, "test_copy.aaf")

        with io.open(src_path, 'rb') as file_a:
            ss_a = CompoundFileBinary(file_a, 'rb')
            with io.open(dst_path, 'wb+') as file_b:
                ss_b = CompoundFileBinary(file_b, 'wb+')

                # copy everything
                for root, storage, streams in ss_a.walk():
                    for item in storage:
                        entry = ss_b.makedir(item.path(), class_id=item.class_id)

                    for item in streams:
                        s_a = ss_a.open(item.path(), 'r')
                        s_b = ss_b.open(item.path(), 'w')
                        s_b.write(s_a.read())

                # check everything exists while file live
                for root, storage, streams in ss_a.walk():

                    assert ss_b.exists(root.path())
                    for item in storage:
                        assert ss_b.exists(item.path())
                        s_b = ss_b.find(item.path())
                        assert s_b.class_id == item.class_id

                    for item in streams:
                        s_a = ss_a.open(item.path(), 'r')
                        s_b = ss_b.open(item.path(), 'r')
                        assert s_a.read() == s_b.read()

                ss_b.close()

            # reopen file and check everything exists
            with io.open(dst_path, 'rb') as f:
                ss_b = CompoundFileBinary(f, 'rb')
                # check everything exists
                for root, storage, streams in ss_a.walk():

                    assert ss_b.exists(root.path())
                    for item in storage:
                        assert ss_b.exists(item.path())
                        s_b = ss_b.find(item.path())
                        assert s_b.class_id == item.class_id

                    for item in streams:
                        s_a = ss_a.open(item.path(), 'r')
                        s_b = ss_b.open(item.path(), 'r')
                        assert s_a.read() == s_b.read()
Exemplo n.º 2
0
    def test_move2(self):
        src_file = common.test_file_01()
        test_file = os.path.join(test_dir, "move2_test.aaf")
        shutil.copy(src_file, test_file)

        dir_list = []
        stream_list = []
        move_root = "/Header-2/Content-3b03"

        with open(test_file, 'rb+') as f:
            cfb = CompoundFileBinary(f, 'rb+')
            for root, storage_items, streams in cfb.walk(move_root):

                for item in storage_items:
                    dir_list.append(item.path())
                for item in streams:
                    stream_list.append(item.path())

            cfb.makedir('/tmp')
            cfb.move(move_root, '/tmp/')

            for item in dir_list:
                assert not cfb.exists(item)
                new_path = item.replace(move_root, '/tmp/Content-3b03')
                assert cfb.exists(new_path)
                assert cfb.find(new_path).isdir()

            for item in stream_list:
                assert not cfb.exists(item)
                new_path = item.replace(move_root, '/tmp/Content-3b03')
                assert cfb.exists(new_path)
                assert cfb.find(new_path).isfile()

            cfb.close()

        with open(test_file, 'rb') as f:
            cfb = CompoundFileBinary(f, 'rb')
            for item in dir_list:
                assert not cfb.exists(item)
                new_path = item.replace(move_root, '/tmp/Content-3b03')
                assert cfb.exists(new_path)
                assert cfb.find(new_path).isdir()

            for item in stream_list:
                assert not cfb.exists(item)
                new_path = item.replace(move_root, '/tmp/Content-3b03')
                assert cfb.exists(new_path)
                assert cfb.find(new_path).isfile()
Exemplo n.º 3
0
    def test_move(self):
        path = os.path.join(test_dir, "move_test.aaf")

        src = "/path/to/item"
        dst = "/dest/path/moved_item"

        stream_data = b'move stream data'

        steam_list = [
            "/path/to/item/child1/sub/sub/stream", "/path/to/item/stream",
            "/path/to/item2/stream"
        ]

        dir_list = [
            "/path/to/item/child1/sub/sub",
            "/path/to/item/child2",
            "/path/to/item/child3/more_stuff",
            "/path/to/item1/child1",
            "/path/to/item2/child2/sub/sub",
            "/path/to/item2/child2",
            "/dest/path/",
        ]

        result_dirs = [
            "/dest",
            "/dest/path",
            "/dest/path/moved_item",
            "/dest/path/moved_item/child1",
            "/dest/path/moved_item/child1/sub",
            "/dest/path/moved_item/child1/sub/sub",
            "/dest/path/moved_item/child3",
            "/path",
            "/path/to",
            "/path/to/item1",
            "/path/to/item2",
            "/path/to/item2/child2",
            "/path/to/item2/child2/sub",
        ]

        result_streams = [
            "/dest/path/moved_item/child1/sub/sub/stream",
            "/dest/path/moved_item/stream",
            '/dest/path/moved_item/child3/stream'
        ]

        with open(path, 'wb+') as f:
            cfb = CompoundFileBinary(f, 'wb+')
            for p in dir_list:
                cfb.makedirs(p)

            for p in steam_list:
                s = cfb.open(p, 'w')
                s.write(stream_data)

            cfb.move(src, dst)
            with self.assertRaises(ValueError):
                cfb.move('/path/to/item2/stream',
                         '/dest/path/moved_item/child3')

            cfb.move('/path/to/item2/stream', '/dest/path/moved_item/child3/')

            with self.assertRaises(ValueError):
                cfb.move('/path/that/doesnt/exist', '/dest/path/moved_item/')

            with self.assertRaises(ValueError):
                cfb.move('/path/to/item2/child2', '/path/that/doesnt/exist')

            for p in result_dirs:
                assert cfb.exists(p)
                entry = cfb.find(p)
                assert entry.isdir()

            for p in result_streams:
                assert cfb.exists(p)
                entry = cfb.find(p)
                assert entry.isfile()
                assert entry.open('r').read() == stream_data

            cfb.close()

        with open(path, 'rb') as f:
            cfb = CompoundFileBinary(f, 'rb')

            for root, storage_items, streams in cfb.walk():
                pass
                # print(root.path(), streams)

            for p in result_dirs:
                assert cfb.exists(p)
                entry = cfb.find(p)
                assert entry.isdir()

            for p in result_streams:
                assert cfb.exists(p)
                entry = cfb.find(p)
                assert entry.isfile()

                assert entry.open('r').read() == stream_data