support xz

This commit is contained in:
dave 2019-05-04 21:34:18 -07:00
parent 0719ce4a5e
commit 1c57c0332e
2 changed files with 28 additions and 14 deletions

View File

@ -14,6 +14,7 @@ import tarfile
from collections import defaultdict from collections import defaultdict
from gzip import GzipFile from gzip import GzipFile
import lzma
from email import message_from_string, message_from_file from email import message_from_string, message_from_file
from functools import cmp_to_key from functools import cmp_to_key
@ -24,6 +25,13 @@ from arpy import Archive
REQUIRED_HEADERS = ('package', 'version', 'architecture') REQUIRED_HEADERS = ('package', 'version', 'architecture')
control_openers = {
'gz': lambda fob: GzipFile(fileobj=fob),
'xz': lambda fob: lzma.open(fob, "r")
}
logging.basicConfig() logging.basicConfig()
@ -47,7 +55,7 @@ class DpkgMissingControlFile(DpkgError):
pass pass
class DpkgMissingControlGzipFile(DpkgError): class DpkgMissingControlArchive(DpkgError):
"""No control.tar.gz file found in dpkg file""" """No control.tar.gz file found in dpkg file"""
pass pass
@ -288,16 +296,21 @@ class Dpkg(object):
def _process_dpkg_file(self, filename): def _process_dpkg_file(self, filename):
dpkg_archive = Archive(filename) dpkg_archive = Archive(filename)
dpkg_archive.read_all_headers() dpkg_archive.read_all_headers()
try: control_opener = None
control_tgz = dpkg_archive.archived_files[b'control.tar.gz'] control_tgz = None
except KeyError: for ext, opener in control_openers.items():
raise DpkgMissingControlGzipFile( control_name = b''.join([b'control.tar.', ext.encode()])
'Corrupt dpkg file: no control.tar.gz file in ar archive.') if control_name in dpkg_archive.archived_files:
control_tgz = dpkg_archive.archived_files[control_name]
control_opener = opener
break
if not control_tgz:
raise DpkgMissingControlArchive(
'Corrupt dpkg file: no control file archive in ar archive.')
self._log.debug('found controlgz: %s', control_tgz) self._log.debug('found controlgz: %s', control_tgz)
# have to pass through BytesIO because gzipfile doesn't support seek # have to pass through BytesIO because gzipfile doesn't support seek
# from end; luckily control tars are tiny # from end; luckily control tars are tiny
with GzipFile(fileobj=control_tgz) as gzf: with control_opener(control_tgz) as gzf:
self._log.debug('opened gzip file: %s', gzf) self._log.debug('opened gzip file: %s', gzf)
with tarfile.open(fileobj=io.BytesIO(gzf.read())) as control_tar: with tarfile.open(fileobj=io.BytesIO(gzf.read())) as control_tar:
self._log.debug('opened tar file: %s', control_tar) self._log.debug('opened tar file: %s', control_tar)

View File

@ -1,6 +1,8 @@
from distutils.core import setup from setuptools import setup
__VERSION__ = '1.3.2'
__VERSION__ = '1.3.1'
setup( setup(
name='pydpkg', name='pydpkg',
@ -12,9 +14,9 @@ setup(
url='https://github.com/theclimatecorporation/python-dpkg', url='https://github.com/theclimatecorporation/python-dpkg',
download_url='https://github.com/theclimatecorporation/python-dpkg/tarball/%s' % __VERSION__, download_url='https://github.com/theclimatecorporation/python-dpkg/tarball/%s' % __VERSION__,
keywords=['apt', 'debian', 'dpkg', 'packaging'], keywords=['apt', 'debian', 'dpkg', 'packaging'],
install_requires=[ setup_requires=[
'arpy==1.1.1', 'arpy==1.1.1',
'six==1.10.0', 'six>=1.10.0',
'PGPy==0.4.1' 'PGPy==0.4.1'
], ],
extras_require={ extras_require={
@ -31,6 +33,5 @@ setup(
"Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.5",
"Programming Language :: Python :: Implementation :: CPython", "Programming Language :: Python :: Implementation :: CPython",
"Topic :: System :: Archiving :: Packaging", "Topic :: System :: Archiving :: Packaging"]
]
) )