poppler: update to 24.11.0; fix the tests
[oi-userland.git] / tools / userland-unpack
blob042c1ed0bd2fca387414c64c1710ed887cd2f6da
1 #!/usr/bin/python3.9
3 # CDDL HEADER START
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]
20 # CDDL HEADER END
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.
32 import os
33 import sys
35 def uncompress_unpack_commands(filename, verbose=False):
36         import re
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
63         # one less extension.
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):
69                 unpack = ""
70         elif (re.search("(\.oxt)$", filename) != None):
71                 unpack = ""
72         elif (re.search("(\.jar)$", filename) != None):
73                 unpack = " | jar xf -"
74         elif (re.search("(\.gem)$", filename) != None):
75                 unpack = ""
77         if (verbose == True):
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
84 # files in the tree.
86 def fixup_permissions(dir, verbose):
87         for entry in os.listdir(dir):
88                 import stat
90                 path = "%s/%s" % (dir, entry)
92                 st = os.lstat(path)
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):
99                         if (verbose == True):
100                                 print("Changing %s from %4.4o to %4.4o" % (path,
101                                                 stat.S_IMODE(st.st_mode), mode))
102                         os.chmod(path, mode)
104                 if stat.S_ISDIR(st.st_mode):
105                         fixup_permissions(path, verbose)
108 def usage():
109         print("Usage: %s [-v|--verbose] [-f|--fix-permissions] [-r|--relocate-to (dir)] (file)" % (sys.argv[0].split('/')[-1]))
110         sys.exit(1)
112 def main():
113         import getopt
114         import sys
115         import tempfile
117         verbose = False
118         permissions = None
119         relocate_to = None
121         if len(sys.argv) == 1:
122                 usage()
124         try:
125                 opts, args = getopt.getopt(sys.argv[1:], "fr:v",
126                         ["fix-permissions", "relocate-to=", "verbose"])
127         except getopt.GetoptError as err:
128                 print(str(err))
129                 usage()
131         for opt, arg in opts:
132                 if opt in [ "-v", "--verbose" ]:
133                         verbose = True
134                 elif opt in [ "-f", "--fix-permissions" ]:
135                         permissions = True
136                 elif opt in [ "-r", "--relocate-to" ]:
137                         relocate_to = arg
138                 else:
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,
148                                                 unpack))
149         os.system("cd %s ; %s %s%s" % (tempdir, uncompress, filename, unpack))
151         # open up the permissions on what we extracted
152         if permissions:
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)
160         else:
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__":
167         main()