1
0
mirror of https://github.com/openSUSE/osc.git synced 2025-02-24 19:22:13 +01:00

Refactor extracting control from Debian package

Use separate functions for each compression type instead of a
cascade of conditionals.
This commit is contained in:
Rainer Müller 2022-08-16 10:41:27 +02:00
parent 5a13baf2ab
commit a6fd4f88d3

View File

@ -54,33 +54,14 @@ class DebQuery(packagequery.PackageQuery, packagequery.PackageQueryResult):
raise DebError(self.__path, 'no debian binary') raise DebError(self.__path, 'no debian binary')
if debbin.read() != b'2.0\n': if debbin.read() != b'2.0\n':
raise DebError(self.__path, 'invalid debian binary format') raise DebError(self.__path, 'invalid debian binary format')
control = arfile.get_file(b'control.tar.gz') for open_func in [self.__open_tar_gz,
if control is not None: self.__open_tar_xz,
# XXX: python2.4 relies on a name self.__open_tar_zst,
tar = tarfile.open(name='control.tar.gz', fileobj=control) self.__open_tar]:
else: tar = open_func(arfile)
control = arfile.get_file(b'control.tar.xz') if tar is not None:
if control: break
if not HAVE_LZMA: if tar is None:
raise DebError(self.__path, 'can\'t open control.tar.xz without python-lzma')
decompressed = lzma.decompress(control.read())
tar = tarfile.open(name="control.tar.xz",
fileobj=BytesIO(decompressed))
else:
control = arfile.get_file(b'control.tar.zst')
if control:
if not HAVE_ZSTD:
raise DebError(self.__path, 'can\'t open control.tar.zst without python-zstandard')
with zstandard.ZstdDecompressor().stream_reader(BytesIO(control.read())) as reader:
decompressed = reader.read()
tar = tarfile.open(name="control.tar.zst",
fileobj=BytesIO(decompressed))
else:
control = arfile.get_file(b'control.tar')
if control:
tar = tarfile.open(name="control.tar",
fileobj=control)
if control is None:
raise DebError(self.__path, 'missing control.tar') raise DebError(self.__path, 'missing control.tar')
try: try:
name = './control' name = './control'
@ -94,6 +75,40 @@ class DebQuery(packagequery.PackageQuery, packagequery.PackageQueryResult):
self.__parse_control(control, all_tags, self_provides, *extra_tags) self.__parse_control(control, all_tags, self_provides, *extra_tags)
return self return self
def __open_tar(self, arfile):
control = arfile.get_file(b'control.tar')
if control:
# XXX: python2.4 relies on a name
return tarfile.open(name="control.tar", fileobj=control)
return None
def __open_tar_gz(self, arfile):
control = arfile.get_file(b'control.tar.gz')
if control:
return tarfile.open(name='control.tar.gz', fileobj=control)
return None
def __open_tar_xz(self, arfile):
control = arfile.get_file(b'control.tar.xz')
if control:
if not HAVE_LZMA:
raise DebError(self.__path, 'can\'t open control.tar.xz without python-lzma')
decompressed = lzma.decompress(control.read())
return tarfile.open(name="control.tar.xz",
fileobj=BytesIO(decompressed))
return None
def __open_tar_zst(self, arfile):
control = arfile.get_file(b'control.tar.zst')
if control:
if not HAVE_ZSTD:
raise DebError(self.__path, 'can\'t open control.tar.zst without python-zstandard')
with zstandard.ZstdDecompressor().stream_reader(BytesIO(control.read())) as reader:
decompressed = reader.read()
return tarfile.open(name="control.tar.zst",
fileobj=BytesIO(decompressed))
return None
def __parse_control(self, control, all_tags=False, self_provides=True, *extra_tags): def __parse_control(self, control, all_tags=False, self_provides=True, *extra_tags):
data = control.readline().strip() data = control.readline().strip()
while data: while data: