2010-02-21 11:14:57 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-02-20 20:04:48 +00:00
|
|
|
# Copyright (C) 2010-2013 Bastian Kleineidam
|
2010-02-21 11:14:57 +00:00
|
|
|
#
|
|
|
|
# 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/>.
|
2012-11-19 19:58:42 +00:00
|
|
|
from __future__ import print_function
|
2012-05-25 20:15:29 +00:00
|
|
|
import sys
|
2012-11-19 19:58:42 +00:00
|
|
|
if not hasattr(sys, "version_info") or sys.version_info < (2, 7, 0, "final", 0):
|
|
|
|
raise SystemExit("This program requires Python 2.7 or later.")
|
2010-02-21 11:14:57 +00:00
|
|
|
import os
|
|
|
|
import shutil
|
2010-03-06 14:23:16 +00:00
|
|
|
import stat
|
2012-11-19 19:58:42 +00:00
|
|
|
import importlib
|
2012-05-25 20:15:29 +00:00
|
|
|
from . import util
|
2013-02-25 20:04:02 +00:00
|
|
|
__all__ = ['list_formats', 'list_archive', 'extract_archive', 'test_archive',
|
|
|
|
'create_archive', 'diff_archives', 'search_archive', 'repack_archive']
|
|
|
|
|
2010-02-21 11:14:57 +00:00
|
|
|
|
2010-02-21 11:20:47 +00:00
|
|
|
# Supported archive commands
|
2010-02-21 12:45:41 +00:00
|
|
|
ArchiveCommands = ('list', 'extract', 'test', 'create')
|
2010-02-21 11:14:57 +00:00
|
|
|
|
|
|
|
# Supported archive formats
|
2012-08-03 20:26:40 +00:00
|
|
|
ArchiveFormats = (
|
|
|
|
'7z', 'ace', 'adf', 'alzip', 'ape', 'ar', 'arc', 'arj',
|
2012-11-19 21:27:54 +00:00
|
|
|
'bzip2', 'cab', 'chm', 'compress', 'cpio', 'deb', 'dms',
|
2013-02-27 18:38:07 +00:00
|
|
|
'flac', 'gzip', 'iso', 'lrzip', 'lzh', 'lzip', 'lzma', 'lzop',
|
2012-08-03 20:26:40 +00:00
|
|
|
'rar', 'rpm', 'rzip', 'shar', 'shn', 'tar', 'xz',
|
2012-05-17 12:28:12 +00:00
|
|
|
'zip', 'zoo')
|
2010-02-21 11:14:57 +00:00
|
|
|
|
2012-05-12 07:31:27 +00:00
|
|
|
# Supported compressions (used with tar for example)
|
|
|
|
# Note that all compressions must also be archive formats
|
|
|
|
ArchiveCompressions = ('bzip2', 'compress', 'gzip', 'lzip', 'lzma', 'xz')
|
2010-02-21 11:14:57 +00:00
|
|
|
|
|
|
|
# Map MIME types to archive format
|
|
|
|
ArchiveMimetypes = {
|
|
|
|
'application/java-archive': 'zip',
|
|
|
|
'application/rar': 'rar',
|
|
|
|
'application/vnd.ms-cab-compressed': 'cab',
|
2013-02-27 18:38:07 +00:00
|
|
|
'application/x-7z-compressed': '7z',
|
|
|
|
'application/x-ace': 'ace',
|
|
|
|
'application/x-adf': 'adf',
|
|
|
|
'application/x-alzip': 'alzip',
|
|
|
|
'application/x-archive': 'ar',
|
|
|
|
'application/x-arc': 'arc',
|
2010-02-21 11:14:57 +00:00
|
|
|
'application/x-arj': 'arj',
|
2013-02-27 18:38:07 +00:00
|
|
|
'application/x-bzip2': 'bzip2',
|
|
|
|
'application/x-cab': 'cab',
|
2012-11-19 21:27:54 +00:00
|
|
|
'application/x-chm': 'chm',
|
2013-02-27 18:38:07 +00:00
|
|
|
'application/x-compress': 'compress',
|
2010-02-21 11:14:57 +00:00
|
|
|
'application/x-cpio': 'cpio',
|
|
|
|
'application/x-debian-package': 'deb',
|
2013-02-27 18:38:07 +00:00
|
|
|
'application/x-dms': 'dms',
|
|
|
|
'application/x-gzip': 'gzip',
|
|
|
|
'application/x-iso9660-image': 'iso',
|
2010-02-21 11:14:57 +00:00
|
|
|
'application/x-lzop': 'lzop',
|
2010-02-22 16:34:08 +00:00
|
|
|
'application/x-lzma': 'lzma',
|
2010-03-04 15:42:37 +00:00
|
|
|
'application/x-lzip': 'lzip',
|
2010-03-06 18:24:34 +00:00
|
|
|
'application/x-lha': 'lzh',
|
2010-03-08 18:58:39 +00:00
|
|
|
'application/x-lrzip': 'lrzip',
|
2013-02-27 18:38:07 +00:00
|
|
|
'application/x-lzh': 'lzh',
|
|
|
|
'application/x-rar': 'rar',
|
|
|
|
'application/x-redhat-package-manager': 'rpm',
|
|
|
|
'application/x-rpm': 'rpm',
|
2010-03-20 10:34:40 +00:00
|
|
|
'application/x-rzip': 'rzip',
|
2012-05-25 20:07:22 +00:00
|
|
|
'application/x-shar': 'shar',
|
2013-02-27 18:38:07 +00:00
|
|
|
'application/x-tar': 'tar',
|
|
|
|
'application/x-xz': 'xz',
|
|
|
|
'application/x-zip-compressed': 'zip',
|
|
|
|
'application/x-zoo': 'zoo',
|
|
|
|
'application/zip': 'zip',
|
2012-05-17 08:43:21 +00:00
|
|
|
'audio/x-ape': 'ape',
|
2012-05-17 12:28:12 +00:00
|
|
|
'audio/x-shn': 'shn',
|
2012-05-17 12:59:33 +00:00
|
|
|
'audio/flac': 'flac',
|
2010-02-21 11:14:57 +00:00
|
|
|
}
|
|
|
|
|
2013-02-20 20:16:32 +00:00
|
|
|
try:
|
|
|
|
# use Python 3 lzma module if available
|
|
|
|
import lzma
|
|
|
|
py_lzma = ('py_lzma',)
|
|
|
|
except ImportError:
|
|
|
|
py_lzma = ()
|
|
|
|
|
2010-02-21 11:28:20 +00:00
|
|
|
# List of programs supporting the given archive format and command.
|
|
|
|
# If command is None, the program supports all commands (list, extract, ...)
|
2012-05-12 11:36:59 +00:00
|
|
|
# Programs starting with "py_" are Python modules.
|
2010-02-21 11:14:57 +00:00
|
|
|
ArchivePrograms = {
|
2010-03-06 14:47:00 +00:00
|
|
|
'ace': {
|
|
|
|
'extract': ('unace',),
|
|
|
|
'test': ('unace',),
|
|
|
|
'list': ('unace',),
|
|
|
|
},
|
2012-08-03 20:26:40 +00:00
|
|
|
'adf': {
|
|
|
|
'extract': ('unadf',),
|
|
|
|
'test': ('unadf',),
|
|
|
|
'list': ('unadf',),
|
|
|
|
},
|
2010-03-06 20:35:03 +00:00
|
|
|
'alzip': {
|
|
|
|
'extract': ('unalz',),
|
|
|
|
'test': ('unalz',),
|
|
|
|
'list': ('unalz',),
|
|
|
|
},
|
2012-05-17 08:43:21 +00:00
|
|
|
'ape': {
|
2012-05-17 09:01:06 +00:00
|
|
|
'create': ('mac',),
|
|
|
|
'extract': ('mac',),
|
|
|
|
'list': ('py_echo',),
|
|
|
|
'test': ('mac',),
|
2012-05-17 08:43:21 +00:00
|
|
|
},
|
2010-03-06 15:52:13 +00:00
|
|
|
'ar': {
|
|
|
|
None: ('ar',),
|
|
|
|
},
|
2010-03-08 17:28:11 +00:00
|
|
|
'arc': {
|
|
|
|
None: ('arc',),
|
2010-03-08 17:36:18 +00:00
|
|
|
'extract': ('nomarch',),
|
|
|
|
'test': ('nomarch',),
|
|
|
|
'list': ('nomarch',),
|
2010-03-08 17:28:11 +00:00
|
|
|
},
|
2010-02-21 11:14:57 +00:00
|
|
|
'bzip2': {
|
2012-05-24 21:10:05 +00:00
|
|
|
'extract': ('pbzip2', 'lbzip2', 'bzip2', '7z', '7za', 'py_bz2'),
|
|
|
|
'test': ('pbzip2', 'lbzip2', 'bzip2', '7z', '7za'),
|
2012-05-12 11:36:59 +00:00
|
|
|
'create': ('pbzip2', 'lbzip2', 'bzip2', 'py_bz2'),
|
2012-05-24 21:10:05 +00:00
|
|
|
'list': ('py_echo', '7z', '7za'),
|
2010-02-21 11:14:57 +00:00
|
|
|
},
|
2012-05-24 21:17:37 +00:00
|
|
|
'cab': {
|
2012-08-04 12:09:08 +00:00
|
|
|
'extract': ('cabextract', '7z'),
|
2012-05-24 21:17:37 +00:00
|
|
|
'create': ('lcab',),
|
|
|
|
'list': ('cabextract', '7z'),
|
|
|
|
'test': ('cabextract', '7z'),
|
|
|
|
},
|
2012-11-19 21:27:54 +00:00
|
|
|
'chm': {
|
2012-11-19 21:39:50 +00:00
|
|
|
'extract': ('archmage', 'extract_chmLib'),
|
2012-11-19 21:27:54 +00:00
|
|
|
'test': ('archmage',),
|
|
|
|
},
|
2012-05-17 12:59:33 +00:00
|
|
|
'flac': {
|
2012-05-17 19:22:56 +00:00
|
|
|
'extract': ('flac',),
|
|
|
|
'test': ('flac',),
|
|
|
|
'create': ('flac',),
|
|
|
|
'list': ('py_echo',),
|
2012-05-17 12:59:33 +00:00
|
|
|
},
|
2010-02-21 11:14:57 +00:00
|
|
|
'tar': {
|
2012-05-23 16:35:54 +00:00
|
|
|
None: ('tar', 'star', 'bsdtar', 'py_tarfile'),
|
2010-02-21 11:14:57 +00:00
|
|
|
},
|
|
|
|
'zip': {
|
2012-05-12 11:36:59 +00:00
|
|
|
None: ('7z', '7za', 'py_zipfile'),
|
2012-05-11 19:16:29 +00:00
|
|
|
'extract': ('unzip',),
|
|
|
|
'list': ('unzip',),
|
2012-11-18 19:43:04 +00:00
|
|
|
'test': ('zip', 'unzip',),
|
2012-05-11 19:16:29 +00:00
|
|
|
'create': ('zip',),
|
2010-02-21 11:14:57 +00:00
|
|
|
},
|
|
|
|
'gzip': {
|
2012-05-25 20:07:22 +00:00
|
|
|
None: ('7z', '7za', 'pigz', 'gzip'),
|
2012-05-12 11:36:59 +00:00
|
|
|
'extract': ('py_gzip',),
|
|
|
|
'create': ('py_gzip',),
|
2010-02-21 11:14:57 +00:00
|
|
|
},
|
2013-02-27 18:38:07 +00:00
|
|
|
'iso': {
|
|
|
|
'extract': ('7z',),
|
|
|
|
'list': ('7z', 'isoinfo'),
|
|
|
|
'test': ('7z',),
|
|
|
|
'create': ('genisoimage',),
|
|
|
|
},
|
2010-03-06 18:24:34 +00:00
|
|
|
'lzh': {
|
|
|
|
None: ('lha',),
|
2012-05-24 08:55:17 +00:00
|
|
|
'extract': ('lhasa',),
|
2010-03-06 18:24:34 +00:00
|
|
|
},
|
2010-03-04 15:42:37 +00:00
|
|
|
'lzip': {
|
2012-05-11 05:14:49 +00:00
|
|
|
'extract': ('plzip', 'lzip', 'clzip', 'pdlzip'),
|
2012-05-12 11:36:59 +00:00
|
|
|
'list': ('py_echo',),
|
2012-05-11 05:14:49 +00:00
|
|
|
'test': ('plzip', 'lzip', 'clzip', 'pdlzip'),
|
|
|
|
'create': ('plzip', 'lzip', 'clzip', 'pdlzip'),
|
2010-03-04 15:42:37 +00:00
|
|
|
},
|
2010-03-08 18:58:39 +00:00
|
|
|
'lrzip': {
|
|
|
|
'extract': ('lrzip',),
|
2012-05-12 11:36:59 +00:00
|
|
|
'list': ('py_echo',),
|
2010-03-08 18:58:39 +00:00
|
|
|
'test': ('lrzip',),
|
|
|
|
'create': ('lrzip',),
|
|
|
|
},
|
2010-02-21 11:14:57 +00:00
|
|
|
'compress': {
|
2010-10-04 17:21:01 +00:00
|
|
|
'extract': ('gzip', '7z', '7za', 'uncompress.real'),
|
2012-05-12 11:36:59 +00:00
|
|
|
'list': ('7z', '7za', 'py_echo',),
|
2010-10-04 17:21:01 +00:00
|
|
|
'test': ('gzip', '7z', '7za'),
|
2010-02-21 14:48:52 +00:00
|
|
|
'create': ('compress',),
|
2010-02-21 11:14:57 +00:00
|
|
|
},
|
|
|
|
'7z': {
|
2010-10-04 17:21:01 +00:00
|
|
|
None: ('7z', '7za'),
|
2010-02-21 11:14:57 +00:00
|
|
|
},
|
|
|
|
'rar': {
|
|
|
|
None: ('rar',),
|
|
|
|
'extract': ('unrar', '7z'),
|
|
|
|
'list': ('unrar', '7z'),
|
2010-02-21 12:40:42 +00:00
|
|
|
'test': ('unrar', '7z'),
|
2010-02-21 11:14:57 +00:00
|
|
|
},
|
|
|
|
'arj': {
|
2010-02-21 14:48:52 +00:00
|
|
|
None: ('arj',),
|
|
|
|
'extract': ('7z',),
|
|
|
|
'list': ('7z',),
|
|
|
|
'test': ('7z',),
|
2010-02-21 11:14:57 +00:00
|
|
|
},
|
|
|
|
'cpio': {
|
2012-05-23 16:22:16 +00:00
|
|
|
'extract': ('cpio', 'bsdcpio', '7z'),
|
|
|
|
'list': ('cpio', 'bsdcpio', '7z'),
|
|
|
|
'test': ('cpio', 'bsdcpio', '7z',),
|
|
|
|
'create': ('cpio', 'bsdcpio'),
|
2010-02-21 11:14:57 +00:00
|
|
|
},
|
|
|
|
'rpm': {
|
|
|
|
'extract': ('rpm2cpio', '7z'),
|
2010-10-04 17:21:01 +00:00
|
|
|
'list': ('rpm', '7z', '7za'),
|
2010-02-21 12:40:42 +00:00
|
|
|
'test': ('rpm', '7z'),
|
2010-02-21 11:14:57 +00:00
|
|
|
},
|
|
|
|
'deb': {
|
|
|
|
'extract': ('dpkg-deb', '7z'),
|
|
|
|
'list': ('dpkg-deb', '7z'),
|
2010-02-21 12:40:42 +00:00
|
|
|
'test': ('dpkg-deb', '7z'),
|
2010-02-21 11:14:57 +00:00
|
|
|
},
|
|
|
|
'lzop': {
|
|
|
|
None: ('lzop',),
|
|
|
|
},
|
2010-02-22 16:21:55 +00:00
|
|
|
'lzma': {
|
2013-02-27 19:20:02 +00:00
|
|
|
'extract': ('7z', 'lzma') + py_lzma,
|
|
|
|
'list': ('7z', 'py_echo'),
|
|
|
|
'test': ('7z', 'lzma'),
|
2013-02-20 20:16:32 +00:00
|
|
|
'create': ('lzma',) + py_lzma,
|
2010-02-22 16:21:55 +00:00
|
|
|
},
|
2010-03-20 10:34:40 +00:00
|
|
|
'rzip': {
|
|
|
|
'extract': ('rzip',),
|
2012-05-12 11:36:59 +00:00
|
|
|
'list': ('py_echo',),
|
2010-03-20 10:34:40 +00:00
|
|
|
'create': ('rzip',),
|
|
|
|
},
|
2012-05-25 20:07:22 +00:00
|
|
|
'shar': {
|
|
|
|
'create': ('shar',),
|
|
|
|
'extract': ('unshar',),
|
|
|
|
},
|
2012-05-17 12:28:12 +00:00
|
|
|
'shn': {
|
|
|
|
'extract': ('shorten',),
|
|
|
|
'list': ('py_echo',),
|
|
|
|
'create': ('shorten',),
|
|
|
|
},
|
2010-02-22 16:34:08 +00:00
|
|
|
'xz': {
|
2013-02-27 19:05:54 +00:00
|
|
|
None: ('xz', '7z'),
|
2013-02-22 17:36:45 +00:00
|
|
|
'extract': py_lzma,
|
|
|
|
'create': py_lzma,
|
2010-02-22 16:34:08 +00:00
|
|
|
},
|
2010-03-21 14:39:02 +00:00
|
|
|
'zoo': {
|
|
|
|
None: ('zoo',),
|
|
|
|
},
|
2011-01-24 20:28:29 +00:00
|
|
|
'dms': {
|
|
|
|
'extract': ('xdms',),
|
|
|
|
'list': ('xdms',),
|
|
|
|
'test': ('xdms',),
|
|
|
|
},
|
2010-02-21 11:14:57 +00:00
|
|
|
}
|
|
|
|
|
2010-10-04 17:21:01 +00:00
|
|
|
# List those programs that have different python module names because of
|
|
|
|
# Python module naming restrictions.
|
2010-02-21 11:14:57 +00:00
|
|
|
ProgramModules = {
|
|
|
|
'7z': 'p7zip',
|
2010-10-04 17:21:01 +00:00
|
|
|
'7za': 'p7azip',
|
2010-02-21 11:14:57 +00:00
|
|
|
'uncompress.real': 'uncompress',
|
|
|
|
'dpkg-deb': 'dpkg',
|
2012-11-19 21:39:50 +00:00
|
|
|
'extract_chmlib': 'chmlib',
|
2010-02-21 11:14:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def get_archive_format (filename):
|
2012-05-12 07:31:27 +00:00
|
|
|
"""Detect filename archive format and optional compression."""
|
|
|
|
mime, compression = util.guess_mime(filename)
|
|
|
|
if not (mime or compression):
|
2010-02-21 11:14:57 +00:00
|
|
|
raise util.PatoolError("unknown archive format for file `%s'" % filename)
|
|
|
|
if mime in ArchiveMimetypes:
|
|
|
|
format = ArchiveMimetypes[mime]
|
|
|
|
else:
|
|
|
|
raise util.PatoolError("unknown archive format for file `%s' (mime-type is `%s')" % (filename, mime))
|
2012-05-12 07:31:27 +00:00
|
|
|
if format == compression:
|
|
|
|
# file cannot be in same format compressed
|
|
|
|
compression = None
|
|
|
|
return format, compression
|
2010-02-21 11:14:57 +00:00
|
|
|
|
|
|
|
|
2012-05-12 07:31:27 +00:00
|
|
|
def check_archive_format (format, compression):
|
|
|
|
"""Make sure format and compression is known."""
|
2010-02-21 11:14:57 +00:00
|
|
|
if format not in ArchiveFormats:
|
|
|
|
raise util.PatoolError("unknown archive format `%s'" % format)
|
2012-05-12 07:31:27 +00:00
|
|
|
if compression is not None and compression not in ArchiveCompressions:
|
|
|
|
raise util.PatoolError("unkonwn archive compression `%s'" % compression)
|
2010-02-21 11:14:57 +00:00
|
|
|
|
|
|
|
|
2013-02-26 19:41:42 +00:00
|
|
|
def find_archive_program (format, command, program=None):
|
2010-02-21 11:14:57 +00:00
|
|
|
"""Find suitable archive program for given format and mode."""
|
2010-02-21 11:28:20 +00:00
|
|
|
commands = ArchivePrograms[format]
|
2010-02-21 11:14:57 +00:00
|
|
|
programs = []
|
2013-02-26 19:41:42 +00:00
|
|
|
if program is not None:
|
|
|
|
# try a specific program first
|
|
|
|
programs.append(program)
|
2010-02-21 11:14:57 +00:00
|
|
|
# first try the universal programs with key None
|
2010-02-21 11:28:20 +00:00
|
|
|
for key in (None, command):
|
|
|
|
if key in commands:
|
|
|
|
programs.extend(commands[key])
|
2010-02-21 14:48:52 +00:00
|
|
|
if not programs:
|
|
|
|
raise util.PatoolError("%s archive format `%s' is not supported" % (command, format))
|
2010-02-21 11:14:57 +00:00
|
|
|
# return the first existing program
|
|
|
|
for program in programs:
|
2012-05-12 11:36:59 +00:00
|
|
|
if program.startswith('py_'):
|
|
|
|
# it's a Python module and therefore always supported
|
|
|
|
return program
|
2010-03-01 15:10:17 +00:00
|
|
|
exe = util.find_program(program)
|
2010-02-21 11:14:57 +00:00
|
|
|
if exe:
|
2010-03-01 15:05:32 +00:00
|
|
|
if program == '7z' and format == 'rar' and not util.p7zip_supports_rar():
|
2010-02-23 18:31:14 +00:00
|
|
|
continue
|
2010-02-21 11:14:57 +00:00
|
|
|
return exe
|
|
|
|
# no programs found
|
2010-02-21 11:28:20 +00:00
|
|
|
raise util.PatoolError("could not find an executable program to %s format %s; candidates are (%s)," % (command, format, ",".join(programs)))
|
2010-02-21 11:14:57 +00:00
|
|
|
|
|
|
|
|
2012-05-17 07:08:28 +00:00
|
|
|
def program_supports_compression (program, compression):
|
|
|
|
"""Decide if the given program supports the compression natively.
|
|
|
|
@return: True iff the program supports the given compression format
|
|
|
|
natively, else False.
|
|
|
|
"""
|
2012-05-23 16:35:54 +00:00
|
|
|
if program in ('tar', 'star', 'bsdtar', 'py_tarfile'):
|
2013-02-26 19:41:42 +00:00
|
|
|
return compression in ('gzip', 'bzip2') + py_lzma
|
2012-05-17 07:08:28 +00:00
|
|
|
return False
|
2010-02-23 18:31:14 +00:00
|
|
|
|
|
|
|
|
2010-02-21 11:14:57 +00:00
|
|
|
def list_formats ():
|
2010-04-05 07:37:54 +00:00
|
|
|
"""Print information about available archive formats to stdout."""
|
2010-02-21 11:14:57 +00:00
|
|
|
for format in ArchiveFormats:
|
2012-11-19 19:58:42 +00:00
|
|
|
print(format, "files:")
|
2010-02-21 11:28:20 +00:00
|
|
|
for command in ArchiveCommands:
|
2010-02-22 20:55:16 +00:00
|
|
|
programs = ArchivePrograms[format]
|
|
|
|
if command not in programs and None not in programs:
|
2012-11-19 19:58:42 +00:00
|
|
|
print(" %8s: - (not supported)" % command)
|
2010-02-22 20:55:16 +00:00
|
|
|
continue
|
2010-02-21 15:04:09 +00:00
|
|
|
try:
|
|
|
|
program = find_archive_program(format, command)
|
2012-11-19 19:58:42 +00:00
|
|
|
print(" %8s: %s" % (command, program), end=' ')
|
2010-02-22 20:55:16 +00:00
|
|
|
if format == 'tar':
|
2012-05-12 07:31:27 +00:00
|
|
|
encs = [x for x in ArchiveCompressions if util.find_program(x)]
|
2010-02-22 20:55:16 +00:00
|
|
|
if encs:
|
2012-11-19 19:58:42 +00:00
|
|
|
print("(supported compressions: %s)" % ", ".join(encs), end=' ')
|
2010-02-22 20:55:16 +00:00
|
|
|
elif format == '7z':
|
2010-03-01 15:05:32 +00:00
|
|
|
if util.p7zip_supports_rar():
|
2012-11-19 19:58:42 +00:00
|
|
|
print("(rar archives supported)", end=' ')
|
2010-02-22 20:55:16 +00:00
|
|
|
else:
|
2012-11-19 19:58:42 +00:00
|
|
|
print("(rar archives not supported)", end=' ')
|
|
|
|
print()
|
2010-02-21 15:04:09 +00:00
|
|
|
except util.PatoolError:
|
2010-03-01 15:14:33 +00:00
|
|
|
handlers = programs.get(None, programs.get(command))
|
2012-11-19 19:58:42 +00:00
|
|
|
print(" %8s: - (no program found; install %s)" %
|
|
|
|
(command, util.strlist_with_or(handlers)))
|
2010-02-21 11:14:57 +00:00
|
|
|
|
|
|
|
|
2013-02-26 19:41:42 +00:00
|
|
|
def check_program_compression(archive, command, program, compression):
|
|
|
|
"""Check if a program supports the given compression."""
|
|
|
|
program = os.path.basename(program)
|
2012-05-17 07:08:28 +00:00
|
|
|
if compression:
|
|
|
|
# check if compression is supported
|
|
|
|
if not program_supports_compression(program, compression):
|
|
|
|
if command == 'create':
|
|
|
|
comp_command = command
|
|
|
|
else:
|
|
|
|
comp_command = 'extract'
|
|
|
|
comp_prog = find_archive_program(compression, comp_command)
|
|
|
|
if not comp_prog:
|
|
|
|
msg = "cannot %s archive `%s': compression `%s' not supported"
|
|
|
|
raise util.PatoolError(msg % (command, archive, compression))
|
2010-02-21 11:14:57 +00:00
|
|
|
|
|
|
|
|
2010-03-06 10:33:44 +00:00
|
|
|
def move_outdir_orphan (outdir):
|
2010-02-21 11:14:57 +00:00
|
|
|
"""Move a single file or directory inside outdir a level up.
|
2010-03-06 10:33:44 +00:00
|
|
|
Never overwrite files.
|
2010-02-21 11:14:57 +00:00
|
|
|
Return (True, outfile) if successful, (False, reason) if not."""
|
|
|
|
entries = os.listdir(outdir)
|
|
|
|
if len(entries) == 1:
|
|
|
|
src = os.path.join(outdir, entries[0])
|
|
|
|
dst = os.path.join(os.path.dirname(outdir), entries[0])
|
|
|
|
if os.path.exists(dst):
|
2010-03-06 10:33:44 +00:00
|
|
|
return (False, "local file exists")
|
2010-02-21 11:14:57 +00:00
|
|
|
shutil.move(src, dst)
|
|
|
|
os.rmdir(outdir)
|
|
|
|
return (True, entries[0])
|
|
|
|
return (False, "multiple files in root")
|
|
|
|
|
|
|
|
|
2013-02-25 20:04:02 +00:00
|
|
|
def run_archive_cmdlist (archive_cmdlist, verbosity=0):
|
2010-03-01 15:03:31 +00:00
|
|
|
"""Run archive command."""
|
2010-02-21 11:28:20 +00:00
|
|
|
# archive_cmdlist is a command list with optional keyword arguments
|
|
|
|
if isinstance(archive_cmdlist, tuple):
|
|
|
|
cmdlist, runkwargs = archive_cmdlist
|
2010-02-21 11:14:57 +00:00
|
|
|
else:
|
2010-02-21 11:28:20 +00:00
|
|
|
cmdlist, runkwargs = archive_cmdlist, {}
|
2013-02-25 20:04:02 +00:00
|
|
|
return util.run_checked(cmdlist, verbosity=verbosity, **runkwargs)
|
2010-02-21 11:14:57 +00:00
|
|
|
|
|
|
|
|
2010-03-06 14:23:16 +00:00
|
|
|
def make_file_readable (filename):
|
|
|
|
"""Make file user readable if it is not a link."""
|
|
|
|
if not os.path.islink(filename):
|
|
|
|
util.set_mode(filename, stat.S_IRUSR)
|
|
|
|
|
|
|
|
|
|
|
|
def make_dir_readable (filename):
|
|
|
|
"""Make directory user readable and executable."""
|
|
|
|
util.set_mode(filename, stat.S_IRUSR|stat.S_IXUSR)
|
|
|
|
|
|
|
|
|
|
|
|
def make_user_readable (directory):
|
|
|
|
"""Make all files in given directory user readable. Also recurse into
|
|
|
|
subdirectories."""
|
|
|
|
for root, dirs, files in os.walk(directory, onerror=util.log_error):
|
|
|
|
for filename in files:
|
|
|
|
make_file_readable(os.path.join(root, filename))
|
|
|
|
for dirname in dirs:
|
|
|
|
make_dir_readable(os.path.join(root, dirname))
|
|
|
|
|
|
|
|
|
2012-08-03 21:11:11 +00:00
|
|
|
def cleanup_outdir (outdir, archive):
|
2010-03-11 15:02:08 +00:00
|
|
|
"""Cleanup outdir after extraction and return target file name and
|
|
|
|
result string."""
|
2010-03-06 14:23:16 +00:00
|
|
|
make_user_readable(outdir)
|
2010-03-11 15:02:08 +00:00
|
|
|
# move single directory or file in outdir
|
|
|
|
(success, msg) = move_outdir_orphan(outdir)
|
|
|
|
if success:
|
|
|
|
# msg is a single directory or filename
|
|
|
|
return msg, "`%s'" % msg
|
|
|
|
# outdir remains unchanged
|
2012-08-03 21:11:11 +00:00
|
|
|
# rename it to something more user-friendly (basically the archive
|
|
|
|
# name without extension)
|
|
|
|
outdir2 = util.get_single_outfile("", archive)
|
|
|
|
os.rename(outdir, outdir2)
|
|
|
|
return outdir2, "`%s' (%s)" % (outdir2, msg)
|
2010-02-21 11:14:57 +00:00
|
|
|
|
|
|
|
|
2013-02-26 19:41:42 +00:00
|
|
|
def _extract_archive(archive, verbosity=0, outdir=None, program=None, format=None, compression=None):
|
|
|
|
"""Extract an archive.
|
2012-05-24 21:10:05 +00:00
|
|
|
@return: output directory if command is 'extract', else None
|
|
|
|
"""
|
2010-03-03 16:35:17 +00:00
|
|
|
if format is None:
|
2012-05-12 07:31:27 +00:00
|
|
|
format, compression = get_archive_format(archive)
|
|
|
|
check_archive_format(format, compression)
|
2013-02-26 19:41:42 +00:00
|
|
|
program = find_archive_program(format, 'extract', program=program)
|
|
|
|
check_program_compression(archive, 'extract', program, compression)
|
|
|
|
get_archive_cmdlist = get_archive_cmdlist_func(program, 'extract', format)
|
|
|
|
if outdir is None:
|
|
|
|
outdir = util.tmpdir(dir=".")
|
|
|
|
do_cleanup_outdir = True
|
|
|
|
else:
|
|
|
|
do_cleanup_outdir = False
|
2010-02-21 11:14:57 +00:00
|
|
|
try:
|
2013-02-26 19:41:42 +00:00
|
|
|
cmdlist = get_archive_cmdlist(archive, compression, program, verbosity, outdir)
|
2012-05-11 20:08:00 +00:00
|
|
|
if cmdlist:
|
|
|
|
# an empty command list means the get_archive_cmdlist() function
|
2012-05-24 21:10:05 +00:00
|
|
|
# already handled the command (eg. when it's a builtin Python
|
2012-05-11 20:08:00 +00:00
|
|
|
# function)
|
2013-02-26 19:41:42 +00:00
|
|
|
run_archive_cmdlist(cmdlist, verbosity=verbosity)
|
|
|
|
if do_cleanup_outdir:
|
|
|
|
target, msg = cleanup_outdir(outdir, archive)
|
|
|
|
else:
|
|
|
|
target, msg = outdir, "`%s'" % outdir
|
|
|
|
if verbosity >= 0:
|
|
|
|
util.log_info("... %s extracted to %s." % (archive, msg))
|
|
|
|
return target
|
2010-02-21 11:14:57 +00:00
|
|
|
finally:
|
2013-02-26 19:41:42 +00:00
|
|
|
# try to remove an empty temporary output directory
|
|
|
|
if do_cleanup_outdir:
|
2010-02-21 11:14:57 +00:00
|
|
|
try:
|
2013-02-26 19:41:42 +00:00
|
|
|
os.rmdir(outdir)
|
2010-02-21 11:14:57 +00:00
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2013-02-26 19:41:42 +00:00
|
|
|
|
|
|
|
def _create_archive(archive, filenames, verbosity=0, program=None, format=None, compression=None):
|
|
|
|
"""Create an archive."""
|
|
|
|
if format is None:
|
|
|
|
format, compression = get_archive_format(archive)
|
|
|
|
check_archive_format(format, compression)
|
|
|
|
program = find_archive_program(format, 'create', program=program)
|
|
|
|
check_program_compression(archive, 'create', program, compression)
|
|
|
|
get_archive_cmdlist = get_archive_cmdlist_func(program, 'create', format)
|
|
|
|
origarchive = None
|
|
|
|
if os.path.basename(program) == 'arc' and \
|
|
|
|
".arc" in archive and not archive.endswith(".arc"):
|
|
|
|
# the arc program mangles the archive name if it contains ".arc"
|
|
|
|
origarchive = archive
|
|
|
|
archive = util.tmpfile(dir=os.path.dirname(archive), suffix=".arc")
|
|
|
|
cmdlist = get_archive_cmdlist(archive, compression, program, verbosity, filenames)
|
|
|
|
if cmdlist:
|
|
|
|
# an empty command list means the get_archive_cmdlist() function
|
|
|
|
# already handled the command (eg. when it's a builtin Python
|
|
|
|
# function)
|
|
|
|
run_archive_cmdlist(cmdlist, verbosity=verbosity)
|
|
|
|
if origarchive:
|
|
|
|
shutil.move(archive, origarchive)
|
|
|
|
|
|
|
|
|
|
|
|
def _handle_archive (archive, command, verbosity=0, program=None, format=None, compression=None):
|
|
|
|
"""Test and list archives."""
|
|
|
|
if format is None:
|
|
|
|
format, compression = get_archive_format(archive)
|
|
|
|
check_archive_format(format, compression)
|
|
|
|
if command not in ('list', 'test'):
|
|
|
|
raise util.PatoolError("invalid archive command `%s'" % command)
|
|
|
|
program = find_archive_program(format, command, program=program)
|
|
|
|
check_program_compression(archive, command, program, compression)
|
|
|
|
get_archive_cmdlist = get_archive_cmdlist_func(program, command, format)
|
|
|
|
# prepare keyword arguments for command list
|
|
|
|
cmdlist = get_archive_cmdlist(archive, compression, program, verbosity)
|
|
|
|
if cmdlist:
|
|
|
|
# an empty command list means the get_archive_cmdlist() function
|
|
|
|
# already handled the command (eg. when it's a builtin Python
|
|
|
|
# function)
|
|
|
|
run_archive_cmdlist(cmdlist, verbosity=verbosity)
|
|
|
|
|
|
|
|
|
2011-10-19 07:10:17 +00:00
|
|
|
def get_archive_cmdlist_func (program, command, format):
|
2012-12-17 17:14:58 +00:00
|
|
|
"""Get the Python function that executes the given program."""
|
2011-10-19 07:10:17 +00:00
|
|
|
# get python module for given archive program
|
|
|
|
key = util.stripext(os.path.basename(program).lower())
|
2012-11-19 19:58:42 +00:00
|
|
|
modulename = ".programs." + ProgramModules.get(key, key)
|
|
|
|
# import the module
|
2011-10-19 07:10:17 +00:00
|
|
|
try:
|
2012-11-19 19:58:42 +00:00
|
|
|
module = importlib.import_module(modulename, __name__)
|
|
|
|
except ImportError as msg:
|
|
|
|
raise util.PatoolError(msg)
|
|
|
|
# get archive handler function (eg. patoolib.programs.star.extract_tar)
|
|
|
|
try:
|
|
|
|
return getattr(module, '%s_%s' % (command, format))
|
|
|
|
except AttributeError as msg:
|
|
|
|
raise util.PatoolError(msg)
|
2011-10-19 07:10:17 +00:00
|
|
|
|
|
|
|
|
2010-04-05 07:37:54 +00:00
|
|
|
def rmtree_log_error (func, path, exc):
|
2010-04-05 00:56:59 +00:00
|
|
|
"""Error function for shutil.rmtree(). Raises a PatoolError."""
|
2010-04-05 00:39:40 +00:00
|
|
|
msg = "Error in %s(%s): %s" % (func.__name__, path, str(exc[1]))
|
2010-04-05 07:37:54 +00:00
|
|
|
util.log_error(msg)
|
2010-04-05 00:39:40 +00:00
|
|
|
|
|
|
|
|
2013-02-25 20:04:02 +00:00
|
|
|
def _diff_archives (archive1, archive2, verbosity=0):
|
|
|
|
"""Show differences between two archives.
|
|
|
|
@return 0 if archives are the same, else 1
|
|
|
|
@raises: PatoolError on errors
|
|
|
|
"""
|
2012-05-11 18:07:50 +00:00
|
|
|
if util.is_same_file(archive1, archive2):
|
|
|
|
return 0
|
2010-03-11 15:02:08 +00:00
|
|
|
diff = util.find_program("diff")
|
|
|
|
if not diff:
|
2012-05-11 18:07:50 +00:00
|
|
|
msg = "The diff(1) program is required for showing archive differences, please install it."
|
|
|
|
raise util.PatoolError(msg)
|
2010-03-11 16:15:03 +00:00
|
|
|
tmpdir1 = util.tmpdir()
|
2010-03-11 15:02:08 +00:00
|
|
|
try:
|
2013-02-26 19:41:42 +00:00
|
|
|
path1 = _extract_archive(archive1, outdir=tmpdir1, verbosity=-1)
|
2012-05-11 18:19:29 +00:00
|
|
|
tmpdir2 = util.tmpdir()
|
|
|
|
try:
|
2013-02-26 19:41:42 +00:00
|
|
|
path2 = _extract_archive(archive2, outdir=tmpdir2, verbosity=-1)
|
2013-02-25 20:04:02 +00:00
|
|
|
return util.run_checked([diff, "-urN", path1, path2], verbosity=1, ret_ok=(0, 1))
|
2012-05-11 18:19:29 +00:00
|
|
|
finally:
|
|
|
|
shutil.rmtree(tmpdir2, onerror=rmtree_log_error)
|
2010-03-11 15:02:08 +00:00
|
|
|
finally:
|
2010-04-05 07:37:54 +00:00
|
|
|
shutil.rmtree(tmpdir1, onerror=rmtree_log_error)
|
2010-03-11 17:33:58 +00:00
|
|
|
|
|
|
|
|
2013-02-25 20:04:02 +00:00
|
|
|
def _search_archive(pattern, archive, verbosity=0):
|
|
|
|
"""Search for given pattern in an archive."""
|
2013-02-22 17:38:52 +00:00
|
|
|
grep = util.find_program("grep")
|
|
|
|
if not grep:
|
|
|
|
msg = "The grep(1) program is required for searching archive contents, please install it."
|
|
|
|
raise util.PatoolError(msg)
|
|
|
|
tmpdir = util.tmpdir()
|
|
|
|
try:
|
2013-02-26 19:41:42 +00:00
|
|
|
path = _extract_archive(archive, outdir=tmpdir, verbosity=-1)
|
2013-02-25 20:04:02 +00:00
|
|
|
return util.run_checked([grep, "-r", "-e", pattern, "."], ret_ok=(0, 1), verbosity=1, cwd=path)
|
2013-02-22 17:38:52 +00:00
|
|
|
finally:
|
|
|
|
shutil.rmtree(tmpdir, onerror=rmtree_log_error)
|
|
|
|
|
|
|
|
|
2013-02-25 20:04:02 +00:00
|
|
|
def _repack_archive (archive1, archive2, verbosity=0):
|
2010-03-11 17:33:58 +00:00
|
|
|
"""Repackage an archive to a different format."""
|
2013-02-21 16:51:34 +00:00
|
|
|
format1, compression1 = get_archive_format(archive1)
|
|
|
|
format2, compression2 = get_archive_format(archive2)
|
|
|
|
if format1 == format2 and compression1 == compression2:
|
|
|
|
# same format and compression allows to copy the file
|
2013-02-25 20:04:02 +00:00
|
|
|
util.link_or_copy(archive1, archive2, verbosity=verbosity)
|
|
|
|
return
|
2010-03-11 17:33:58 +00:00
|
|
|
tmpdir = util.tmpdir()
|
|
|
|
try:
|
2013-02-25 20:04:02 +00:00
|
|
|
kwargs = dict(verbosity=verbosity, outdir=tmpdir)
|
2013-02-21 16:51:34 +00:00
|
|
|
same_format = (format1 == format2 and compression1 and compression2)
|
|
|
|
if same_format:
|
|
|
|
# only decompress since the format is the same
|
|
|
|
kwargs['format'] = compression1
|
2013-02-26 19:41:42 +00:00
|
|
|
path = _extract_archive(archive1, **kwargs)
|
2010-03-11 17:33:58 +00:00
|
|
|
archive = os.path.abspath(archive2)
|
2013-02-25 20:04:02 +00:00
|
|
|
files = tuple(os.listdir(path))
|
2013-02-21 16:51:34 +00:00
|
|
|
olddir = os.getcwd()
|
2013-02-25 20:04:02 +00:00
|
|
|
os.chdir(path)
|
2013-02-21 16:51:34 +00:00
|
|
|
try:
|
2013-02-26 19:41:42 +00:00
|
|
|
kwargs = dict(verbosity=verbosity)
|
2013-02-21 16:51:34 +00:00
|
|
|
if same_format:
|
|
|
|
# only compress since the format is the same
|
|
|
|
kwargs['format'] = compression2
|
2013-02-26 19:41:42 +00:00
|
|
|
_create_archive(archive, files, **kwargs)
|
2013-02-21 16:51:34 +00:00
|
|
|
finally:
|
|
|
|
os.chdir(olddir)
|
2010-03-11 17:33:58 +00:00
|
|
|
finally:
|
2010-04-05 07:37:54 +00:00
|
|
|
shutil.rmtree(tmpdir, onerror=rmtree_log_error)
|
2012-04-09 10:23:02 +00:00
|
|
|
|
|
|
|
|
2013-02-25 20:04:02 +00:00
|
|
|
# the patool library API
|
2012-04-09 10:23:02 +00:00
|
|
|
|
2013-02-25 20:04:02 +00:00
|
|
|
def extract_archive(archive, verbosity=0, outdir=None, program=None):
|
2012-04-09 10:23:02 +00:00
|
|
|
"""Extract given archive."""
|
2013-02-25 20:04:02 +00:00
|
|
|
util.check_existing_filename(archive)
|
|
|
|
if verbosity >= 0:
|
|
|
|
util.log_info("Extracting %s ..." % archive)
|
2013-02-26 19:41:42 +00:00
|
|
|
return _extract_archive(archive, verbosity=verbosity, outdir=outdir, program=program)
|
2012-04-09 10:23:02 +00:00
|
|
|
|
|
|
|
|
2013-02-26 19:41:42 +00:00
|
|
|
def list_archive(archive, verbosity=1, program=None):
|
2012-04-09 10:23:02 +00:00
|
|
|
"""List given archive."""
|
2013-02-26 19:41:42 +00:00
|
|
|
# Set default verbosity to 1 since the listing output should be visible.
|
2013-02-25 20:04:02 +00:00
|
|
|
util.check_existing_filename(archive)
|
|
|
|
if verbosity >= 0:
|
|
|
|
util.log_info("Listing %s ..." % archive)
|
|
|
|
return _handle_archive(archive, 'list', verbosity=verbosity, program=program)
|
2012-04-09 10:23:02 +00:00
|
|
|
|
|
|
|
|
2013-02-25 20:04:02 +00:00
|
|
|
def test_archive(archive, verbosity=0, program=None):
|
2012-04-09 10:23:02 +00:00
|
|
|
"""Test given archive."""
|
2013-02-25 20:04:02 +00:00
|
|
|
util.check_existing_filename(archive)
|
|
|
|
if verbosity >= 0:
|
|
|
|
util.log_info("Testing %s ..." % archive)
|
|
|
|
res = _handle_archive(archive, 'test', verbosity=verbosity, program=program)
|
|
|
|
if verbosity >= 0:
|
|
|
|
util.log_info("... tested ok.")
|
|
|
|
return res
|
2012-04-09 10:23:02 +00:00
|
|
|
|
|
|
|
|
2013-02-25 20:04:02 +00:00
|
|
|
def create_archive(archive, filenames, verbosity=0, program=None):
|
2012-04-09 10:23:02 +00:00
|
|
|
"""Create given archive with given files."""
|
2013-02-25 20:04:02 +00:00
|
|
|
util.check_new_filename(archive)
|
|
|
|
util.check_archive_filelist(filenames)
|
|
|
|
if verbosity >= 0:
|
|
|
|
util.log_info("Creating %s ..." % archive)
|
2013-02-26 19:41:42 +00:00
|
|
|
res = _create_archive(archive, filenames, verbosity=verbosity, program=program)
|
2013-02-25 20:04:02 +00:00
|
|
|
if verbosity >= 0:
|
|
|
|
util.log_info("... %s created." % archive)
|
|
|
|
return res
|
2012-04-09 10:23:02 +00:00
|
|
|
|
|
|
|
|
2013-02-25 20:04:02 +00:00
|
|
|
def diff_archives(archive1, archive2, verbosity=0):
|
2012-04-09 10:23:02 +00:00
|
|
|
"""Print differences between two archives."""
|
2013-02-25 20:04:02 +00:00
|
|
|
util.check_existing_filename(archive1)
|
|
|
|
util.check_existing_filename(archive2)
|
|
|
|
if verbosity >= 0:
|
|
|
|
util.log_info("Comparing %s with %s ..." % (archive1, archive2))
|
|
|
|
res = _diff_archives(archive1, archive2, verbosity=verbosity)
|
|
|
|
if res == 0 and verbosity >= 0:
|
|
|
|
util.log_info("... no differences found.")
|
2012-04-09 10:23:02 +00:00
|
|
|
|
|
|
|
|
2013-02-25 20:04:02 +00:00
|
|
|
def search_archive(pattern, archive, verbosity=0):
|
2013-02-22 17:38:52 +00:00
|
|
|
"""Search pattern in archive members."""
|
2013-02-25 20:04:02 +00:00
|
|
|
if not pattern:
|
|
|
|
raise util.PatoolError("empty search pattern")
|
|
|
|
util.check_existing_filename(archive)
|
|
|
|
if verbosity >= 0:
|
|
|
|
util.log_info("Searching %r in %s ..." % (pattern, archive))
|
|
|
|
res = _search_archive(pattern, archive, verbosity=verbosity)
|
|
|
|
if res == 1 and verbosity >= 0:
|
|
|
|
util.log_info("... %r not found" % pattern)
|
|
|
|
return res
|
2013-02-22 17:38:52 +00:00
|
|
|
|
|
|
|
|
2013-02-25 20:04:02 +00:00
|
|
|
def repack_archive (archive, archive_new, verbosity=0):
|
2012-11-19 21:27:54 +00:00
|
|
|
"""Repack archive to different file and/or format."""
|
2013-02-25 20:04:02 +00:00
|
|
|
util.check_existing_filename(archive)
|
|
|
|
util.check_new_filename(archive_new)
|
|
|
|
if verbosity >= 0:
|
|
|
|
util.log_info("Repacking %s to %s ..." % (archive, archive_new))
|
|
|
|
res = _repack_archive(archive, archive_new, verbosity=verbosity)
|
|
|
|
if verbosity >= 0:
|
|
|
|
util.log_info("... repacking successful.")
|
|
|
|
return res
|