]>
git.ipfire.org Git - pakfire.git/blob - pakfire/compress.py
2 ###############################################################################
4 # Pakfire - The IPFire package management system #
5 # Copyright (C) 2011 Pakfire development team #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
27 from constants
import *
31 progressbar
.Bar(left
="[", right
="]"),
33 progressbar
.Percentage(),
39 def __compress_helper(i
, o
, comp
, flush
, progress
=None):
41 widgets
= [ "%-30s " % os
.path
.basename(filename
)] + PROGRESS_WIDGETS
43 maxval
= os
.path
.getsize(filename
)
45 progress
= progressbar
.ProgressBar(
53 buf
= i
.read(BUFFER_SIZE
)
61 buf
= i
.read(BUFFER_SIZE
)
68 def compress(filename
, filename2
=None, algo
="xz", progress
=None):
75 o
= open(filename2
, "w")
77 compressobj(i
, o
, algo
="xz", progress
=None)
82 def compressobj(i
, o
, algo
="xz", progress
=None):
85 comp
= lzma
.LZMACompressor()
88 comp
= zlib
.compressobj(9)
90 return __compress_helper(i
, o
, comp
.compress
, comp
.flush
, progress
=progress
)
92 def decompress(filename
, filename2
=None, algo
="xz", progress
=None):
99 o
= open(filename2
, "w")
101 decompressobj(i
, o
, algo
="xz", progress
=None)
106 def decompressobj(i
, o
, algo
="xz", progress
=None):
109 comp
= lzma
.LZMADecompressor()
112 comp
= zlib
.decompressobj(9)
114 return __compress_helper(i
, o
, comp
.decompress
, comp
.flush
, progress
=progress
)
116 def compress_file(inputfile
, outputfile
, message
="", algo
="xz", progress
=True):
118 Compress a file in place.
120 assert os
.path
.exists(inputfile
)
122 # Get total size of the file for the progressbar.
123 total_size
= os
.path
.getsize(inputfile
)
125 # Open the input file for reading.
126 i
= open(inputfile
, "r")
128 # Open the output file for wrinting.
129 o
= open(outputfile
, "w")
133 message
= _("Compressing %s") % os
.path
.basename(filename
)
135 progress
= progressbar
.ProgressBar(
136 widgets
= ["%-40s" % message
, " ",] + PROGRESS_WIDGETS
,
143 compressor
= lzma
.LZMACompressor()
145 comp
= zlib
.decompressobj(9)
147 raise Exception, "Unknown compression choosen: %s" % algo
151 buf
= i
.read(BUFFER_SIZE
)
155 # Update progressbar.
158 progress
.update(size
)
160 # Compress the bits in buf.
161 buf
= compressor
.compress(buf
)
163 # Write the compressed output.
167 buf
= compressor
.flush()
170 # Close the progress bar.