File: __init__.py

package info (click to toggle)
game-data-packager 87
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 33,392 kB
  • sloc: python: 15,387; sh: 704; ansic: 95; makefile: 50
file content (469 lines) | stat: -rw-r--r-- 12,795 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#!/usr/bin/python3
# encoding=utf-8
#
# Copyright © 2014-2015 Simon McVittie <smcv@debian.org>
# Copyright © 2015 Alexandre Detiste <alexandre@detiste.be>
# SPDX-License-Identifier: GPL-2.0-or-later

from __future__ import annotations

import errno
import os
import shlex
import shutil
import subprocess
import tarfile
import time
import zipfile
from abc import (ABCMeta, abstractmethod)
from collections.abc import (Collection, Iterator)
from types import TracebackType
from typing import (Any, BinaryIO)


class UnpackableEntry(metaclass=ABCMeta):
    """An entry in a StreamUnpackable.
    """
    @property
    @abstractmethod
    def is_directory(self) -> bool:
        raise NotImplementedError

    @property
    @abstractmethod
    def is_regular_file(self) -> bool:
        """True if the entry is a regular file. False if it is a
        directory, symlink, or some special thing like an instruction
        to patch some other file.
        """
        raise NotImplementedError

    @property
    def is_extractable(self) -> bool:
        """True if the entry is something that we can extract.

        The default implementation is that we can extract regular files.
        """
        return self.is_regular_file

    def get_symbolic_link_target(self) -> str | None:
        """Target of the symbolic link, or None if this is not a
        symbolic link.
        """
        return None

    @property
    def mtime(self) -> float | None:
        """The last-modification time, or None if unspecified."""
        return None

    @property
    @abstractmethod
    def name(self) -> str:
        """The absolute or relative filename, with Unix path separators."""
        raise NotImplementedError

    @property
    @abstractmethod
    def size(self) -> int:
        """The size in bytes."""
        raise NotImplementedError

    @property
    def type_indicator(self) -> str:
        """One or more ASCII symbols indicating the file type."""
        if self.is_directory:
            ret = 'd'
        elif self.is_regular_file:
            ret = '-'
        elif self.get_symbolic_link_target() is not None:
            ret = 'l'
        else:
            ret = '?'

        if self.is_extractable:
            ret += 'r'
        else:
            ret += '-'

        return ret


class SimpleUnpackable(metaclass=ABCMeta):
    """An archive in which entries can be inspected and extracted,
    but only all at once.
    """

    @property
    @abstractmethod
    def format(self) -> str:
        """Return the YAML "format" entry for this file.
        """
        raise NotImplementedError

    @abstractmethod
    def extractall(
        self,
        path: str,
        members: Collection[str] | None = None,
    ) -> None:
        """Extract all or most members of this archive to path.
        members is merely a hint: extracting more members than desired
        is allowed.
        """
        raise NotImplementedError

    def seekable(self) -> bool:
        """Return True if the unpacker is able to seek.
        """
        return False


