From ce7ea2e8d86f4c6a0d9c8f546cf46f19cd26ec2c Mon Sep 17 00:00:00 2001 From: Ansgar Burchardt Date: Tue, 11 Aug 2015 21:38:48 +0200 Subject: [PATCH] Add helper functions to decompress files. --- daklib/compress.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 daklib/compress.py diff --git a/daklib/compress.py b/daklib/compress.py new file mode 100644 index 00000000..26634800 --- /dev/null +++ b/daklib/compress.py @@ -0,0 +1,52 @@ +# Copyright (C) 2015, Ansgar Burchardt +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +""" +Helper methods to deal with (de)compressing files +""" + +import daklib.config + +import os +import shutil +import subprocess +import tempfile + +def decompress_xz(input, output): + subprocess.check_call(["xz", "--decompress"], stdin=input, stdout=output) + +def decompress_bz2(input, output): + subprocess.check_call(["bzip2", "--decompress"], stdin=input, stdout=output) + +def decompress_gz(input, output): + subprocess.check_call(["gzip", "--decompress"], stdin=input, stdout=output) + +decompressors = { + '.xz': decompress_xz, + '.bz2': decompress_bz2, + '.gz': decompress_gz, +} + +def decompress(input, output, filename=None): + if filename is None: + filename = input.name + + base, ext = os.path.splitext(filename) + decompressor = decompressors.get(ext, None) + if decompressor is not None: + decompressor(input, output) + else: + shutil.copyfileobj(input, output) -- 2.39.5