]> git.decadent.org.uk Git - dak.git/commitdiff
Add helper functions to decompress files.
authorAnsgar Burchardt <ansgar@debian.org>
Tue, 11 Aug 2015 19:38:48 +0000 (21:38 +0200)
committerAnsgar Burchardt <ansgar@debian.org>
Wed, 12 Aug 2015 08:00:08 +0000 (10:00 +0200)
daklib/compress.py [new file with mode: 0644]

diff --git a/daklib/compress.py b/daklib/compress.py
new file mode 100644 (file)
index 0000000..2663480
--- /dev/null
@@ -0,0 +1,52 @@
+# Copyright (C) 2015, Ansgar Burchardt <ansgar@debian.org>
+#
+# 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)