author | shreya.jain@oracle.com <shreya.jain@oracle.com> |
Tue, 08 Nov 2016 13:32:39 -0800 | |
changeset 7269 | 727c4eb5cc2e |
parent 5682 | 94c0ca64c022 |
permissions | -rwxr-xr-x |
3770
ca450a806cc1
20440888 Userland python tools should migrate to 2.7
John Beck <John.Beck@Oracle.COM>
parents:
2054
diff
changeset
|
1 |
#!/usr/bin/python2.7 |
2 | 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 |
# |
|
5682
94c0ca64c022
15558602 TCL_LD_SEARCH_FLAGS is wrongly defined in tclConfig.sh
Shawn Walker-Salas <shawn.walker@oracle.com>
parents:
5477
diff
changeset
|
22 |
|
94c0ca64c022
15558602 TCL_LD_SEARCH_FLAGS is wrongly defined in tclConfig.sh
Shawn Walker-Salas <shawn.walker@oracle.com>
parents:
5477
diff
changeset
|
23 |
# |
5477
5eabbdf042bc
PSARC/2015/113 Add Ruby 2.1 and EOF and Remove Ruby 1.9
April Chin <april.chin@oracle.com>
parents:
3770
diff
changeset
|
24 |
# Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved. |
2 | 25 |
# |
26 |
# |
|
27 |
# unpack.py - an archive unpack utility |
|
28 |
# |
|
29 |
# A simple program to uncompress and unpack source archive files into a target |
|
30 |
# directory and fix permissions if requested. |
|
31 |
# |
|
32 |
||
33 |
import os |
|
34 |
import sys |
|
35 |
||
36 |
def uncompress_unpack_commands(filename, verbose=False): |
|
37 |
import re |
|
38 |
||
39 |
uncompress = "/bin/cat" |
|
40 |
||
41 |
if (re.search("(\.bz2|\.tbz|\.tbz2)$", filename) != None): |
|
42 |
uncompress = "/usr/bin/bzip2 -dc" |
|
43 |
elif (re.search("(\.gz|\.tgz)$", filename) != None): |
|
44 |
uncompress = "/usr/bin/gzip -dc" |
|
45 |
elif (re.search("(\.Z)$", filename) != None): |
|
46 |
uncompress = "/usr/bin/uncompress -c" |
|
47 |
elif (re.search("(\.7z)$", filename) != None): |
|
48 |
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
|
49 |
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
|
50 |
uncompress = "/usr/bin/xz -dc" |
2 | 51 |
elif (re.search("(\.zip)$", filename) != None): |
52 |
uncompress = "/usr/bin/unzip -qo" |
|
2054
5f403d9bcaad
PSARC/2014/275 Hiera 1.3.4
Kristina Tripp <Kristina.Tripp@oracle.com>
parents:
666
diff
changeset
|
53 |
elif (re.search("(\.gem)$", filename) != None): |
5477
5eabbdf042bc
PSARC/2015/113 Add Ruby 2.1 and EOF and Remove Ruby 1.9
April Chin <april.chin@oracle.com>
parents:
3770
diff
changeset
|
54 |
ruby_ver = os.getenv('RUBY_VERSION', '') |
5eabbdf042bc
PSARC/2015/113 Add Ruby 2.1 and EOF and Remove Ruby 1.9
April Chin <april.chin@oracle.com>
parents:
3770
diff
changeset
|
55 |
uncompress = "/usr/ruby/" + ruby_ver + "/bin/gem unpack" |
2 | 56 |
|
57 |
unpack = " | gtar -xf -" |
|
58 |
||
59 |
if (re.search("(\.zip)$", filename) != None): |
|
60 |
unpack = "" |
|
61 |
elif (re.search("(\.jar)$", filename) != None): |
|
62 |
unpack = " | jar xf -" |
|
2054
5f403d9bcaad
PSARC/2014/275 Hiera 1.3.4
Kristina Tripp <Kristina.Tripp@oracle.com>
parents:
666
diff
changeset
|
63 |
elif (re.search("(\.gem)$", filename) != None): |
5f403d9bcaad
PSARC/2014/275 Hiera 1.3.4
Kristina Tripp <Kristina.Tripp@oracle.com>
parents:
666
diff
changeset
|
64 |
unpack = "" |
2 | 65 |
|
66 |
if (verbose == True): |
|
67 |
print "command: %s %s %s" % (uncompress, filename, unpack) |
|
68 |
||
69 |
return uncompress, unpack |
|
70 |
||
71 |
# |
|
72 |
# recurse down a directory tree opening permissions so that others may access |
|
73 |
# files in the tree. |
|
74 |
# |
|
75 |
def fixup_permissions(dir, verbose): |
|
76 |
for entry in os.listdir(dir): |
|
77 |
import stat |
|
78 |
||
79 |
path = "%s/%s" % (dir, entry) |
|
80 |
||
81 |
st = os.lstat(path) |
|
82 |
mode = stat.S_IMODE(st.st_mode) |
|
83 |
mode |= (stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) |
|
84 |
if stat.S_ISDIR(st.st_mode): |
|
85 |
mode |= (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) |
|
86 |
||
87 |
if (stat.S_IMODE(st.st_mode) != mode): |
|
88 |
if (verbose == True): |
|
89 |
print "Changing %s from %4.4o to %4.4o" % (path, |
|
90 |
stat.S_IMODE(st.st_mode), mode) |
|
91 |
os.chmod(path, mode) |
|
92 |
||
93 |
if stat.S_ISDIR(st.st_mode): |
|
94 |
fixup_permissions(path, verbose) |
|
95 |
||
96 |
||
97 |
def usage(): |
|
98 |
print "Usage: %s [-v|--verbose] [-f|--fix-permissions] [-r|--relocate-to (dir)] (file)" % (sys.argv[0].split('/')[-1]) |
|
99 |
sys.exit(1) |
|
100 |
||
101 |
def main(): |
|
102 |
import getopt |
|
103 |
import sys |
|
104 |
import tempfile |
|
105 |
||
106 |
verbose = False |
|
107 |
permissions = None |
|
108 |
relocate_to = None |
|
109 |
||
110 |
try: |
|
111 |
opts, args = getopt.getopt(sys.argv[1:], "fr:v", |
|
112 |
["fix-permissions", "relocate-to=", "verbose"]) |
|
113 |
except getopt.GetoptError, err: |
|
114 |
print str(err) |
|
115 |
usage() |
|
116 |
||
117 |
for opt, arg in opts: |
|
118 |
if opt in [ "-v", "--verbose" ]: |
|
119 |
verbose = True |
|
120 |
elif opt in [ "-f", "--fix-permissions" ]: |
|
121 |
permissions = True |
|
122 |
elif opt in [ "-r", "--relocate-to" ]: |
|
123 |
relocate_to = arg |
|
124 |
else: |
|
125 |
assert False, "unknown option" |
|
126 |
||
127 |
filename = ((args[0] == '/') and "%s" or "../%s") % args[0] |
|
128 |
uncompress, unpack = uncompress_unpack_commands(filename) |
|
129 |
tempdir = tempfile.mkdtemp(dir='.') |
|
130 |
||
131 |
# extract the archive contents |
|
132 |
if (verbose == True): |
|
133 |
print "cd %s ; %s %s%s" % (tempdir, uncompress, filename, |
|
134 |
unpack) |
|
135 |
os.system("cd %s ; %s %s%s" % (tempdir, uncompress, filename, unpack)) |
|
136 |
||
137 |
# open up the permissions on what we extracted |
|
138 |
if permissions: |
|
139 |
fixup_permissions(tempdir, verbose) |
|
140 |
||
141 |
if (relocate_to == None): |
|
142 |
# move everything in the tempdir here |
|
143 |
for entry in os.listdir(tempdir): |
|
144 |
path= "%s/%s" % (tempdir, entry) |
|
145 |
os.renames(path, entry) |
|
146 |
else: |
|
147 |
# rename the tempdir and open it's permissions |
|
148 |
os.renames(tempdir, relocate_to) |
|
149 |
os.chmod(relocate_to, 0755) |
|
150 |
||
151 |
||
152 |
if __name__ == "__main__": |
|
153 |
main() |