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.
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.
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]
22 # Copyright (c) 2010, Oracle and/or it's affiliates. All rights reserved.
25 # unpack.py - an archive unpack utility
27 # A simple program to uncompress and unpack source archive files into a target
28 # directory and fix permissions if requested.
34 def uncompress_unpack_commands(filename, verbose=False):
37 uncompress = "/bin/cat"
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("(\.xz)$", filename) != None):
48 uncompress = "/usr/bin/xz -dc"
49 elif (re.search("(\.zip)$", filename) != None):
50 uncompress = "/usr/bin/unzip -qo"
51 elif (re.search("(\.oxt)$", filename) != None):
52 uncompress = "/usr/bin/unzip -qo"
53 elif (re.search("(\.gem)$", filename) != None):
54 uncompress = "/usr/bin/gem unpack"
56 unpack = " | gtar -xf -"
58 if (re.search("(\.zip)$", filename) != None):
60 elif (re.search("(\.oxt)$", filename) != None):
62 elif (re.search("(\.jar)$", filename) != None):
63 unpack = " | jar xf -"
64 elif (re.search("(\.gem)$", filename) != None):
68 print "command: %s %s %s" % (uncompress, filename, unpack)
70 return uncompress, unpack
73 # recurse down a directory tree opening permissions so that others may access
76 def fixup_permissions(dir, verbose):
77 for entry in os.listdir(dir):
80 path = "%s/%s" % (dir, entry)
83 mode = stat.S_IMODE(st.st_mode)
84 mode |= (stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
85 if stat.S_ISDIR(st.st_mode):
86 mode |= (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
88 if (stat.S_IMODE(st.st_mode) != mode):
90 print "Changing %s from %4.4o to %4.4o" % (path,
91 stat.S_IMODE(st.st_mode), mode)
94 if stat.S_ISDIR(st.st_mode):
95 fixup_permissions(path, verbose)
99 print "Usage: %s [-v|--verbose] [-f|--fix-permissions] [-r|--relocate-to (dir)] (file)" % (sys.argv[0].split('/')[-1])
112 opts, args = getopt.getopt(sys.argv[1:], "fr:v",
113 ["fix-permissions", "relocate-to=", "verbose"])
114 except getopt.GetoptError, err:
118 for opt, arg in opts:
119 if opt in [ "-v", "--verbose" ]:
121 elif opt in [ "-f", "--fix-permissions" ]:
123 elif opt in [ "-r", "--relocate-to" ]:
126 assert False, "unknown option"
128 filename = ((args[0][0] == '/') and "%s" or "../%s") % args[0]
129 uncompress, unpack = uncompress_unpack_commands(filename)
130 tempdir = tempfile.mkdtemp(dir='.')
132 # extract the archive contents
133 if (verbose == True):
134 print "cd %s ; %s %s%s" % (tempdir, uncompress, filename,
136 os.system("cd %s ; %s %s%s" % (tempdir, uncompress, filename, unpack))
138 # open up the permissions on what we extracted
140 fixup_permissions(tempdir, verbose)
142 if (relocate_to == None):
143 # move everything in the tempdir here
144 for entry in os.listdir(tempdir):
145 path= "%s/%s" % (tempdir, entry)
146 os.renames(path, entry)
148 # rename the tempdir and open it's permissions
149 os.renames(tempdir, relocate_to)
150 os.chmod(relocate_to, 0755)
153 if __name__ == "__main__":