Andreas Wacknitz
2022-09-13 818f75d99adbc980f7603d6f716ea737164e8564
commit | author | age
fb10ba 1 #!/usr/bin/python3.5
9c75c0 2 #
NJ 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.
681a42 23 # Copyright (c) 2018-2019, Michal Nowak
9c75c0 24 #
NJ 25 #
26 # unpack.py - an archive unpack utility
27 #
28 #  A simple program to uncompress and unpack source archive files into a target
29 #  directory and fix permissions if requested.
30 #
31
32 import os
33 import sys
34
35 def uncompress_unpack_commands(filename, verbose=False):
36     import re
37
38     uncompress = "/bin/cat"
39
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 --s"
b1b371 48     elif (re.search("(\.lz)$", filename) != None):
MN 49         uncompress = "/usr/bin/lzip -dc"
8ae331 50     elif (re.search("(\.xz)$", filename) != None):
RB 51         uncompress = "/usr/bin/xz -dc"
9c75c0 52     elif (re.search("(\.zip)$", filename) != None):
NJ 53         uncompress = "/usr/bin/unzip -qo"
db3be8 54     elif (re.search("(\.oxt)$", filename) != None):
AP 55         uncompress = "/usr/bin/unzip -qo"
681a42 56     elif (re.search("(\.zst|\.tzst)$", filename) != None):
MN 57         uncompress = "/usr/bin/unzstd -c"
18b823 58     elif (re.search("(\.gem)$", filename) != None):
1e5ead 59         ruby_ver = os.getenv('RUBY_VERSION', '')
AP 60         uncompress = "/usr/ruby/" + ruby_ver + "/bin/gem unpack"
9c75c0 61
818f75 62     unpack = " | gtar -xf -"
9c75c0 63
818f75 64     if (re.search("(\.zip)$", filename) != None):
9c75c0 65         unpack = ""
db3be8 66     elif (re.search("(\.oxt)$", filename) != None):
AP 67         unpack = ""
9c75c0 68     elif (re.search("(\.jar)$", filename) != None):
NJ 69         unpack = " | jar xf -"
18b823 70     elif (re.search("(\.gem)$", filename) != None):
AP 71         unpack = ""
9c75c0 72
NJ 73     if (verbose == True):
818f75 74         print("command: %s %s %s" % (uncompress, filename, unpack))
9c75c0 75
NJ 76     return uncompress, unpack
77
78 #
79 # recurse down a directory tree opening permissions so that others may access
80 # files in the tree.
81 #
82 def fixup_permissions(dir, verbose):
83     for entry in os.listdir(dir):
84         import stat
85
86         path = "%s/%s" % (dir, entry)
87
88         st = os.lstat(path)
89         mode = stat.S_IMODE(st.st_mode)
90         mode |= (stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
91         if stat.S_ISDIR(st.st_mode):
92             mode |= (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
93
94         if (stat.S_IMODE(st.st_mode) != mode):
95             if (verbose == True):
fb10ba 96                 print("Changing %s from %4.4o to %4.4o" % (path,
AP 97                         stat.S_IMODE(st.st_mode), mode))
9c75c0 98             os.chmod(path, mode)
NJ 99
100         if stat.S_ISDIR(st.st_mode):
101             fixup_permissions(path, verbose)
102
103
104 def usage():
fb10ba 105     print("Usage: %s [-v|--verbose] [-f|--fix-permissions] [-r|--relocate-to (dir)] (file)" % (sys.argv[0].split('/')[-1]))
9c75c0 106     sys.exit(1)
NJ 107
108 def main():
109     import getopt
110     import sys
111     import tempfile
112
113     verbose = False
114     permissions = None
115     relocate_to = None
fab151 116
9c75c0 117     try:
NJ 118         opts, args = getopt.getopt(sys.argv[1:], "fr:v",
119             ["fix-permissions", "relocate-to=", "verbose"])
fb10ba 120     except getopt.GetoptError as err:
AP 121         print(str(err))
9c75c0 122         usage()
NJ 123
124     for opt, arg in opts:
125         if opt in [ "-v", "--verbose" ]:
126             verbose = True
127         elif opt in [ "-f", "--fix-permissions" ]:
128             permissions = True
129         elif opt in [ "-r", "--relocate-to" ]:
130             relocate_to = arg
131         else:
132             assert False, "unknown option"
133
e18339 134     filename = ((args[0][0] == '/') and "%s" or "../%s") % args[0]
9c75c0 135     uncompress, unpack = uncompress_unpack_commands(filename)
NJ 136     tempdir = tempfile.mkdtemp(dir='.')
137
138     # extract the archive contents
139     if (verbose == True):    
fb10ba 140         print("cd %s ; %s %s%s" % (tempdir, uncompress, filename,
AP 141                         unpack))
9c75c0 142     os.system("cd %s ; %s %s%s" % (tempdir, uncompress, filename, unpack))
NJ 143
144     # open up the permissions on what we extracted
145     if permissions:
146         fixup_permissions(tempdir, verbose)
147
148     if (relocate_to == None):
149         # move everything in the tempdir here
150         for entry in os.listdir(tempdir):
151             path= "%s/%s" % (tempdir, entry)
152             os.renames(path, entry)
153     else:
154         # rename the tempdir and open it's permissions
155         os.renames(tempdir, relocate_to)
fb10ba 156         os.chmod(relocate_to, 0o755)
9c75c0 157
NJ 158
159 if __name__ == "__main__":
160     main()