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.end_of_data, 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.pak')
        pak_file.close()

        self.buff.seek(0)

        pak_file = pak.PakFile(self.buff, 'r')
        self.assertTrue('./test_data/test.pak' 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.pak')

        self.assertTrue('./test_data/test.pak' in pak_file.namelist(), 'Pak 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('brown.jpg')
        self.assertIsNotNone(info, 'FileInfo should not be None')
        self.assertEqual(info.filename, 'brown.jpg', 'FileInfo names should match')
        self.assertEqual(info.file_size, 7352, '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('brown.jpg')
        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
def main():
    parser = Parser(
        prog='unpak',
        description='Default action is to extract files to xdir.',
        epilog='example: unpak PAK0.PAK -d ./out => extract all files to ./out'
    )

    parser.add_argument('file', metavar='file.pak', action=ResolvePathAction)

    parser.add_argument('-l', '--list', action='store_true', help='list files')

    parser.add_argument('-d',
                        metavar='xdir',
                        dest='dest',
                        default=os.getcwd(),
                        action=ResolvePathAction,
                        help='extract files into xdir')

    parser.add_argument('-q',
                        dest='quiet',
                        action='store_true',
                        help='quiet mode')

    parser.add_argument('-v',
                        '--version',
                        dest='version',
                        action='version',
                        help=argparse.SUPPRESS,
                        version=f'{parser.prog} version {hcli.__version__}')

    args = parser.parse_args()

    if not pak.is_pakfile(args.file):
        print(f'{parser.prog}: cannot find or open {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, f'{count} file{"s" if count == 1 else ""}'])

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

            table.insert(-1, separator)

            print(f'Archive: {os.path.basename(args.file)}')
            print(tabulate(table, headers=headers))

            sys.exit(0)

    with pak.PakFile(args.file) as pak_file:
        info_list = pak_file.infolist()
        for item in sorted(info_list, key=lambda i: i.filename):
            filename = item.filename
            fullpath = os.path.join(args.dest, filename)

            if not args.quiet:
                print(f' extracting: {fullpath}')

            try:
                pak_file.extract(filename, args.dest)

            except:
                print(f'{parser.prog}: error: {sys.exc_info()[0]}',
                      file=sys.stderr)

    sys.exit(0)