-
Notifications
You must be signed in to change notification settings - Fork 5
/
common.py
executable file
·284 lines (214 loc) · 7.94 KB
/
common.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env python3
#
# SPDX-FileCopyrightText: (C) 2020-2023 Joel Winarske
#
# SPDX-License-Identifier: Apache-2.0
#
#
import errno
import os
import sys
from sys import stderr as stream
# use kiB's
kb = 1024
def check_python_version():
if sys.version_info[1] < 7:
sys.exit('Python >= 3.7 required. This machine is running 3.%s' %
sys.version_info[1])
def print_banner(text):
print('*' * (len(text) + 6))
print("** %s **" % text)
print('*' * (len(text) + 6))
def handle_ctrl_c(_signal, _frame):
sys.exit("Ctl+C - Closing")
def run_command(cmd: str, cwd: str) -> str:
""" Run Command in specified working directory """
import re
import subprocess
# replace all consecutive whitespace characters (tabs, newlines etc.) with a single space
cmd = re.sub('\\s{2,}', ' ', cmd)
print('Running [%s] in %s' % (cmd, cwd))
(retval, output) = subprocess.getstatusoutput(f'cd {cwd} && {cmd}')
if retval:
sys.exit("failed %s (cmd was %s)%s" % (retval, cmd, ":\n%s" % output if output else ""))
print(output.rstrip())
return output.rstrip()
def make_sure_path_exists(path: str):
try:
os.makedirs(path)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
def get_md5sum(file: str) -> str:
"""Return md5sum of specified file"""
import hashlib
if not os.path.exists(file):
return ''
md5_hash = hashlib.md5()
with open(file, "rb") as f:
# Read and update hash in chunks of 4K
for byte_block in iter(lambda: f.read(4096), b""):
md5_hash.update(byte_block)
return md5_hash.hexdigest()
def get_sha1sum(file: str) -> str:
"""Return sha1sum of specified file"""
import hashlib
if not os.path.exists(file):
return ''
sha1_hash = hashlib.sha1()
with open(file, "rb") as f:
# Read and update hash in chunks of 4K
for byte_block in iter(lambda: f.read(4096), b""):
sha1_hash.update(byte_block)
return sha1_hash.hexdigest()
def get_sha256sum(file: str):
"""Return sha256sum of specified file"""
import hashlib
if not os.path.exists(file):
return ''
sha256_hash = hashlib.sha256()
with open(file, "rb") as f:
# Read and update hash in chunks of 4K
for byte_block in iter(lambda: f.read(4096), b""):
sha256_hash.update(byte_block)
return sha256_hash.hexdigest()
def download_https_file(cwd, url, file, cookie_file, netrc, md5, sha1, sha256, redirect=False, connect_timeout=None):
download_filepath = os.path.join(cwd, file)
sha256_file = os.path.join(cwd, file + '.sha256')
if compare_sha256(download_filepath, sha256_file):
print("%s exists, skipping download" % download_filepath)
return True
if os.path.exists(download_filepath):
if md5:
# don't download if md5 is good
if md5 == get_md5sum(download_filepath):
print("** Using %s" % download_filepath)
return True
else:
os.remove(download_filepath)
elif sha1:
# don't download if sha1 is good
if sha1 == get_sha1sum(download_filepath):
print("** Using %s" % download_filepath)
return True
else:
os.remove(download_filepath)
elif sha256:
# don't download if sha256 is good
if sha256 == get_sha256sum(download_filepath):
print("** Using %s" % download_filepath)
return True
else:
os.remove(download_filepath)
print("** Downloading %s via %s" % (file, url))
res = fetch_https_binary_file(
url, download_filepath, redirect, None, cookie_file, netrc, connect_timeout)
if not res:
os.remove(download_filepath)
print_banner("Failed to download %s" % file)
return False
if os.path.exists(download_filepath):
if md5:
expected_md5 = get_md5sum(download_filepath)
if md5 != expected_md5:
sys.exit('Download artifact %s md5: %s does not match expected: %s' %
(download_filepath, md5, expected_md5))
elif sha1:
expected_sha1 = get_sha1sum(download_filepath)
if sha1 != expected_sha1:
sys.exit('Download artifact %s sha1: %s does not match expected: %s' %
(download_filepath, md5, expected_sha1))
elif sha256:
expected_sha256 = get_sha256sum(download_filepath)
if sha256 != expected_sha256:
sys.exit('Download artifact %s sha256: %s does not match expected: %s' %
(download_filepath, sha256, expected_sha256))
write_sha256_file(cwd, file)
return True
def compare_sha256(archive_path: str, sha256_file: str) -> bool:
if not os.path.exists(archive_path):
return False
if not os.path.exists(sha256_file):
return False
archive_sha256_val = get_sha256sum(archive_path)
with open(sha256_file, 'r') as f:
sha256_file_val = f.read().replace('\n', '')
if archive_sha256_val == sha256_file_val:
return True
return False
def write_sha256_file(cwd: str, filename: str):
file = os.path.join(cwd, filename)
sha256_val = get_sha256sum(file)
sha256_file = os.path.join(cwd, filename + '.sha256')
with open(sha256_file, 'w+') as f:
f.write(sha256_val)
def fetch_https_progress(download_t, download_d, _upload_t, _upload_d):
"""callback function for pycurl.XFERINFOFUNCTION"""
stream.write('Progress: {}/{} kiB ({}%)\r'.format(str(int(download_d / kb)), str(int(download_t / kb)),
str(int(download_d / download_t * 100) if download_t > 0 else 0)))
stream.flush()
def fetch_https_binary_file(url, filename, redirect, headers, cookie_file, netrc, connect_timeout) -> bool:
"""Fetches binary file via HTTPS"""
import pycurl
import time
retries_left = 3
delay_between_retries = 5 # seconds
success = False
c = pycurl.Curl()
c.setopt(pycurl.URL, url)
if connect_timeout is not None:
c.setopt(pycurl.CONNECTTIMEOUT, connect_timeout)
c.setopt(pycurl.NOSIGNAL, 1)
c.setopt(pycurl.NOPROGRESS, False)
c.setopt(pycurl.XFERINFOFUNCTION, fetch_https_progress)
if headers:
c.setopt(pycurl.HTTPHEADER, headers)
if redirect:
c.setopt(pycurl.FOLLOWLOCATION, 1)
c.setopt(pycurl.AUTOREFERER, 1)
c.setopt(pycurl.MAXREDIRS, 255)
if cookie_file:
cookie_file = os.path.expandvars(cookie_file)
print("Using cookie file: %s" % cookie_file)
c.setopt(pycurl.COOKIEFILE, cookie_file)
if netrc:
c.setopt(pycurl.NETRC, 1)
while retries_left > 0:
try:
with open(filename, 'wb') as f:
c.setopt(pycurl.WRITEFUNCTION, f.write)
c.perform()
success = True
break
except pycurl.error:
retries_left -= 1
print('curl retry')
time.sleep(delay_between_retries)
status = c.getinfo(pycurl.HTTP_CODE)
c.close()
os.sync()
if not redirect and status == 302:
print_banner("Download Status: %d" % status)
return False
if not status == 200:
print_banner("Download Status: %d" % status)
sys.exit('Download Failed')
return success
def test_internet_connection() -> bool:
"""Test internet by connecting to nameserver"""
import pycurl
c = pycurl.Curl()
c.setopt(pycurl.URL, "https://dns.google")
c.setopt(pycurl.FOLLOWLOCATION, 0)
c.setopt(pycurl.CONNECTTIMEOUT, 5)
c.setopt(pycurl.NOSIGNAL, 1)
c.setopt(pycurl.NOPROGRESS, 1)
c.setopt(pycurl.NOBODY, 1)
try:
c.perform()
except:
pass
res = False
if c.getinfo(pycurl.RESPONSE_CODE) == 200:
res = True
return res