From b3e1e759b3038544b1e71ce6ed8707a61406ecb8 Mon Sep 17 00:00:00 2001 From: Emilio Pozuelo Monfort Date: Wed, 30 Sep 2020 21:01:33 +0200 Subject: debian_support: updateFile: support .xz files https://bugs.debian.org/931533 --- lib/python/debian_support.py | 52 ++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 21 deletions(-) (limited to 'lib') diff --git a/lib/python/debian_support.py b/lib/python/debian_support.py index b6e5134474..73f6b0f80f 100644 --- a/lib/python/debian_support.py +++ b/lib/python/debian_support.py @@ -18,7 +18,7 @@ from __future__ import print_function """This module implements facilities to deal with Debian-specific metadata.""" -import gzip +import gzip, lzma import io import json import os.path @@ -28,8 +28,10 @@ import tempfile try: from urllib.request import urlopen + from urllib.error import URLError except ImportError: from urllib2 import urlopen + from urllib2.error import URLError try: from cStringIO import StringIO as streamIO @@ -316,9 +318,6 @@ def patchLines(lines, patches): lines[first:last] = args def replaceFile(lines, local): - - import os.path - local_new = local + '.new' new_file = open(local_new, 'w+') @@ -331,32 +330,41 @@ def replaceFile(lines, local): if os.path.exists(local_new): os.unlink(local_new) -def downloadGunzipLines(remote): - """Downloads a file from a remote location and gunzips it. +def downloadCompressedLines(remote): + """Downloads a file from a remote location and uncompresses it. Returns the lines in the file.""" + if remote.endswith('.gz'): + cls = gzip + elif remote.endswith('.xz'): + cls = lzma + else: + raise ValueError('file format not supported: %s' % remote) + data = urlopen(remote, timeout=TIMEOUT) try: - gfile = gzip.GzipFile(fileobj=streamIO(data.read())) - try: - if sys.version_info.major == 3: - return io.TextIOWrapper(gfile).readlines() - else: - return gfile.readlines() - finally: - gfile.close() + b = io.BytesIO(cls.decompress(data.read())) + t = io.TextIOWrapper(b, 'utf-8') + return t.readlines() finally: data.close() - + +def downloadLines(remote): + try: + return downloadCompressedLines(remote + '.xz') + except URLError: + return downloadCompressedLines(remote + '.gz') + def downloadFile(remote, local): - """Copies a gzipped remote file to the local system. + """Copies a compressed remote file to the local system. - remote - URL, without the .gz suffix + remote - URL, without compression suffix local - name of the local file """ - - lines = downloadGunzipLines(remote + '.gz') + + lines = downloadLines(remote) + replaceFile(lines, local) return lines @@ -440,8 +448,10 @@ def updateFile(remote, local, verbose=None): if verbose: print("updateFile: downloading patch " + repr(patch_name)) try: - patch_contents = downloadGunzipLines(remote + '.diff/' + patch_name - + '.gz') + # We could remove the extension here and call downloadLines + # when diff files come with another compression + patch_contents = downloadCompressedLines(remote + '.diff/' + + patch_name + '.gz') except IOError: return downloadFile(remote, local) if readLinesSHA1(patch_contents ) != patch_hashes[patch_name]: -- cgit v1.2.3