author | Mike Sullivan <Mike.Sullivan@Oracle.COM> |
Mon, 20 Oct 2014 17:41:24 -0700 | |
branch | s11-update |
changeset 3413 | e741c056f5a6 |
parent 3378 | 8c7eb3630145 |
child 3770 | ca450a806cc1 |
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 |
# |
|
3378
8c7eb3630145
PSARC/2014/275 Hiera 1.3.4
Kristina Tripp <Kristina.Tripp@oracle.com>
parents:
666
diff
changeset
|
22 |
# Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 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" |
|
3378
8c7eb3630145
PSARC/2014/275 Hiera 1.3.4
Kristina Tripp <Kristina.Tripp@oracle.com>
parents:
666
diff
changeset
|
51 |
elif (re.search("(\.gem)$", filename) != None): |
8c7eb3630145
PSARC/2014/275 Hiera 1.3.4
Kristina Tripp <Kristina.Tripp@oracle.com>
parents:
666
diff
changeset
|
52 |
uncompress = "/usr/bin/gem unpack" |
2 | 53 |
|
54 |
unpack = " | gtar -xf -" |
|
55 |
||
56 |
if (re.search("(\.zip)$", filename) != None): |
|
57 |
unpack = "" |
|
58 |
elif (re.search("(\.jar)$", filename) != None): |
|
59 |
unpack = " | jar xf -" |
|
3378
8c7eb3630145
PSARC/2014/275 Hiera 1.3.4
Kristina Tripp <Kristina.Tripp@oracle.com>
parents:
666
diff
changeset
|
60 |
elif (re.search("(\.gem)$", filename) != None): |
8c7eb3630145
PSARC/2014/275 Hiera 1.3.4
Kristina Tripp <Kristina.Tripp@oracle.com>
parents:
666
diff
changeset
|
61 |
unpack = "" |
2 | 62 |
|
63 |
if (verbose == True): |
|
64 |
print "command: %s %s %s" % (uncompress, filename, unpack) |
|
65 |
||
66 |
return uncompress, unpack |
|
67 |
||
68 |
# |
|
69 |
# recurse down a directory tree opening permissions so that others may access |
|
70 |
# files in the tree. |
|
71 |
# |
|
72 |
def fixup_permissions(dir, verbose): |
|
73 |
for entry in os.listdir(dir): |
|
74 |
import stat |
|
75 |
||
76 |
path = "%s/%s" % (dir, entry) |
|
77 |
||
78 |
st = os.lstat(path) |
|
79 |
mode = stat.S_IMODE(st.st_mode) |
|
80 |
mode |= (stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) |
|
81 |
if stat.S_ISDIR(st.st_mode): |
|
82 |
mode |= (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) |
|
83 |
||
84 |
if (stat.S_IMODE(st.st_mode) != mode): |
|
85 |
if (verbose == True): |
|
86 |
print "Changing %s from %4.4o to %4.4o" % (path, |
|
87 |
stat.S_IMODE(st.st_mode), mode) |
|
88 |
os.chmod(path, mode) |
|
89 |
||
90 |
if stat.S_ISDIR(st.st_mode): |
|
91 |
fixup_permissions(path, verbose) |
|
92 |
||
93 |
||
94 |
def usage(): |
|
95 |
print "Usage: %s [-v|--verbose] [-f|--fix-permissions] [-r|--relocate-to (dir)] (file)" % (sys.argv[0].split('/')[-1]) |
|
96 |
sys.exit(1) |
|
97 |
||
98 |
def main(): |
|
99 |
import getopt |
|
100 |
import sys |
|
101 |
import tempfile |
|
102 |
||
103 |
verbose = False |
|
104 |
permissions = None |
|
105 |
relocate_to = None |
|
106 |
||
107 |
try: |
|
108 |
opts, args = getopt.getopt(sys.argv[1:], "fr:v", |
|
109 |
["fix-permissions", "relocate-to=", "verbose"]) |
|
110 |
except getopt.GetoptError, err: |
|
111 |
print str(err) |
|
112 |
usage() |
|
113 |
||
114 |
for opt, arg in opts: |
|
115 |
if opt in [ "-v", "--verbose" ]: |
|
116 |
verbose = True |
|
117 |
elif opt in [ "-f", "--fix-permissions" ]: |
|
118 |
permissions = True |
|
119 |
elif opt in [ "-r", "--relocate-to" ]: |
|
120 |
relocate_to = arg |
|
121 |
else: |
|
122 |
assert False, "unknown option" |
|
123 |
||
124 |
filename = ((args[0] == '/') and "%s" or "../%s") % args[0] |
|
125 |
uncompress, unpack = uncompress_unpack_commands(filename) |
|
126 |
tempdir = tempfile.mkdtemp(dir='.') |
|
127 |
||
128 |
# extract the archive contents |
|
129 |
if (verbose == True): |
|
130 |
print "cd %s ; %s %s%s" % (tempdir, uncompress, filename, |
|
131 |
unpack) |
|
132 |
os.system("cd %s ; %s %s%s" % (tempdir, uncompress, filename, unpack)) |
|
133 |
||
134 |
# open up the permissions on what we extracted |
|
135 |
if permissions: |
|
136 |
fixup_permissions(tempdir, verbose) |
|
137 |
||
138 |
if (relocate_to == None): |
|
139 |
# move everything in the tempdir here |
|
140 |
for entry in os.listdir(tempdir): |
|
141 |
path= "%s/%s" % (tempdir, entry) |
|
142 |
os.renames(path, entry) |
|
143 |
else: |
|
144 |
# rename the tempdir and open it's permissions |
|
145 |
os.renames(tempdir, relocate_to) |
|
146 |
os.chmod(relocate_to, 0755) |
|
147 |
||
148 |
||
149 |
if __name__ == "__main__": |
|
150 |
main() |