summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorEmilio Pozuelo Monfort <pochu@debian.org>2020-09-30 21:01:33 +0200
committerEmilio Pozuelo Monfort <pochu@debian.org>2020-09-30 21:30:26 +0200
commitb3e1e759b3038544b1e71ce6ed8707a61406ecb8 (patch)
tree9ab10f23e1b2638f817629028faf47cba895f438 /lib
parent4613cf4f398570c7ba630b5648faf2fdadedfff3 (diff)
debian_support: updateFile: support .xz files
https://bugs.debian.org/931533
Diffstat (limited to 'lib')
-rw-r--r--lib/python/debian_support.py52
1 files changed, 31 insertions, 21 deletions
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]:

© 2014-2024 Faster IT GmbH | imprint | privacy policy