patool/patool

93 lines
3.0 KiB
Python
Executable File

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2010 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/>.
"""
patool [extract|list|create|formats] [sub-command-options] <command-args>
"""
import sys
if not hasattr(sys, "version_info") or sys.version_info < (2, 4, 0, "final", 0):
raise SystemExit("This program requires Python 2.4 or later.")
import os
from patoolib import handle_archive, list_formats, baker
# parameter help and short options
params = {
"verbose": "Verbose operation (if the helper application supports it).",
}
shortopts = {"verbose": "v"}
def handle_multi_archive(archives, cmd, **kwargs):
"""Handle a multi-archive command."""
res = 0
archives = [x for x in archives if os.path.exists(x)]
if archives:
for archive in archives:
newres = handle_archive(archive, cmd, **kwargs)
# return error if one of the archives could not be extracted
if newres:
res = newres
else:
print >>sys.stderr, "patool error: none of the given archive files exist"
res = 1
return res
@baker.command(default=True, shortopts=shortopts, params=params)
def extract (archive, *archives, **kwargs):
"""Extract files from archive(s)."""
return handle_multi_archive((archive,)+archives, 'extract', **kwargs)
@baker.command(shortopts=shortopts, params=params)
def list (archive, *archives, **kwargs):
"""List files in archive(s)."""
return handle_multi_archive((archive,)+archives, 'list', **kwargs)
@baker.command(shortopts=shortopts, params=params)
def test (archive, *archives, **kwargs):
"""Test files in archive(s)."""
return handle_multi_archive((archive,)+archives, 'test', **kwargs)
@baker.command(shortopts=shortopts, params=params)
def create (archive, file1, *files, **kwargs):
"""Create an archive from given files."""
allfiles = (file1,)+files
return handle_archive(archive, 'create', *allfiles, **kwargs)
@baker.command
def diff (archive1, archive2):
"""Show differences between two archives."""
return handle_archive(archive1, "diff", archive2)
@baker.command
def repack (archive1, archive2):
"""Repackage one archive in another format."""
return handle_archive(archive1, "repack", archive2)
@baker.command
def formats ():
"""List supported and available archive formats."""
return list_formats()
try:
sys.exit(baker.run())
except baker.CommandError, msg:
print >>sys.stderr, "patool error:", msg
sys.exit(1)