-
Notifications
You must be signed in to change notification settings - Fork 175
/
hifi_utils.py
183 lines (151 loc) · 6.44 KB
/
hifi_utils.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
import os
import hashlib
import platform
import shutil
import ssl
import subprocess
import sys
import tarfile
import re
import urllib
import urllib.request
import zipfile
import tempfile
import time
import functools
print = functools.partial(print, flush=True)
ansi_colors = {
'black' : 30,
'red': 31,
'green': 32,
'yellow': 33,
'blue': 34,
'magenta': 35,
'cyan': 36,
'white': 37,
'clear': 0
}
def scriptRelative(*paths):
scriptdir = os.path.dirname(os.path.realpath(sys.argv[0]))
result = os.path.join(scriptdir, *paths)
result = os.path.realpath(result)
result = os.path.normcase(result)
return result
def recursiveFileList(startPath, excludeNamePattern=None ):
result = []
if os.path.isfile(startPath):
result.append(startPath)
elif os.path.isdir(startPath):
for dirName, subdirList, fileList in os.walk(startPath):
for fname in fileList:
if excludeNamePattern and re.match(excludeNamePattern, fname):
continue
result.append(os.path.realpath(os.path.join(startPath, dirName, fname)))
result.sort()
return result
def executeSubprocessCapture(processArgs):
processResult = subprocess.run(processArgs, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if (0 != processResult.returncode):
raise RuntimeError('Call to "{}" failed.\n\narguments:\n{}\n\nstdout:\n{}\n\nstderr:\n{}'.format(
processArgs[0],
' '.join(processArgs[1:]),
processResult.stdout.decode('utf-8'),
processResult.stderr.decode('utf-8')))
return processResult.stdout.decode('utf-8')
def executeSubprocess(processArgs, folder=None, env=None):
restoreDir = None
if folder != None:
restoreDir = os.getcwd()
os.chdir(folder)
process = subprocess.Popen(
processArgs, stdout=sys.stdout, stderr=sys.stderr, env=env)
process.wait()
if (0 != process.returncode):
raise RuntimeError('Call to "{}" failed.\n\narguments:\n{}\n'.format(
processArgs[0],
' '.join(processArgs[1:]),
))
if restoreDir != None:
os.chdir(restoreDir)
def hashFile(file, hasherType = hashlib.sha512):
hasher = hasherType()
with open(file, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hasher.update(chunk)
return hasher.hexdigest()
# Assumes input files are in deterministic order
def hashFiles(filenames):
hasher = hashlib.sha256()
for filename in filenames:
with open(filename, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hasher.update(chunk)
return hasher.hexdigest()
def hashFolder(folder):
filenames = recursiveFileList(folder)
return hashFiles(filenames)
def downloadFile(urls, hash=None, hasher=hashlib.sha512, retries=3):
errorLog = ""
for url in urls:
for i in range(retries):
tempFileName = None
# some sites may block non-browser user agents
user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
headers = { 'User-Agent': user_agent }
request = urllib.request.Request(url, None, headers)
try:
# OSX Python doesn't support SSL, so we need to bypass it.
# However, we still validate the downloaded file's sha512 hash
if 'Darwin' == platform.system():
context = ssl._create_unverified_context()
else:
context = None
tempFileDescriptor, tempFileName = tempfile.mkstemp()
with urllib.request.urlopen(request, context=context) as response, open(tempFileDescriptor, 'wb') as tempFile:
shutil.copyfileobj(response, tempFile)
except Exception as e:
errorLog = errorLog + url + ": " + repr(e) + "\n"
continue
downloadHash = hashFile(tempFileName, hasher)
# Verify the hash
if hash is not None and hash != downloadHash:
errorLog = errorLog + "Try {}: Downloaded file {} hash {} does not match expected hash {} for url {}\n".format(i + 1, tempFileName, downloadHash, hash, url)
errorLog = errorLog + "File stats: " + os.stat(tempFileName) + "\n"
os.remove(tempFileName)
continue
return tempFileName
raise RuntimeError("Failed to download file from any of {} \nError Log:\n{}".format(urls, errorLog))
def downloadAndExtract(urls, destPath, hash=None, hasher=hashlib.sha512):
tempFileName = downloadFile(urls, hash, hasher)
try:
with zipfile.ZipFile(tempFileName) as zip:
zip.extractall(destPath)
except zipfile.BadZipFile as error:
with tarfile.open(tempFileName, 'r:*') as tgz:
def is_within_directory(directory, target):
abs_directory = os.path.abspath(directory)
abs_target = os.path.abspath(target)
prefix = os.path.commonprefix([abs_directory, abs_target])
return prefix == abs_directory
def safe_extract(tar, path=".", members=None, *, numeric_owner=False):
for member in tar.getmembers():
member_path = os.path.join(path, member.name)
if not is_within_directory(path, member_path):
raise Exception("Attempted Path Traversal in Tar File")
tar.extractall(path, members, numeric_owner=numeric_owner)
safe_extract(tgz, destPath)
os.remove(tempFileName)
def readEnviromentVariableFromFile(buildRootDir, var):
with open(os.path.join(buildRootDir, '_env', var + ".txt")) as fp:
return fp.read()
class SilentFatalError(Exception):
"""Thrown when some sort of fatal condition happened, and we already reported it to the user.
This excecption exists to give a chance to run any cleanup needed before exiting.
It should be handled at the bottom of the call stack, where the only action is to call
sys.exit(ex.exit_code)
"""
def __init__(self, exit_code):
self.exit_code = exit_code
def color(color_name):
# Ideally we'd use the termcolor module, but this avoids adding it as a dependency.
print("\033[1;{}m".format(ansi_colors[color_name]), end='')