Skip to content

Commit

Permalink
Support extraction of BZIP2 files with the Python bz2 module.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastian Kleineidam committed May 11, 2012
1 parent 6882f93 commit 45a5f7e
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 4 deletions.
2 changes: 2 additions & 0 deletions doc/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* Added support for the lbzip2 program handling BZIP2 archives.
* Added support for the plzip program handling LZIP archives.
* Prevent overwriting the same file with repack.
* Support extraction of BZIP2 files with the Python bz2 module.


0.15 "Contraband" (released 8.4.2012)

Expand Down
8 changes: 6 additions & 2 deletions patoolib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
},
'bzip2': {
None: ('7z', '7za'),
'extract': ('pbzip2', 'lbzip2', 'bzip2'),
'extract': ('pbzip2', 'lbzip2', 'bzip2', 'pybz2'),
'test': ('pbzip2', 'lbzip2', 'bzip2'),
'create': ('pbzip2', 'lbzip2', 'bzip2'),
'list': ('echo',),
Expand Down Expand Up @@ -452,7 +452,11 @@ def _handle_archive (archive, command, *args, **kwargs):
archive = util.tmpfile(dir=os.path.dirname(archive), suffix=".arc")
try:
cmdlist = get_archive_cmdlist(archive, encoding, program, *args, **cmd_kwargs)
run_archive_cmdlist(cmdlist)
if cmdlist:
# an empty command list means the get_archive_cmdlist() function
# already handled the command (eg. when it's a builting Python
# function)
run_archive_cmdlist(cmdlist)
if command == 'extract':
if do_cleanup_outdir:
target, msg = cleanup_outdir(cmd_kwargs["outdir"])
Expand Down
46 changes: 46 additions & 0 deletions patoolib/programs/pybz2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2012 Bastian Kleineidam
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
"""Archive commands for the bz2 Python module."""
from patoolib import util
try:
# try external bz2file module with multi-stream support
import bz2file as bz2
except ImportError:
import bz2

READ_SIZE_BYTES = 1024*1024

def extract_bzip2 (archive, encoding, cmd, **kwargs):
"""Extract a BZIP2 archive with the bz2 Python module functionality."""
verbose = kwargs['verbose']
if verbose:
util.log_info('extracting %s...' % archive)
targetname = util.get_single_outfile(kwargs['outdir'], archive)
bz2file = bz2.BZ2File(archive)
try:
targetfile = open(targetname, 'wb')
try:
data = bz2file.read(READ_SIZE_BYTES)
while data:
targetfile.write(data)
data = bz2file.read(READ_SIZE_BYTES)
finally:
targetfile.close()
finally:
bz2file.close()
if verbose:
util.log_info('... extracted to %s' % targetname)
return None
10 changes: 9 additions & 1 deletion patoolib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,17 @@ def stripext (filename):
def get_single_outfile (directory, archive):
"""Get output filename if archive is in a single file format like gzip."""
outfile = os.path.join(directory, stripext(archive))
if archive == outfile:
if is_same_filename(archive, outfile):
# prevent overwriting the archive itself
outfile += ".raw"
if os.path.exists(outfile):
# prevent overwriting existing files
i = 1
newfile = "%s%d" % (outfile, i)
while os.path.exists(newfile):
newfile = "%s%d" % (outfile, i)
i += 1
outfile = newfile
return outfile


Expand Down
4 changes: 4 additions & 0 deletions tests/test_archives.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ def test_bzip2 (self):
self.archive_test('t .bz2')
self.archive_create('t .bz2', singlefile=True)

def test_pybz2 (self):
self.program = 'pybz2'
self.archive_extract('t .bz2')

@needs_program('pbzip2')
def test_pbzip2 (self):
self.program = 'pbzip2'
Expand Down
2 changes: 1 addition & 1 deletion tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2010-2011 Bastian Kleineidam
# Copyright (C) 2010-2012 Bastian Kleineidam
#
# 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
Expand Down

0 comments on commit 45a5f7e

Please sign in to comment.