userland-unpack revision 5680
3770N/A#!/usr/bin/python2.7
2N/A#
2N/A# CDDL HEADER START
2N/A#
2N/A# The contents of this file are subject to the terms of the
2N/A# Common Development and Distribution License (the "License").
2N/A# You may not use this file except in compliance with the License.
2N/A#
2N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A# or http://www.opensolaris.org/os/licensing.
2N/A# See the License for the specific language governing permissions
2N/A# and limitations under the License.
2N/A#
2N/A# When distributing Covered Code, include this CDDL HEADER in each
2N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A# If applicable, add the following below this CDDL HEADER, with the
2N/A# fields enclosed by brackets "[]" replaced with your own identifying
2N/A# information: Portions Copyright [yyyy] [name of copyright owner]
2N/A#
2N/A# CDDL HEADER END
2N/A#
5680N/A
5680N/A#
5475N/A# Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
2N/A#
2N/A#
2N/A# unpack.py - an archive unpack utility
2N/A#
2N/A# A simple program to uncompress and unpack source archive files into a target
2N/A# directory and fix permissions if requested.
2N/A#
2N/A
2N/Aimport os
2N/Aimport sys
2N/A
2N/Adef uncompress_unpack_commands(filename, verbose=False):
2N/A import re
2N/A
2N/A uncompress = "/bin/cat"
2N/A
2N/A if (re.search("(\.bz2|\.tbz|\.tbz2)$", filename) != None):
2N/A uncompress = "/usr/bin/bzip2 -dc"
2N/A elif (re.search("(\.gz|\.tgz)$", filename) != None):
2N/A uncompress = "/usr/bin/gzip -dc"
2N/A elif (re.search("(\.Z)$", filename) != None):
2N/A uncompress = "/usr/bin/uncompress -c"
2N/A elif (re.search("(\.7z)$", filename) != None):
2N/A uncompress = "/usr/bin/7z --s"
682N/A elif (re.search("(\.xz)$", filename) != None):
682N/A uncompress = "/usr/bin/xz -dc"
2N/A elif (re.search("(\.zip)$", filename) != None):
2N/A uncompress = "/usr/bin/unzip -qo"
3109N/A elif (re.search("(\.gem)$", filename) != None):
5475N/A ruby_ver = os.getenv('RUBY_VERSION', '')
5475N/A uncompress = "/usr/ruby/" + ruby_ver + "/bin/gem unpack"
2N/A
2N/A unpack = " | gtar -xf -"
2N/A
2N/A if (re.search("(\.zip)$", filename) != None):
2N/A unpack = ""
2N/A elif (re.search("(\.jar)$", filename) != None):
2N/A unpack = " | jar xf -"
3109N/A elif (re.search("(\.gem)$", filename) != None):
3109N/A unpack = ""
2N/A
2N/A if (verbose == True):
2N/A print "command: %s %s %s" % (uncompress, filename, unpack)
2N/A
2N/A return uncompress, unpack
2N/A
2N/A#
2N/A# recurse down a directory tree opening permissions so that others may access
2N/A# files in the tree.
2N/A#
2N/Adef fixup_permissions(dir, verbose):
2N/A for entry in os.listdir(dir):
2N/A import stat
2N/A
2N/A path = "%s/%s" % (dir, entry)
2N/A
2N/A st = os.lstat(path)
2N/A mode = stat.S_IMODE(st.st_mode)
2N/A mode |= (stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
2N/A if stat.S_ISDIR(st.st_mode):
2N/A mode |= (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
2N/A
2N/A if (stat.S_IMODE(st.st_mode) != mode):
2N/A if (verbose == True):
2N/A print "Changing %s from %4.4o to %4.4o" % (path,
2N/A stat.S_IMODE(st.st_mode), mode)
2N/A os.chmod(path, mode)
2N/A
2N/A if stat.S_ISDIR(st.st_mode):
2N/A fixup_permissions(path, verbose)
2N/A
2N/A
2N/Adef usage():
2N/A print "Usage: %s [-v|--verbose] [-f|--fix-permissions] [-r|--relocate-to (dir)] (file)" % (sys.argv[0].split('/')[-1])
2N/A sys.exit(1)
2N/A
2N/Adef main():
2N/A import getopt
2N/A import sys
2N/A import tempfile
2N/A
2N/A verbose = False
2N/A permissions = None
2N/A relocate_to = None
2N/A
2N/A try:
2N/A opts, args = getopt.getopt(sys.argv[1:], "fr:v",
2N/A ["fix-permissions", "relocate-to=", "verbose"])
2N/A except getopt.GetoptError, err:
2N/A print str(err)
2N/A usage()
2N/A
2N/A for opt, arg in opts:
2N/A if opt in [ "-v", "--verbose" ]:
2N/A verbose = True
2N/A elif opt in [ "-f", "--fix-permissions" ]:
2N/A permissions = True
2N/A elif opt in [ "-r", "--relocate-to" ]:
2N/A relocate_to = arg
2N/A else:
2N/A assert False, "unknown option"
2N/A
2N/A filename = ((args[0] == '/') and "%s" or "../%s") % args[0]
2N/A uncompress, unpack = uncompress_unpack_commands(filename)
2N/A tempdir = tempfile.mkdtemp(dir='.')
2N/A
2N/A # extract the archive contents
2N/A if (verbose == True):
2N/A print "cd %s ; %s %s%s" % (tempdir, uncompress, filename,
2N/A unpack)
2N/A os.system("cd %s ; %s %s%s" % (tempdir, uncompress, filename, unpack))
2N/A
2N/A # open up the permissions on what we extracted
2N/A if permissions:
2N/A fixup_permissions(tempdir, verbose)
2N/A
2N/A if (relocate_to == None):
2N/A # move everything in the tempdir here
2N/A for entry in os.listdir(tempdir):
2N/A path= "%s/%s" % (tempdir, entry)
2N/A os.renames(path, entry)
2N/A else:
2N/A # rename the tempdir and open it's permissions
2N/A os.renames(tempdir, relocate_to)
2N/A os.chmod(relocate_to, 0755)
2N/A
2N/A
2N/Aif __name__ == "__main__":
2N/A main()