tools: switch userland fetch to gpg(1)
[unleashed-userland.git] / tools / userland-unpack
bloba2b51adb5e99db8acce58d68b7e8a6231db9fbcb
1 #!/usr/bin/python2.7
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.
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.
31 import os
32 import sys
34 def uncompress_unpack_commands(filename, verbose=False):
35         import re
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):
60                 unpack = ""
61         elif (re.search("(\.oxt)$", filename) != None):
62                 unpack = ""
63         elif (re.search("(\.jar)$", filename) != None):
64                 unpack = " | jar xf -"
65         elif (re.search("(\.gem)$", filename) != None):
66                 unpack = ""
68         if (verbose == True):
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
75 # files in the tree.
77 def fixup_permissions(dir, verbose):
78         for entry in os.listdir(dir):
79                 import stat
81                 path = "%s/%s" % (dir, entry)
83                 st = os.lstat(path)
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):
90                         if (verbose == True):
91                                 print "Changing %s from %4.4o to %4.4o" % (path,
92                                                 stat.S_IMODE(st.st_mode), mode)
93                         os.chmod(path, mode)
95                 if stat.S_ISDIR(st.st_mode):
96                         fixup_permissions(path, verbose)
99 def usage():
100         print "Usage: %s [-v|--verbose] [-f|--fix-permissions] [-r|--relocate-to (dir)] (file)" % (sys.argv[0].split('/')[-1])
101         sys.exit(1)
103 def main():
104         import getopt
105         import sys
106         import tempfile
108         verbose = False
109         permissions = None
110         relocate_to = None
112         try:
113                 opts, args = getopt.getopt(sys.argv[1:], "fr:v",
114                         ["fix-permissions", "relocate-to=", "verbose"])
115         except getopt.GetoptError, err:
116                 print str(err)
117                 usage()
119         for opt, arg in opts:
120                 if opt in [ "-v", "--verbose" ]:
121                         verbose = True
122                 elif opt in [ "-f", "--fix-permissions" ]:
123                         permissions = True
124                 elif opt in [ "-r", "--relocate-to" ]:
125                         relocate_to = arg
126                 else:
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,
136                                                 unpack)
137         os.system("cd %s ; %s %s%s" % (tempdir, uncompress, filename, unpack))
139         # open up the permissions on what we extracted
140         if permissions:
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)
148         else:
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__":
155         main()