Ejemplo n.º 1
0
    def test_empty_pak_file(self):
        with pak.PakFile(self.buff, 'w'):
            pass

        self.buff.seek(0)

        with pak.PakFile(self.buff, 'r') as pak_file:
            self.assertEqual(len(pak_file.namelist()), 0, 'Pak file should have not entries')
            self.assertEqual(pak_file.start_of_directory, 12, 'Directory should start immediately after header')
Ejemplo n.º 2
0
    def test_zero_byte_file(self):
        with pak.PakFile(self.buff, 'w') as pak_file:
            pak_file.writestr('zero.txt', b'')

        self.buff.seek(0)

        with pak.PakFile(self.buff) as pak_file:
            info = pak_file.getinfo('zero.txt')
            self.assertEqual(info.file_offset, 12, 'File Info offset of test file should be 12')
            self.assertEqual(info.file_size, 0, 'File Info size of test file should be 0')

            data = pak_file.read('zero.txt')
            self.assertEqual(len(data), 0, 'Length of bytes read should be zero.')
Ejemplo n.º 3
0
    def test_write_string(self):
        p0 = pak.PakFile(self.buff, 'w')
        p0.writestr('test.cfg', b'bind ALT +strafe')
        p0.writestr(pak.PakInfo('docs/readme.txt'), 'test')
        p0.close()

        self.buff.seek(0)

        p1 = pak.PakFile(self.buff, 'r')

        self.assertTrue('test.cfg' in p1.namelist(), 'Cfg file should be in Pak file')
        self.assertTrue('docs/readme.txt' in p1.namelist(), 'Txt file should be in Pak file')
        self.assertEqual(p1.read('test.cfg'), b'bind ALT +strafe', 'Cfg file content should not change')
        self.assertEqual(p1.read('docs/readme.txt').decode('ascii'), 'test', 'Txt file conent should not change')

        p1.close()
        self.buff.close()
Ejemplo n.º 4
0
    def test_context_manager(self):
        with pak.PakFile('./test_data/test.pak', 'r') as pak_file:
            self.assertFalse(pak_file.fp.closed, 'File should be open')
            self.assertEqual(pak_file.mode, 'r', 'File mode should be \'r\'')
            fp = pak_file.fp
            pak_file._did_modify = False

        self.assertTrue(fp.closed, 'File should be closed')
        self.assertIsNone(pak_file.fp, 'File pointer should be cleaned up')
Ejemplo n.º 5
0
    def test_append(self):
        with open('./test_data/test.pak', 'rb') as f:
            self.buff = io.BytesIO(f.read())

        pak_file = pak.PakFile(self.buff, 'a')
        pak_file.write('./test_data/test.bsp')
        pak_file.close()

        self.buff.seek(0)

        pak_file = pak.PakFile(self.buff, 'r')
        self.assertTrue('./test_data/test.bsp' in pak_file.namelist(), 'Appended file should be in Pak file')
        self.assertEqual(len(pak_file.infolist()), 3, 'Pak file should contain exactly three entries.')

        fp = pak_file.fp
        pak_file.close()

        self.assertFalse(self.buff.closed, 'Pak file should not close passed file-like object')

        self.buff.close()
Ejemplo n.º 6
0
    def test_write(self):
        pak_file = pak.PakFile(self.buff, 'w')
        self.assertFalse(pak_file.fp.closed, 'File should be open')

        pak_file.write('./test_data/test.mdl')
        pak_file.write('./test_data/test.bsp', './progs/test.bsp')

        self.assertTrue('./test_data/test.mdl' in pak_file.namelist(), 'Mdl file should be in Pak file')
        self.assertTrue('./progs/test.bsp' in pak_file.namelist(), 'Bsp file should be in Pak file')

        fp = pak_file.fp
        pak_file.close()
        self.assertFalse(fp.closed, 'File should be open')
        self.assertIsNone(pak_file.fp, 'File pointer should be cleaned up')

        self.buff.close()
Ejemplo n.º 7
0
    def test_read(self):
        pak_file = pak.PakFile('./test_data/test.pak', 'r')
        self.assertFalse(pak_file.fp.closed, 'File should be open')
        self.assertEqual(len(pak_file.infolist()), 2, 'Pak file should contain exactly two entries.')

        info = pak_file.getinfo('./test_data/test.mdl')
        self.assertIsNotNone(info, 'FileInfo should not be None')
        self.assertEqual(info.filename, './test_data/test.mdl', 'FileInfo names should match')
        self.assertEqual(info.file_size, 4724, 'FileInfo size of test file should be 4724')
        self.assertEqual(info.file_offset, 12, 'FileInfo offset of test file should be 12')

        file = pak_file.open('./test_data/test.mdl')
        self.assertIsNotNone(file, 'File should not be None')
        file.close()

        fp = pak_file.fp
        pak_file.close()
        self.assertTrue(fp.closed, 'File should be closed')
        self.assertIsNone(pak_file.fp, 'File pointer should be cleaned up')
Ejemplo n.º 8
0
                                                       __version__))

    args = parser.parse_args()

    if not args.list:
        parser.error('the following arguments are required: list')

    dir = os.path.dirname(args.file) or '.'
    if not os.path.exists(dir):
        os.makedirs(dir)

    filemode = 'a'
    if not os.path.isfile(args.file):
        filemode = 'w'

    with pak.PakFile(args.file, filemode) as pak_file:
        if not args.quiet:
            print('Archive: %s' % os.path.basename(args.file))

        # Process input files
        for file in args.list:
            # Walk directories
            if os.path.isdir(file):
                for root, dirs, files in os.walk(file):
                    for name in [f for f in files if not f.startswith('.')]:
                        fullpath = os.path.join(root, name)
                        relpath = os.path.relpath(fullpath, os.getcwd())

                        if not args.quiet:
                            print('  adding: %s' % relpath)
Ejemplo n.º 9
0
                        '--version',
                        dest='version',
                        action='version',
                        help=argparse.SUPPRESS,
                        version='{} version {}'.format(parser.prog,
                                                       __version__))

    args = parser.parse_args()

    if not pak.is_pakfile(args.file):
        print('{0}: cannot find or open {1}'.format(parser.prog, args.file),
              file=sys.stderr)
        sys.exit(1)

    if args.list:
        with pak.PakFile(args.file) as pak_file:
            info_list = sorted(pak_file.infolist(), key=lambda i: i.filename)

            headers = ['Length', 'Name']
            table = [[i.file_size, i.filename] for i in info_list]
            length = sum([i.file_size for i in info_list])
            count = len(info_list)
            table.append(
                [length,
                 '%d file%s' % (count, 's' if count == 1 else '')])

            separator = []
            for i in range(len(headers)):
                t = max(len(str(length)), len(headers[i]) + 2)
                separator.append('-' * t)