-
Notifications
You must be signed in to change notification settings - Fork 3
/
tools.py
235 lines (200 loc) · 6.57 KB
/
tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import subprocess
import tarfile
import zipfile
import gzip
import bz2
import urllib2
import urlparse
import os
from os import path
from distutils.dir_util import mkpath, remove_tree
import sys
import shutil
import inspect
from urlparse import urlsplit
import re
import ssl
def enter_dir(path):
check_dir(path)
oldpath = os.getcwd()
os.chdir(path)
return oldpath
def check_dir(path):
if not os.path.isdir(path):
assert not os.path.exists(path), "Folder: " + str(path) + " already exists as non-directory"
mkpath(path)
def source_env(path):
f = open(path)
for line in f:
line = line.strip()
if not line.startswith('export'):
continue
name, value = line[6:].strip().split('=')
value = value.replace("$" + name, os.environ.get(name,""))
value = value.strip()
if value.startswith('$(') and value.endswith(')'):
value = getCommandOutput(value[2:-1])
os.environ[name] = value.strip()
def download(url, filename=None):
#based on stackoverflow answer
def getFileName(url,openUrl):
if 'Content-Disposition' in openUrl.info():
# If the response has Content-Disposition, try to get filename from it
match = re.search('filename="(?P<filename>.*)"', str(openUrl.info()))
if match is not None:
return match.group('filename')
# if no filename was found above, parse it out of the final URL.
return os.path.basename(urlsplit(openUrl.url)[2])
try:
if hasattr(ssl, '_create_unverified_context'):
context=ssl._create_unverified_context()
u = urllib2.urlopen(urllib2.Request(url), context=context)
else:
u = urllib2.urlopen(urllib2.Request(url))
#context = hasattr(ssl, '_create_unverified_context') and ssl._create_unverified_context() or None
#context = ssl._create_unverified_context()
#u = urllib2.urlopen(urllib2.Request(url), context=context)
except urllib2.HTTPError, e:
error("Could not download %s, error: %s" %(str(url), str(e)))
oldmask = os.umask(0000)
try:
filename = filename or getFileName(url,u)
meta = u.info()
if len(meta.getheaders("Content-Length")) > 0:
file_size = int(meta.getheaders("Content-Length")[0])
else:
file_size = -1
if file_size < 0:
file_size == "unknown"
print "Downloading: %s Bytes: %s" % (filename, str(file_size))
f = open(filename, 'w')
file_size_dl = 0
block_sz = 8192
while True:
buffer = u.read(block_sz)
if not buffer:
break
file_size_dl += block_sz
f.write(buffer)
if file_size >= 0:
status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
else:
status = r"%10d [file size unknown]" % (file_size_dl)
status = status + chr(8)*(len(status)+1)
print status,
f.close()
finally:
u.close()
os.umask(oldmask)
return filename
def is_gzipfile(filename):
try:
gzip.open(filename,'r')
gzip.close()
return True
except Exception:
return False
def unpack(filename, workdir="", create_workdir=False):
res = []
if not workdir:
if filename.endswith('.tar.gz'):
workdir = filename[:-7]
elif filename.endswith('.tar.xz'):
workdir = filename[:-7]
elif filename.endswith('.tar.bz2'):
workdir = filename[:-8]
elif filename.endswith('.tgz') or filename.endswith('.zip'):
workdir = filename[:-4]
else:
raise RuntimeError, "Cannot determine work directory"
if os.path.isdir(workdir):
remove_tree(workdir)
if create_workdir:
os.mkdir(workdir)
curcwd = os.getcwd()
os.chdir(workdir)
if filename.endswith('bz2'):
runCommand("tar -xjf " + filename)
elif filename.endswith('zip'):
runCommand("unzip -o " + filename)
elif filename.endswith('.tar.xz'):
runCommand("tar -xf " + filename)
else:
runCommand("tar -xzf " + filename)
if create_workdir:
os.chdir(curcwd)
runCommand('find %s -type d ! -perm -g+rwxs -print0 | xargs -0 chmod g+rwxs' % workdir, exception = None)
runCommand('find %s -type f ! -perm -g+rwxs -print0 | xargs -0 chmod g+rw' % workdir, exception = None)
return workdir
def runCommand(cmd, exception = False):
oldmask = os.umask(0000)
#print "Executing: \n" + cmd
res = subprocess.call(cmd,shell=True)
if not res == 0:
if exception == True:
raise RuntimeError, "Command failed"
elif exception == False:
error('The following command failed: ' + cmd)
os.umask(oldmask)
def getCommandOutput(cmd):
oldmask = os.umask(0000)
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
res = p.communicate()[0]
if p.returncode != 0:
raise RuntimeError, "Command failed"
os.umask(oldmask)
return res
def platformContains(value):
q = getCommandOutput('uname -a')
return value in q
def error(msg):
print "\033[41mERROR: " + msg + "\033[0m"
sys.exit(1)
def warning(msg):
print "\033[33mWARNING: " + msg + "\033[0m"
def info(name, msg):
print "\033[32m* " + name + ": " + msg + "\033[0m"
def parse_version(version):
x = version.split(".")
nx = []
for e in x:
try:
nx.append(int(e))
except ValueError:
nx.append(e)
return tuple(nx)
def parse_dep(dependency):
if '==' in dependency:
op = "=="
elif '!=' in dependency:
op = "!="
elif '>=' in dependency:
op = ">="
elif '<=' in dependency:
op = "<="
elif '>' in dependency:
op = ">"
elif '<' in dependency:
op = '<'
else:
return (dependency,)
name, version = dependency.split(op)
return (name, op, parse_version(version))
def get_src_path(obj):
srcfile = inspect.getfile(obj)
if os.path.islink(srcfile):
srcfile = os.readlink(srcfile)
srcpath = os.path.abspath(os.path.dirname(srcfile))
return srcpath
def modifyEnviron(moddict):
olddict = {}
for k,v in moddict.iteritems():
if k in os.environ:
olddict[k]= os.environ[k]
else:
olddict[k] = ""
if v:
os.environ[k]= v
else:
del os.environ[k]
return olddict