author | Danek Duvall <danek.duvall@oracle.com> |
Mon, 05 Aug 2013 13:37:57 -0700 | |
changeset 1420 | 597ecfc1f6c0 |
parent 666 | 3e3828ae1878 |
child 2054 | 5f403d9bcaad |
permissions | -rwxr-xr-x |
2 | 1 |
#!/usr/bin/python2.6 |
2 |
# |
|
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. |
|
23 |
# |
|
24 |
# |
|
25 |
# unpack.py - an archive unpack utility |
|
26 |
# |
|
27 |
# A simple program to uncompress and unpack source archive files into a target |
|
28 |
# directory and fix permissions if requested. |
|
29 |
# |
|
30 |
||
31 |
import os |
|
32 |
import sys |
|
33 |
||
34 |
def uncompress_unpack_commands(filename, verbose=False): |
|
35 |
import re |
|
36 |
||
37 |
uncompress = "/bin/cat" |
|
38 |
||
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" |
|
666
3e3828ae1878
PSARC 2011/397 Update GNU grep to 2.10
Rich Burridge <rich.burridge@oracle.com>
parents:
34
diff
changeset
|
47 |
elif (re.search("(\.xz)$", filename) != None): |
3e3828ae1878
PSARC 2011/397 Update GNU grep to 2.10
Rich Burridge <rich.burridge@oracle.com>
parents:
34
diff
changeset
|
48 |
uncompress = "/usr/bin/xz -dc" |
2 | 49 |
elif (re.search("(\.zip)$", filename) != None): |
50 |
uncompress = "/usr/bin/unzip -qo" |
|
51 |
||
52 |
unpack = " | gtar -xf -" |
|
53 |
||
54 |
if (re.search("(\.zip)$", filename) != None): |
|
55 |
unpack = "" |
|
56 |
elif (re.search("(\.jar)$", filename) != None): |
|
57 |
unpack = " | jar xf -" |
|
58 |
||
59 |
if (verbose == True): |
|
60 |
print "command: %s %s %s" % (uncompress, filename, unpack) |
|
61 |
||
62 |
return uncompress, unpack |
|
63 |
||
64 |
# |
|
65 |
# recurse down a directory tree opening permissions so that others may access |
|
66 |
# files in the tree. |
|
67 |
# |
|
68 |
def fixup_permissions(dir, verbose): |
|
69 |
for entry in os.listdir(dir): |
|
70 |
import stat |
|
71 |
||
72 |
path = "%s/%s" % (dir, entry) |
|
73 |
||
74 |
st = os.lstat(path) |
|
75 |
mode = stat.S_IMODE(st.st_mode) |
|
76 |
mode |= (stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) |
|
77 |
if stat.S_ISDIR(st.st_mode): |
|
78 |
mode |= (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) |
|
79 |
||
80 |
if (stat.S_IMODE(st.st_mode) != mode): |
|
81 |
if (verbose == True): |
|
82 |
print "Changing %s from %4.4o to %4.4o" % (path, |
|
83 |
stat.S_IMODE(st.st_mode), mode) |
|
84 |
os.chmod(path, mode) |
|
85 |
||
86 |
if stat.S_ISDIR(st.st_mode): |
|
87 |
fixup_permissions(path, verbose) |
|
88 |
||
89 |
||
90 |
def usage(): |
|
91 |
print "Usage: %s [-v|--verbose] [-f|--fix-permissions] [-r|--relocate-to (dir)] (file)" % (sys.argv[0].split('/')[-1]) |
|
92 |
sys.exit(1) |
|
93 |
||
94 |
def main(): |
|
95 |
import getopt |
|
96 |
import sys |
|
97 |
import tempfile |
|
98 |
||
99 |
verbose = False |
|
100 |
permissions = None |
|
101 |
relocate_to = None |
|
102 |
||
103 |
try: |
|
104 |
opts, args = getopt.getopt(sys.argv[1:], "fr:v", |
|
105 |
["fix-permissions", "relocate-to=", "verbose"]) |
|
106 |
except getopt.GetoptError, err: |
|
107 |
print str(err) |
|
108 |
usage() |
|
109 |
||
110 |
for opt, arg in opts: |
|
111 |
if opt in [ "-v", "--verbose" ]: |
|
112 |
verbose = True |
|
113 |
elif opt in [ "-f", "--fix-permissions" ]: |
|
114 |
permissions = True |
|
115 |
elif opt in [ "-r", "--relocate-to" ]: |
|
116 |
relocate_to = arg |
|
117 |
else: |
|
118 |
assert False, "unknown option" |
|
119 |
||
120 |
filename = ((args[0] == '/') and "%s" or "../%s") % args[0] |
|
121 |
uncompress, unpack = uncompress_unpack_commands(filename) |
|
122 |
tempdir = tempfile.mkdtemp(dir='.') |
|
123 |
||
124 |
# extract the archive contents |
|
125 |
if (verbose == True): |
|
126 |
print "cd %s ; %s %s%s" % (tempdir, uncompress, filename, |
|
127 |
unpack) |
|
128 |
os.system("cd %s ; %s %s%s" % (tempdir, uncompress, filename, unpack)) |
|
129 |
||
130 |
# open up the permissions on what we extracted |
|
131 |
if permissions: |
|
132 |
fixup_permissions(tempdir, verbose) |
|
133 |
||
134 |
if (relocate_to == None): |
|
135 |
# move everything in the tempdir here |
|
136 |
for entry in os.listdir(tempdir): |
|
137 |
path= "%s/%s" % (tempdir, entry) |
|
138 |
os.renames(path, entry) |
|
139 |
else: |
|
140 |
# rename the tempdir and open it's permissions |
|
141 |
os.renames(tempdir, relocate_to) |
|
142 |
os.chmod(relocate_to, 0755) |
|
143 |
||
144 |
||
145 |
if __name__ == "__main__": |
|
146 |
main() |