2
|
1 |
#!/usr/bin/python2.6
|
|
2 |
#
|
|
3 |
# CDDL HEADER START
|
|
4 |
#
|
|
5 |
# The contents of this file are subject to the terms of the
|
|
6 |
# Common Development and Distribution License (the "License").
|
|
7 |
# You may not use this file except in compliance with the License.
|
|
8 |
#
|
|
9 |
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
|
|
10 |
# or http://www.opensolaris.org/os/licensing.
|
|
11 |
# See the License for the specific language governing permissions
|
|
12 |
# and limitations under the License.
|
|
13 |
#
|
|
14 |
# When distributing Covered Code, include this CDDL HEADER in each
|
|
15 |
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
|
|
16 |
# If applicable, add the following below this CDDL HEADER, with the
|
|
17 |
# fields enclosed by brackets "[]" replaced with your own identifying
|
|
18 |
# information: Portions Copyright [yyyy] [name of copyright owner]
|
|
19 |
#
|
|
20 |
# CDDL HEADER END
|
|
21 |
#
|
|
22 |
# Copyright (c) 2010, Oracle and/or it's affiliates. All rights reserved.
|
|
23 |
#
|
|
24 |
#
|
|
25 |
# unpack.py - an archive unpack utility
|
|
26 |
#
|
|
27 |
# A simple program to uncompress and unpack source archive files into a target
|
|
28 |
# directory and fix permissions if requested.
|
|
29 |
#
|
|
30 |
|
|
31 |
import os
|
|
32 |
import sys
|
|
33 |
|
|
34 |
def uncompress_unpack_commands(filename, verbose=False):
|
|
35 |
import re
|
|
36 |
|
|
37 |
uncompress = "/bin/cat"
|
|
38 |
|
|
39 |
if (re.search("(\.bz2|\.tbz|\.tbz2)$", filename) != None):
|
|
40 |
uncompress = "/usr/bin/bzip2 -dc"
|
|
41 |
elif (re.search("(\.gz|\.tgz)$", filename) != None):
|
|
42 |
uncompress = "/usr/bin/gzip -dc"
|
|
43 |
elif (re.search("(\.Z)$", filename) != None):
|
|
44 |
uncompress = "/usr/bin/uncompress -c"
|
|
45 |
elif (re.search("(\.7z)$", filename) != None):
|
|
46 |
uncompress = "/usr/bin/7z --s"
|
|
47 |
elif (re.search("(\.zip)$", filename) != None):
|
|
48 |
uncompress = "/usr/bin/unzip -qo"
|
|
49 |
|
|
50 |
unpack = " | gtar -xf -"
|
|
51 |
|
|
52 |
if (re.search("(\.zip)$", filename) != None):
|
|
53 |
unpack = ""
|
|
54 |
elif (re.search("(\.jar)$", filename) != None):
|
|
55 |
unpack = " | jar xf -"
|
|
56 |
|
|
57 |
if (verbose == True):
|
|
58 |
print "command: %s %s %s" % (uncompress, filename, unpack)
|
|
59 |
|
|
60 |
return uncompress, unpack
|
|
61 |
|
|
62 |
#
|
|
63 |
# recurse down a directory tree opening permissions so that others may access
|
|
64 |
# files in the tree.
|
|
65 |
#
|
|
66 |
def fixup_permissions(dir, verbose):
|
|
67 |
for entry in os.listdir(dir):
|
|
68 |
import stat
|
|
69 |
|
|
70 |
path = "%s/%s" % (dir, entry)
|
|
71 |
|
|
72 |
st = os.lstat(path)
|
|
73 |
mode = stat.S_IMODE(st.st_mode)
|
|
74 |
mode |= (stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
|
|
75 |
if stat.S_ISDIR(st.st_mode):
|
|
76 |
mode |= (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
|
|
77 |
|
|
78 |
if (stat.S_IMODE(st.st_mode) != mode):
|
|
79 |
if (verbose == True):
|
|
80 |
print "Changing %s from %4.4o to %4.4o" % (path,
|
|
81 |
stat.S_IMODE(st.st_mode), mode)
|
|
82 |
os.chmod(path, mode)
|
|
83 |
|
|
84 |
if stat.S_ISDIR(st.st_mode):
|
|
85 |
fixup_permissions(path, verbose)
|
|
86 |
|
|
87 |
|
|
88 |
def usage():
|
|
89 |
print "Usage: %s [-v|--verbose] [-f|--fix-permissions] [-r|--relocate-to (dir)] (file)" % (sys.argv[0].split('/')[-1])
|
|
90 |
sys.exit(1)
|
|
91 |
|
|
92 |
def main():
|
|
93 |
import getopt
|
|
94 |
import sys
|
|
95 |
import tempfile
|
|
96 |
|
|
97 |
verbose = False
|
|
98 |
permissions = None
|
|
99 |
relocate_to = None
|
|
100 |
|
|
101 |
try:
|
|
102 |
opts, args = getopt.getopt(sys.argv[1:], "fr:v",
|
|
103 |
["fix-permissions", "relocate-to=", "verbose"])
|
|
104 |
except getopt.GetoptError, err:
|
|
105 |
print str(err)
|
|
106 |
usage()
|
|
107 |
|
|
108 |
for opt, arg in opts:
|
|
109 |
if opt in [ "-v", "--verbose" ]:
|
|
110 |
verbose = True
|
|
111 |
elif opt in [ "-f", "--fix-permissions" ]:
|
|
112 |
permissions = True
|
|
113 |
elif opt in [ "-r", "--relocate-to" ]:
|
|
114 |
relocate_to = arg
|
|
115 |
else:
|
|
116 |
assert False, "unknown option"
|
|
117 |
|
|
118 |
filename = ((args[0] == '/') and "%s" or "../%s") % args[0]
|
|
119 |
uncompress, unpack = uncompress_unpack_commands(filename)
|
|
120 |
tempdir = tempfile.mkdtemp(dir='.')
|
|
121 |
|
|
122 |
# extract the archive contents
|
|
123 |
if (verbose == True):
|
|
124 |
print "cd %s ; %s %s%s" % (tempdir, uncompress, filename,
|
|
125 |
unpack)
|
|
126 |
os.system("cd %s ; %s %s%s" % (tempdir, uncompress, filename, unpack))
|
|
127 |
|
|
128 |
# open up the permissions on what we extracted
|
|
129 |
if permissions:
|
|
130 |
fixup_permissions(tempdir, verbose)
|
|
131 |
|
|
132 |
if (relocate_to == None):
|
|
133 |
# move everything in the tempdir here
|
|
134 |
for entry in os.listdir(tempdir):
|
|
135 |
path= "%s/%s" % (tempdir, entry)
|
|
136 |
os.renames(path, entry)
|
|
137 |
else:
|
|
138 |
# rename the tempdir and open it's permissions
|
|
139 |
os.renames(tempdir, relocate_to)
|
|
140 |
os.chmod(relocate_to, 0755)
|
|
141 |
|
|
142 |
|
|
143 |
if __name__ == "__main__":
|
|
144 |
main()
|