class StreamUnpackable(SimpleUnpackable, metaclass=ABCMeta):
    """An archive in which entries can be inspected and extracted
    by iteration.
    """

    @abstractmethod
    def __iter__(self) -> Iterator[UnpackableEntry]:
        """Iterate through UnpackableEntry objects."""
        raise NotImplementedError

    @abstractmethod
    def open(self, member: str | UnpackableEntry) -> BinaryIO:
        """Open a binary file-like entry for the name or entry.
        """
        raise NotImplementedError

    def extract(
        self,
        member: str | UnpackableEntry,
        path: str | None = None,
    ) -> None:
        """Extract the given member from the archive into the given
        directory.
        """

        assert not isinstance(member, bytes), member

        if isinstance(member, str):
            filename = member
        else:
            filename = member.name

        reader = self.open(member)

        if not reader:
            raise ValueError('cannot open %s' % member)

        with reader:
            filename = filename.lstrip('/')

            while filename.startswith('../'):
                filename = filename[3:]
            filename = filename.replace('/../', '/')
            if filename.endswith('/..'):
                filename = filename[:-3]
            if filename.endswith('/'):
                filename = filename[:-1]
            if path is None:
                path = '.'

            dest = os.path.join(path, filename)
            os.makedirs(os.path.dirname(dest), exist_ok=True)

            try:
                os.remove(dest)
            except OSError as e:
                if e.errno != errno.ENOENT:
                    raise

            with open(dest, 'xb') as writer:
                shutil.copyfileobj(reader, writer)

    def extractall(
        self,
        path: str | None,
        members: Collection[str] | None = None,
    ) -> None:
        for entry in self:
            if entry.is_extractable:
                if members is None or entry.name in members:
                    self.extract(entry, path)

    def printdir(self) -> None:
        for entry in self:
            if entry.size is None:
                size = '?' * 9
            else:
                size = '%9s' % entry.size

            if entry.mtime is not None:
                mtime = time.strftime(
                    '%Y-%m-%d %H:%M:%S', time.gmtime(entry.mtime))
            else:
                mtime = '????-??-?? ??:??:??'

            print('%s %s %s %s' % (
                entry.type_indicator, size, mtime,
                shlex.quote(entry.name)))

    def seekable(self) -> bool:
        """Return True if the unpacker is able to seek.
        """
        return False

    def __enter__(self) -> StreamUnpackable:
        return self

    def __exit__(
        self,
        ex_type: type[BaseException] | None = None,
        ex_value: BaseException | None = None,
        ex_traceback: TracebackType | None = None
    ) -> None:
        pass


class WrapperUnpacker(StreamUnpackable):
    """Base class for a StreamUnpackable that wraps a TarFile-like object."""

    def __init__(self) -> None:
        # TODO: It must support open(), close() and iteration, but we don't
        # currently check that
        # + https://github.com/python/mypy/issues/3138
        self._impl: Any | None = None

    @abstractmethod
    def _wrap_entry(self, entry: Any) -> UnpackableEntry:
        raise NotImplementedError

    @abstractmethod
    def _is_entry(self, entry: Any) -> bool:
        raise NotImplementedError

    def __enter__(self) -> WrapperUnpacker:
        return self

    def __exit__(
        self,
        ex_type: type[BaseException] | None = None,
        ex_value: BaseException | None = None,
        ex_traceback: TracebackType | None = None
    ) -> None:
        if self._impl is not None:
            self._impl.close()
            self._impl = None

    def __iter__(self) -> Iterator[UnpackableEntry]:
        assert self._impl is not None, 'unpacker context not entered'

        for entry in self._impl:
            yield self._wrap_entry(entry)

    def open(self, entry: Any) -> BinaryIO:
        assert self._impl is not None, 'unpacker context not entered'
        assert self._is_entry(entry)
        contents = self._impl.open(entry.impl)
        assert contents is not None
        return contents


class TarEntry(UnpackableEntry):
    __slots__ = 'impl'

    def __init__(self, impl: tarfile.TarInfo) -> None:
        self.impl = impl

    @property
    def is_directory(self) -> bool:
        return self.impl.isdir()

    @property
    def is_regular_file(self) -> bool:
        return self.impl.isfile()

    @property
    def mtime(self) -> float:
        return self.impl.mtime

    @property
    def name(self) -> str:
        return self.impl.name

    @property
    def size(self) -> int:
        return self.impl.size

    def get_symbolic_link_target(self) -> str | None:
        if self.impl.issym():
            return self.impl.linkname
        else:
            return None

    @property
    def type_indicator(self) -> str:
        """One or more ASCII symbols indicating the file type."""
        if self.impl.isdir():
            ret = 'd'
        elif self.impl.isfile():
            ret = '-'
        elif self.impl.issym():
            ret = 'l'
        else:
            ret = '?<%r>' % self.impl.type

        if self.is_extractable:
            ret += 'r'
        else:
            ret += '-'

        return ret


