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.
23 # Copyright (c) 2018-2019, Michal Nowak
26 # unpack.py - an archive unpack utility
28 # A simple program to uncompress and unpack source archive files into a target
29 # directory and fix permissions if requested.
35 def uncompress_unpack_commands(filename, verbose=False):
38 uncompress = "/bin/cat"
40 if (re.search("(\.bz2|\.tbz|\.tbz2)$", filename) != None):
41 uncompress = "/usr/bin/bzip2 -dc"
42 elif (re.search("(\.gz|\.tgz)$", filename) != None):
43 uncompress = "/usr/bin/gzip -dc"
44 elif (re.search("(\.Z)$", filename) != None):
45 uncompress = "/usr/bin/uncompress -c"
46 elif (re.search("(\.7z)$", filename) != None):
47 uncompress = "/usr/bin/7z x"
48 elif (re.search("(\.lz)$", filename) != None):
49 uncompress = "/usr/bin/lzip -dc"
50 elif (re.search("(\.xz)$", filename) != None):
51 uncompress = "/usr/bin/xz -dc"
52 elif (re.search("(\.zip)$", filename) != None):
53 uncompress = "/usr/bin/unzip -qo"
54 elif (re.search("(\.oxt)$", filename) != None):
55 uncompress = "/usr/bin/unzip -qo"
56 elif (re.search("(\.zst|\.tzst)$", filename) != None):
57 uncompress = "/usr/bin/unzstd -c"
58 elif (re.search("(\.gem)$", filename) != None):
59 ruby_ver = os.getenv('RUBY_VERSION', '')
60 uncompress = "/usr/ruby/" + ruby_ver + "/bin/gem unpack"
62 # if the file is just compressed, redirect stdout to ./filename with
64 unpack = " > ./" + '.'.join(os.path.basename(filename).split('.')[:-1])
66 if (re.search("(\.tar\.[^\.]+|\.tgz|\.txz|\.tbz2?)$",filename) != None):
67 unpack = " | gtar -xf -"
68 elif (re.search("(\.zip)$", filename) != None):
70 elif (re.search("(\.oxt)$", filename) != None):
72 elif (re.search("(\.jar)$", filename) != None):
73 unpack = " | jar xf -"
74 elif (re.search("(\.gem)$", filename) != None):
78 print("command: %s %s%s" % (uncompress, filename, unpack))
80 return uncompress, unpack
83 # recurse down a directory tree opening permissions so that others may access
86 def fixup_permissions(dir, verbose):
87 for entry in os.listdir(dir):
90 path = "%s/%s" % (dir, entry)
93 mode = stat.S_IMODE(st.st_mode)
94 mode |= (stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
95 if stat.S_ISDIR(st.st_mode):
96 mode |= (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
98 if (stat.S_IMODE(st.st_mode) != mode):
100 print("Changing %s from %4.4o to %4.4o" % (path,
101 stat.S_IMODE(st.st_mode), mode))
104 if stat.S_ISDIR(st.st_mode):
105 fixup_permissions(path, verbose)
109 print("Usage: %s [-v|--verbose] [-f|--fix-permissions] [-r|--relocate-to (dir)] (file)" % (sys.argv[0].split('/')[-1]))
121 if len(sys.argv) == 1:
125 opts, args = getopt.getopt(sys.argv[1:], "fr:v",
126 ["fix-permissions", "relocate-to=", "verbose"])
127 except getopt.GetoptError as err:
131 for opt, arg in opts:
132 if opt in [ "-v", "--verbose" ]:
134 elif opt in [ "-f", "--fix-permissions" ]:
136 elif opt in [ "-r", "--relocate-to" ]:
139 assert False, "unknown option"
141 filename = ((args[0][0] == '/') and "%s" or "../%s") % args[0]
142 uncompress, unpack = uncompress_unpack_commands(filename)
143 tempdir = tempfile.mkdtemp(dir='.')
145 # extract the archive contents
146 if (verbose == True):
147 print("cd %s ; %s %s%s" % (tempdir, uncompress, filename,
149 os.system("cd %s ; %s %s%s" % (tempdir, uncompress, filename, unpack))
151 # open up the permissions on what we extracted
153 fixup_permissions(tempdir, verbose)
155 if (relocate_to == None):
156 # move everything in the tempdir here
157 for entry in os.listdir(tempdir):
158 path= "%s/%s" % (tempdir, entry)
159 os.renames(path, entry)
161 # rename the tempdir and open it's permissions
162 os.renames(tempdir, relocate_to)
163 os.chmod(relocate_to, 0o755)
166 if __name__ == "__main__":