]> git.decadent.org.uk Git - dak.git/blob - daklib/compress.py
Add by-hash support
[dak.git] / daklib / compress.py
1 # Copyright (C) 2015, Ansgar Burchardt <ansgar@debian.org>
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16
17 """
18 Helper methods to deal with (de)compressing files
19 """
20
21 import daklib.config
22
23 import os
24 import shutil
25 import subprocess
26 import tempfile
27
28 def decompress_xz(input, output):
29     subprocess.check_call(["xz", "--decompress"], stdin=input, stdout=output)
30
31 def decompress_bz2(input, output):
32     subprocess.check_call(["bzip2", "--decompress"], stdin=input, stdout=output)
33
34 def decompress_gz(input, output):
35     subprocess.check_call(["gzip", "--decompress"], stdin=input, stdout=output)
36
37 decompressors = {
38     '.xz': decompress_xz,
39     '.bz2': decompress_bz2,
40     '.gz': decompress_gz,
41 }
42
43 def decompress(input, output, filename=None):
44     if filename is None:
45         filename = input.name
46
47     base, ext = os.path.splitext(filename)
48     decompressor = decompressors.get(ext, None)
49     if decompressor is not None:
50         decompressor(input, output)
51     else:
52         shutil.copyfileobj(input, output)