class DpkgDebUnpacker(WrapperUnpacker):
    def __init__(self, path: str) -> None:
        self._path = path
        self._fsys_process: Any | None = None  # ?

    def __enter__(self) -> DpkgDebUnpacker:
        self._fsys_process = subprocess.Popen(
            ['dpkg-deb', '--fsys-tarfile', self._path],
            stdout=subprocess.PIPE,
        ).__enter__()
        assert self._fsys_process is not None
        self._impl = tarfile.open(
            self._path, mode='r|', fileobj=self._fsys_process.stdout,
        ).__enter__()
        return self

    def __exit__(
        self,
        ex_type: type[BaseException] | None = None,
        ex_value: BaseException | None = None,
        ex_traceback: TracebackType | None = None
    ) -> None:
        if self._impl is not None:
            self._impl.__exit__(ex_type, ex_value, ex_traceback)
            self._impl = None

        if self._fsys_process is not None:
            self._fsys_process.__exit__(ex_type, ex_value, ex_traceback)
            self._fsys_process = None

    @property
    def format(self) -> str:
        return 'deb'

    def open(self, entry: Any) -> BinaryIO:
        assert isinstance(entry, TarEntry)
        assert type(self._impl) is tarfile.TarFile
        contents = self._impl.extractfile(entry.impl)
        assert contents is not None
        return contents

    def _is_entry(self, entry: Any) -> bool:
        return isinstance(entry, TarEntry)

    def _wrap_entry(self, entry: tarfile.TarInfo) -> UnpackableEntry:
        return TarEntry(entry)


class TarUnpacker(WrapperUnpacker):
    def __init__(
        self,
        name: str,
        reader: BinaryIO | None = None,
        compression: str = '*',
        skip: int = 0,
    ) -> None:
        super(TarUnpacker, self).__init__()
        self.skip = skip
        self.compression = compression

        if reader is None:
            reader = open(name, 'rb')

        if skip:
            discard = reader.read(skip)
            assert len(discard) == skip

        self._impl = tarfile.open(
            name, mode='r|' + compression, fileobj=reader)

    @property
    def format(self) -> str:
        return 'tar.' + self.compression

    def open(self, entry: Any) -> BinaryIO:
        assert isinstance(entry, TarEntry)
        assert type(self._impl) is tarfile.TarFile
        contents = self._impl.extractfile(entry.impl)
        assert contents is not None
        return contents

    def _is_entry(self, entry: Any) -> bool:
        return isinstance(entry, TarEntry)

    def _wrap_entry(self, entry: tarfile.TarInfo) -> UnpackableEntry:
        return TarEntry(entry)


class ZipEntry(UnpackableEntry):
    __slots__ = 'impl'

    def __init__(self, impl: zipfile.ZipInfo) -> None:
        self.impl = impl

    @property
    def is_directory(self) -> bool:
        return self.name.endswith('/')

    @property
    def is_regular_file(self) -> bool:
        return not self.name.endswith('/')

    @property
    def mtime(self) -> float:
        return time.mktime(self.impl.date_time + (0, 0, -1))

    @property
    def name(self) -> str:
        return self.impl.filename

    @property
    def size(self) -> int:
        return self.impl.file_size


class ZipUnpacker(WrapperUnpacker):
    def __init__(
        self,
        file_or_name: str | BinaryIO
    ) -> None:
        super(ZipUnpacker, self).__init__()
        if hasattr(file_or_name, 'seekable') and not file_or_name.seekable():
            self.__seekable = False
        else:
            # zip files based on an on-disk file are seekable
            self.__seekable = True

        self._impl = zipfile.ZipFile(file_or_name, 'r')

    def __iter__(self) -> Iterator[ZipEntry]:
        assert type(self._impl) is zipfile.ZipFile
        for entry in self._impl.infolist():
            yield ZipEntry(entry)

    def _is_entry(self, entry: Any) -> bool:
        return isinstance(entry, ZipEntry)

    def _wrap_entry(self, entry: zipfile.ZipInfo) -> UnpackableEntry:
        return ZipEntry(entry)

    @property
    def format(self) -> str:
        return 'zip'

    def seekable(self) -> bool:
        return self.__seekable