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 ruby_ver = os.getenv('RUBY_VERSION', '')
55 uncompress = "/usr/ruby/" + ruby_ver + "/bin/gem unpack"
57 unpack = " | gtar -xf -"
59 if (re.search("(\.zip)$", filename) != None):
61 elif (re.search("(\.oxt)$", filename) != None):
63 elif (re.search("(\.jar)$", filename) != None):
64 unpack = " | jar xf -"
65 elif (re.search("(\.gem)$", filename) != None):
69 print "command: %s %s %s" % (uncompress, filename, unpack)
71 return uncompress, unpack
74 # recurse down a directory tree opening permissions so that others may access
77 def fixup_permissions(dir, verbose):
78 for entry in os.listdir(dir):
81 path = "%s/%s" % (dir, entry)
84 mode = stat.S_IMODE(st.st_mode)
85 mode |= (stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
86 if stat.S_ISDIR(st.st_mode):
87 mode |= (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
89 if (stat.S_IMODE(st.st_mode) != mode):
91 print "Changing %s from %4.4o to %4.4o" % (path,
92 stat.S_IMODE(st.st_mode), mode)
95 if stat.S_ISDIR(st.st_mode):
96 fixup_permissions(path, verbose)
100 print "Usage: %s [-v|--verbose] [-f|--fix-permissions] [-r|--relocate-to (dir)] (file)" % (sys.argv[0].split('/')[-1])
113 opts, args = getopt.getopt(sys.argv[1:], "fr:v",
114 ["fix-permissions", "relocate-to=", "verbose"])
115 except getopt.GetoptError, err:
119 for opt, arg in opts:
120 if opt in [ "-v", "--verbose" ]:
122 elif opt in [ "-f", "--fix-permissions" ]:
124 elif opt in [ "-r", "--relocate-to" ]:
127 assert False, "unknown option"
129 filename = ((args[0][0] == '/') and "%s" or "../%s") % args[0]
130 uncompress, unpack = uncompress_unpack_commands(filename)
131 tempdir = tempfile.mkdtemp(dir='.')
133 # extract the archive contents
134 if (verbose == True):
135 print "cd %s ; %s %s%s" % (tempdir, uncompress, filename,
137 os.system("cd %s ; %s %s%s" % (tempdir, uncompress, filename, unpack))
139 # open up the permissions on what we extracted
141 fixup_permissions(tempdir, verbose)
143 if (relocate_to == None):
144 # move everything in the tempdir here
145 for entry in os.listdir(tempdir):
146 path= "%s/%s" % (tempdir, entry)
147 os.renames(path, entry)
149 # rename the tempdir and open it's permissions
150 os.renames(tempdir, relocate_to)
151 os.chmod(relocate_to, 0755)
154 if __name__ == "__main__":