-
Notifications
You must be signed in to change notification settings - Fork 3
/
patool
executable file
·102 lines (84 loc) · 3.31 KB
/
patool
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 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
# 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
for archive in archives:
if not os.path.isfile(archive):
res = 1
msg = "archive %r is not a file" % archive
print >>sys.stderr, "patool error:", msg
else:
newres = handle_archive(archive, cmd, **kwargs)
# return error if one of the archives could not be extracted
if newres:
res = newres
return res
extract_params = {
"outdir": "Extract to given directory.",
}
extract_params.update(params)
@baker.command(default=True, shortopts=shortopts, params=extract_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(shortopts=shortopts, params=params)
def diff (archive1, archive2, **kwargs):
"""Show differences between two archives."""
from patoolib import diff
return diff(archive1, archive2, **kwargs)
@baker.command(shortopts=shortopts, params=params)
def repack (archive1, archive2, **kwargs):
"""Repackage one archive in another format."""
from patoolib import repack
return repack(archive1, archive2, **kwargs)
@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
baker.help(sys.argv[0])
sys.exit(1)