1
|
|
|
#!/usr/bin/env python3 |
2
|
|
|
"""This module is used for miscellaneous utilities.""" |
3
|
|
|
|
4
|
|
|
import os # path work |
5
|
|
|
import argparse # argument parser for filters |
6
|
|
|
import platform # platform info |
7
|
|
|
import glob # cap grabbing |
8
|
|
|
import hashlib # base url creation |
9
|
|
|
import threading # get thread for spinner |
10
|
|
|
import time # spinner delay |
11
|
|
|
import sys # streams, version info |
12
|
|
|
import itertools # spinners gonna spin |
13
|
|
|
import subprocess # loader verification |
14
|
|
|
from bbarchivist import bbconstants # cap location, version, filename bits |
15
|
|
|
from bbarchivist import compat # backwards compat |
16
|
|
|
from bbarchivist import dummy # useless stdout |
17
|
|
|
from bbarchivist import iniconfig # config parsing |
18
|
|
|
|
19
|
|
|
__author__ = "Thurask" |
20
|
|
|
__license__ = "WTFPL v2" |
21
|
|
|
__copyright__ = "Copyright 2015-2016 Thurask" |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
def grab_cap(): |
25
|
|
|
""" |
26
|
|
|
Figure out where cap is, local, specified or system-supplied. |
27
|
|
|
""" |
28
|
|
|
try: |
29
|
|
|
capfile = glob.glob(os.path.join(os.getcwd(), bbconstants.CAP.filename))[0] |
30
|
|
|
except IndexError: |
31
|
|
|
try: |
32
|
|
|
cappath = cappath_config_loader() |
33
|
|
|
capfile = glob.glob(cappath)[0] |
34
|
|
|
except IndexError: |
35
|
|
|
cappath_config_writer(bbconstants.CAP.location) |
36
|
|
|
return bbconstants.CAP.location # no ini cap |
37
|
|
|
else: |
38
|
|
|
cappath_config_writer(os.path.abspath(capfile)) |
39
|
|
|
return os.path.abspath(capfile) # ini cap |
40
|
|
|
else: |
41
|
|
|
return os.path.abspath(capfile) # local cap |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
def grab_cfp(): |
45
|
|
|
""" |
46
|
|
|
Figure out where cfp is, local or system-supplied. |
47
|
|
|
""" |
48
|
|
|
try: |
49
|
|
|
cfpfile = glob.glob(os.path.join(os.getcwd(), bbconstants.CFP.filename))[0] |
50
|
|
|
except IndexError: |
51
|
|
|
cfpfile = bbconstants.CFP.location # system cfp |
52
|
|
|
return os.path.abspath(cfpfile) # local cfp |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
def new_enough(minver): |
56
|
|
|
""" |
57
|
|
|
Check if we're at or above a minimum Python version. |
58
|
|
|
|
59
|
|
|
:param minver: Minimum Python version (3.minver). |
60
|
|
|
:type minver: int |
61
|
|
|
""" |
62
|
|
|
return False if minver > sys.version_info[1] else True |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
def fsizer(file_size): |
66
|
|
|
""" |
67
|
|
|
Raw byte file size to human-readable string. |
68
|
|
|
|
69
|
|
|
:param file_size: Number to parse. |
70
|
|
|
:type file_size: float |
71
|
|
|
""" |
72
|
|
|
if file_size is None: |
73
|
|
|
file_size = 0 |
74
|
|
|
fsize = float(file_size) |
75
|
|
|
for sfix in ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB']: |
76
|
|
|
if fsize < 1024.0: |
77
|
|
|
size = "{0:3.2f}{1}".format(fsize, sfix) |
78
|
|
|
break |
79
|
|
|
else: |
80
|
|
|
fsize /= 1024.0 |
81
|
|
|
else: |
82
|
|
|
size = "{0:3.2f}{1}".format(fsize, 'YB') |
83
|
|
|
return size |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
def signed_file_args(files): |
87
|
|
|
""" |
88
|
|
|
Check if there are between 1 and 6 files supplied to argparse. |
89
|
|
|
|
90
|
|
|
:param files: List of signed files, between 1 and 6 strings. |
91
|
|
|
:type files: list(str) |
92
|
|
|
""" |
93
|
|
|
filelist = [file for file in files if file] |
94
|
|
|
if not 1 <= len(filelist) <= 6: |
95
|
|
|
raise argparse.ArgumentError(argument=None, message="Requires 1-6 signed files") |
96
|
|
|
return files |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
def file_exists(file): |
100
|
|
|
""" |
101
|
|
|
Check if file exists, raise argparse error if it doesn't. |
102
|
|
|
|
103
|
|
|
:param file: Path to a file, including extension. |
104
|
|
|
:type file: str |
105
|
|
|
""" |
106
|
|
|
if not os.path.exists(file): |
107
|
|
|
raise argparse.ArgumentError(argument=None, message="{0} not found.".format(file)) |
108
|
|
|
return file |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
def positive_integer(input_int): |
112
|
|
|
""" |
113
|
|
|
Check if number > 0, raise argparse error if it isn't. |
114
|
|
|
|
115
|
|
|
:param input_int: Integer to check. |
116
|
|
|
:type input_int: str |
117
|
|
|
""" |
118
|
|
|
if int(input_int) <= 0: |
119
|
|
|
info = "{0} is not >=0.".format(str(input_int)) |
120
|
|
|
raise argparse.ArgumentError(argument=None, message=info) |
121
|
|
|
return int(input_int) |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
def valid_method(method): |
125
|
|
|
""" |
126
|
|
|
Check if compression method is valid, raise argparse error if it isn't. |
127
|
|
|
|
128
|
|
|
:param method: Compression method to check. |
129
|
|
|
:type method: str |
130
|
|
|
""" |
131
|
|
|
methodlist = bbconstants.METHODS |
132
|
|
|
if not new_enough(3): |
133
|
|
|
methodlist = [x for x in bbconstants.METHODS if x != "txz"] |
134
|
|
|
if method not in methodlist: |
135
|
|
|
info = "Invalid method {0}.".format(method) |
136
|
|
|
raise argparse.ArgumentError(argument=None, message=info) |
137
|
|
|
return method |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
def valid_carrier(mcc_mnc): |
141
|
|
|
""" |
142
|
|
|
Check if MCC/MNC is valid (1-3 chars), raise argparse error if it isn't. |
143
|
|
|
|
144
|
|
|
:param mcc_mnc: MCC/MNC to check. |
145
|
|
|
:type mcc_mnc: str |
146
|
|
|
""" |
147
|
|
|
if not str(mcc_mnc).isdecimal(): |
148
|
|
|
infod = "Non-integer {0}.".format(str(mcc_mnc)) |
149
|
|
|
raise argparse.ArgumentError(argument=None, message=infod) |
150
|
|
|
if len(str(mcc_mnc)) > 3 or len(str(mcc_mnc)) == 0: |
151
|
|
|
infol = "{0} is invalid.".format(str(mcc_mnc)) |
152
|
|
|
raise argparse.ArgumentError(argument=None, message=infol) |
153
|
|
|
else: |
154
|
|
|
return mcc_mnc |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
def escreens_pin(pin): |
158
|
|
|
""" |
159
|
|
|
Check if given PIN is valid, raise argparse error if it isn't. |
160
|
|
|
|
161
|
|
|
:param pin: PIN to check. |
162
|
|
|
:type pin: str |
163
|
|
|
""" |
164
|
|
|
if len(pin) == 8: |
165
|
|
|
try: |
166
|
|
|
int(pin, 16) # hexadecimal-ness |
167
|
|
|
except ValueError: |
168
|
|
|
raise argparse.ArgumentError(argument=None, message="Invalid PIN.") |
169
|
|
|
else: |
170
|
|
|
return pin.lower() |
171
|
|
|
else: |
172
|
|
|
raise argparse.ArgumentError(argument=None, message="Invalid PIN.") |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
def escreens_duration(duration): |
176
|
|
|
""" |
177
|
|
|
Check if Engineering Screens duration is valid. |
178
|
|
|
|
179
|
|
|
:param duration: Duration to check. |
180
|
|
|
:type duration: int |
181
|
|
|
""" |
182
|
|
|
if int(duration) in (1, 3, 6, 15, 30): |
183
|
|
|
return int(duration) |
184
|
|
|
else: |
185
|
|
|
raise argparse.ArgumentError(argument=None, message="Invalid duration.") |
186
|
|
|
|
187
|
|
|
|
188
|
|
|
def droidlookup_hashtype(method): |
189
|
|
|
""" |
190
|
|
|
Check if Android autoloader lookup hash type is valid. |
191
|
|
|
|
192
|
|
|
:param method: None for regular OS links, "sha256/512" for SHA256 or 512 hash. |
193
|
|
|
:type method: str |
194
|
|
|
""" |
195
|
|
|
if method.lower() in ("sha512", "sha256"): |
196
|
|
|
return method.lower() |
197
|
|
|
else: |
198
|
|
|
raise argparse.ArgumentError(argument=None, message="Invalid type.") |
199
|
|
|
|
200
|
|
|
|
201
|
|
|
def droidlookup_devicetype(device): |
202
|
|
|
""" |
203
|
|
|
Check if Android autoloader device type is valid. |
204
|
|
|
|
205
|
|
|
:param device: Android autoloader types to check. |
206
|
|
|
:type device: str |
207
|
|
|
""" |
208
|
|
|
devices = ("Priv", "DTEK50") |
209
|
|
|
#devices = ("Priv", "DTEK50", "DTEK60") |
210
|
|
|
if device is None: |
211
|
|
|
return None |
212
|
|
|
else: |
213
|
|
|
for dev in devices: |
214
|
|
|
if dev.lower() == device.lower(): |
215
|
|
|
return dev |
216
|
|
|
raise argparse.ArgumentError(argument=None, message="Invalid device.") |
217
|
|
|
|
218
|
|
|
|
219
|
|
|
def s2b(input_check): |
220
|
|
|
""" |
221
|
|
|
Return Boolean interpretation of string input. |
222
|
|
|
|
223
|
|
|
:param input_check: String to check if it means True or False. |
224
|
|
|
:type input_check: str |
225
|
|
|
""" |
226
|
|
|
return str(input_check).lower() in ("yes", "true", "t", "1", "y") |
227
|
|
|
|
228
|
|
|
|
229
|
|
|
def is_amd64(): |
230
|
|
|
""" |
231
|
|
|
Check if script is running on an AMD64 system. |
232
|
|
|
""" |
233
|
|
|
return platform.machine().endswith("64") |
234
|
|
|
|
235
|
|
|
|
236
|
|
|
def is_windows(): |
237
|
|
|
""" |
238
|
|
|
Check if script is running on Windows. |
239
|
|
|
""" |
240
|
|
|
return platform.system() == "Windows" |
241
|
|
|
|
242
|
|
|
|
243
|
|
|
def get_seven_zip(talkative=False): |
244
|
|
|
""" |
245
|
|
|
Return name of 7-Zip executable. |
246
|
|
|
On POSIX, it MUST be 7za. |
247
|
|
|
On Windows, it can be installed or supplied with the script. |
248
|
|
|
:func:`win_seven_zip` is used to determine if it's installed. |
249
|
|
|
|
250
|
|
|
:param talkative: Whether to output to screen. False by default. |
251
|
|
|
:type talkative: bool |
252
|
|
|
""" |
253
|
|
|
return win_seven_zip(talkative) if is_windows() else "7za" |
254
|
|
|
|
255
|
|
|
|
256
|
|
|
def win_seven_zip(talkative=False): |
257
|
|
|
""" |
258
|
|
|
For Windows, check where 7-Zip is ("where", pretty much). |
259
|
|
|
Consult registry first for any installed instances of 7-Zip. |
260
|
|
|
|
261
|
|
|
:param talkative: Whether to output to screen. False by default. |
262
|
|
|
:type talkative: bool |
263
|
|
|
""" |
264
|
|
|
if talkative: |
265
|
|
|
print("CHECKING INSTALLED FILES...") |
266
|
|
|
try: |
267
|
|
|
import winreg # windows registry |
268
|
|
|
hk7z = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\7-Zip") |
269
|
|
|
path = winreg.QueryValueEx(hk7z, "Path") |
270
|
|
|
except OSError as exc: |
271
|
|
|
if talkative: |
272
|
|
|
print("SOMETHING WENT WRONG") |
273
|
|
|
print(str(exc)) |
274
|
|
|
print("TRYING LOCAL FILES...") |
275
|
|
|
return win_seven_zip_local(talkative) |
276
|
|
|
else: |
277
|
|
|
if talkative: |
278
|
|
|
print("7ZIP USING INSTALLED FILES") |
279
|
|
|
return '"{0}"'.format(os.path.join(path[0], "7z.exe")) |
280
|
|
|
|
281
|
|
|
|
282
|
|
|
def win_seven_zip_local(talkative=False): |
283
|
|
|
""" |
284
|
|
|
If 7-Zip isn't in the registry, fall back onto supplied executables. |
285
|
|
|
If *those* aren't there, return "error". |
286
|
|
|
|
287
|
|
|
:param talkative: Whether to output to screen. False by default. |
288
|
|
|
:type talkative: bool |
289
|
|
|
""" |
290
|
|
|
listdir = os.listdir(os.getcwd()) |
291
|
|
|
filecount = 0 |
292
|
|
|
for i in listdir: |
293
|
|
|
if i in ["7za.exe", "7za64.exe"]: |
294
|
|
|
filecount += 1 |
295
|
|
|
if filecount == 2: |
296
|
|
|
if talkative: |
297
|
|
|
print("7ZIP USING LOCAL FILES") |
298
|
|
|
if is_amd64(): |
299
|
|
|
return "7za64.exe" |
300
|
|
|
else: |
301
|
|
|
return "7za.exe" |
302
|
|
|
else: |
303
|
|
|
if talkative: |
304
|
|
|
print("NO LOCAL FILES") |
305
|
|
|
return "error" |
306
|
|
|
|
307
|
|
|
|
308
|
|
|
def get_core_count(): |
309
|
|
|
""" |
310
|
|
|
Find out how many CPU cores this system has. |
311
|
|
|
""" |
312
|
|
|
try: |
313
|
|
|
cores = str(compat.enum_cpus()) # 3.4 and up |
314
|
|
|
except NotImplementedError: |
315
|
|
|
cores = "1" # 3.2-3.3 |
316
|
|
|
else: |
317
|
|
|
if compat.enum_cpus() is None: |
318
|
|
|
cores = "1" |
319
|
|
|
return cores |
320
|
|
|
|
321
|
|
|
|
322
|
|
|
def prep_seven_zip_path(path, talkative=False): |
323
|
|
|
""" |
324
|
|
|
Print p7zip path on POSIX, or notify if not there. |
325
|
|
|
|
326
|
|
|
:param path: Path to use. |
327
|
|
|
:type path: str |
328
|
|
|
|
329
|
|
|
:param talkative: Whether to output to screen. False by default. |
330
|
|
|
:type talkative: bool |
331
|
|
|
""" |
332
|
|
|
if path is None: |
333
|
|
|
if talkative: |
334
|
|
|
print("NO 7ZIP") |
335
|
|
|
print("PLEASE INSTALL p7zip") |
336
|
|
|
return False |
337
|
|
|
else: |
338
|
|
|
if talkative: |
339
|
|
|
print("7ZIP FOUND AT {0}".format(path)) |
340
|
|
|
return True |
341
|
|
|
|
342
|
|
|
|
343
|
|
|
def prep_seven_zip_posix(talkative=False): |
344
|
|
|
""" |
345
|
|
|
Check for p7zip on POSIX. |
346
|
|
|
|
347
|
|
|
:param talkative: Whether to output to screen. False by default. |
348
|
|
|
:type talkative: bool |
349
|
|
|
""" |
350
|
|
|
try: |
351
|
|
|
path = compat.where_which("7za") |
352
|
|
|
except ImportError: |
353
|
|
|
if talkative: |
354
|
|
|
print("PLEASE INSTALL SHUTILWHICH WITH PIP") |
355
|
|
|
return False |
356
|
|
|
else: |
357
|
|
|
return prep_seven_zip_path(path, talkative) |
358
|
|
|
|
359
|
|
|
|
360
|
|
|
def prep_seven_zip(talkative=False): |
361
|
|
|
""" |
362
|
|
|
Check for presence of 7-Zip. |
363
|
|
|
On POSIX, check for p7zip. |
364
|
|
|
On Windows, check for 7-Zip. |
365
|
|
|
|
366
|
|
|
:param talkative: Whether to output to screen. False by default. |
367
|
|
|
:type talkative: bool |
368
|
|
|
""" |
369
|
|
|
if is_windows(): |
370
|
|
|
return get_seven_zip(talkative) != "error" |
371
|
|
|
else: |
372
|
|
|
return prep_seven_zip_posix(talkative) |
373
|
|
|
|
374
|
|
|
|
375
|
|
|
def increment(version, inc=3): |
376
|
|
|
""" |
377
|
|
|
Increment version by given number. For repeated lookups. |
378
|
|
|
|
379
|
|
|
:param version: w.x.y.ZZZZ, becomes w.x.y.(ZZZZ + increment). |
380
|
|
|
:type version: str |
381
|
|
|
|
382
|
|
|
:param inc: What to increment by. Default is 3. |
383
|
|
|
:type inc: str |
384
|
|
|
""" |
385
|
|
|
splitos = version.split(".") |
386
|
|
|
splitos[3] = int(splitos[3]) |
387
|
|
|
if splitos[3] > 9996: # prevent overflow |
388
|
|
|
splitos[3] = 0 |
389
|
|
|
splitos[3] += int(inc) |
390
|
|
|
splitos[3] = str(splitos[3]) |
391
|
|
|
return ".".join(splitos) |
392
|
|
|
|
393
|
|
|
|
394
|
|
|
def stripper(name): |
395
|
|
|
""" |
396
|
|
|
Strip fluff from bar filename. |
397
|
|
|
|
398
|
|
|
:param name: Bar filename, must contain '-nto+armle-v7+signed.bar'. |
399
|
|
|
:type name: str |
400
|
|
|
""" |
401
|
|
|
return name.replace("-nto+armle-v7+signed.bar", "") |
402
|
|
|
|
403
|
|
|
|
404
|
|
|
def create_base_url(softwareversion): |
405
|
|
|
""" |
406
|
|
|
Make the root URL for production server files. |
407
|
|
|
|
408
|
|
|
:param softwareversion: Software version to hash. |
409
|
|
|
:type softwareversion: str |
410
|
|
|
""" |
411
|
|
|
# Hash software version |
412
|
|
|
swhash = hashlib.sha1(softwareversion.encode('utf-8')) |
413
|
|
|
hashedsoftwareversion = swhash.hexdigest() |
414
|
|
|
# Root of all urls |
415
|
|
|
baseurl = "http://cdn.fs.sl.blackberry.com/fs/qnx/production/{0}".format(hashedsoftwareversion) |
416
|
|
|
return baseurl |
417
|
|
|
|
418
|
|
|
|
419
|
|
|
def create_bar_url(softwareversion, appname, appversion): |
420
|
|
|
""" |
421
|
|
|
Make the URL for any production server file. |
422
|
|
|
|
423
|
|
|
:param softwareversion: Software version to hash. |
424
|
|
|
:type softwareversion: str |
425
|
|
|
|
426
|
|
|
:param appname: Application name, like on server (ex. sys.pim.calendar -> "calendar") |
427
|
|
|
:type appname: str |
428
|
|
|
|
429
|
|
|
:param appversion: Application version. |
430
|
|
|
:type appversion: str |
431
|
|
|
""" |
432
|
|
|
baseurl = create_base_url(softwareversion) |
433
|
|
|
return "{0}/{1}-{2}-nto+armle-v7+signed.bar".format(baseurl, appname, appversion) |
434
|
|
|
|
435
|
|
|
|
436
|
|
|
def generate_urls(softwareversion, osversion, radioversion, core=False): |
437
|
|
|
""" |
438
|
|
|
Generate a list of OS URLs and a list of radio URLs based on input. |
439
|
|
|
|
440
|
|
|
:param softwareversion: Software version to hash. |
441
|
|
|
:type softwareversion: str |
442
|
|
|
|
443
|
|
|
:param osversion: OS version. |
444
|
|
|
:type osversion: str |
445
|
|
|
|
446
|
|
|
:param radioversion: Radio version. |
447
|
|
|
:type radioversion: str |
448
|
|
|
|
449
|
|
|
:param core: Whether or not to return core URLs as well. |
450
|
|
|
:type core: bool |
451
|
|
|
""" |
452
|
|
|
osurls = [ |
453
|
|
|
create_bar_url(softwareversion, "winchester.factory_sfi.desktop", osversion), |
454
|
|
|
create_bar_url(softwareversion, "qc8960.factory_sfi.desktop", osversion), |
455
|
|
|
create_bar_url(softwareversion, "qc8960.factory_sfi.desktop", osversion), |
456
|
|
|
create_bar_url(softwareversion, "qc8974.factory_sfi.desktop", osversion) |
457
|
|
|
] |
458
|
|
|
radiourls = [ |
459
|
|
|
create_bar_url(softwareversion, "m5730", radioversion), |
460
|
|
|
create_bar_url(softwareversion, "qc8960", radioversion), |
461
|
|
|
create_bar_url(softwareversion, "qc8960.omadm", radioversion), |
462
|
|
|
create_bar_url(softwareversion, "qc8960.wtr", radioversion), |
463
|
|
|
create_bar_url(softwareversion, "qc8960.wtr5", radioversion), |
464
|
|
|
create_bar_url(softwareversion, "qc8930.wtr5", radioversion), |
465
|
|
|
create_bar_url(softwareversion, "qc8974.wtr2", radioversion) |
466
|
|
|
] |
467
|
|
|
coreurls = [] |
468
|
|
|
splitos = [int(i) for i in osversion.split(".")] |
469
|
|
|
osurls[2] = filter_1031(osurls[2], splitos, 5) |
470
|
|
|
osurls[3] = filter_1031(osurls[3], splitos, 6) |
471
|
|
|
if core: |
472
|
|
|
for url in osurls: |
473
|
|
|
coreurls.append(url.replace(".desktop", "")) |
474
|
|
|
return osurls, radiourls, coreurls |
475
|
|
|
|
476
|
|
|
|
477
|
|
|
def filter_1031(osurl, splitos, device): |
478
|
|
|
""" |
479
|
|
|
Modify URLs to reflect changes in 10.3.1+. |
480
|
|
|
|
481
|
|
|
:param osurl: OS URL to modify. |
482
|
|
|
:type osurl: str |
483
|
|
|
|
484
|
|
|
:param splitos: OS version, split and cast to int: [10, 3, 2, 2876] |
485
|
|
|
:type splitos: list(int) |
486
|
|
|
|
487
|
|
|
:param device: Device to use. |
488
|
|
|
:type device: int |
489
|
|
|
""" |
490
|
|
|
if (splitos[1] >= 4) or (splitos[1] == 3 and splitos[2] >= 1): |
491
|
|
|
if device == 5: |
492
|
|
|
osurl = osurl.replace("qc8960.factory_sfi", "qc8960.factory_sfi_hybrid_qc8x30") |
493
|
|
|
elif device == 6: |
494
|
|
|
osurl = osurl.replace("qc8974.factory_sfi", "qc8960.factory_sfi_hybrid_qc8974") |
495
|
|
|
return osurl |
496
|
|
|
|
497
|
|
|
|
498
|
|
|
def generate_lazy_urls(softwareversion, osversion, radioversion, device): |
499
|
|
|
""" |
500
|
|
|
Generate a pair of OS/radio URLs based on input. |
501
|
|
|
|
502
|
|
|
:param softwareversion: Software version to hash. |
503
|
|
|
:type softwareversion: str |
504
|
|
|
|
505
|
|
|
:param osversion: OS version. |
506
|
|
|
:type osversion: str |
507
|
|
|
|
508
|
|
|
:param radioversion: Radio version. |
509
|
|
|
:type radioversion: str |
510
|
|
|
|
511
|
|
|
:param device: Device to use. |
512
|
|
|
:type device: int |
513
|
|
|
""" |
514
|
|
|
splitos = [int(i) for i in osversion.split(".")] |
515
|
|
|
rads = ["m5730", "qc8960", "qc8960.omadm", "qc8960.wtr", |
516
|
|
|
"qc8960.wtr5", "qc8930.wtr4", "qc8974.wtr2"] |
517
|
|
|
oses = ["winchester.factory", "qc8960.factory", "qc8960.verizon", |
518
|
|
|
"qc8974.factory"] |
519
|
|
|
maps = {0:0, 1:1, 2:2, 3:1, 4:1, 5:1, 6:3} |
520
|
|
|
osurl = create_bar_url(softwareversion, "{0}_sfi.desktop".format(oses[maps[device]]), osversion) |
521
|
|
|
radiourl = create_bar_url(softwareversion, rads[device], radioversion) |
522
|
|
|
osurl = filter_1031(osurl, splitos, device) |
523
|
|
|
return osurl, radiourl |
524
|
|
|
|
525
|
|
|
|
526
|
|
|
def bulk_urls(softwareversion, osversion, radioversion, core=False, alturl=None): |
527
|
|
|
""" |
528
|
|
|
Generate all URLs, plus extra Verizon URLs. |
529
|
|
|
|
530
|
|
|
:param softwareversion: Software version to hash. |
531
|
|
|
:type softwareversion: str |
532
|
|
|
|
533
|
|
|
:param osversion: OS version. |
534
|
|
|
:type osversion: str |
535
|
|
|
|
536
|
|
|
:param radioversion: Radio version. |
537
|
|
|
:type radioversion: str |
538
|
|
|
|
539
|
|
|
:param device: Device to use. |
540
|
|
|
:type device: int |
541
|
|
|
|
542
|
|
|
:param core: Whether or not to return core URLs as well. |
543
|
|
|
:type core: bool |
544
|
|
|
|
545
|
|
|
:param alturl: The base URL for any alternate radios. |
546
|
|
|
:type alturl: str |
547
|
|
|
""" |
548
|
|
|
baseurl = create_base_url(softwareversion) |
549
|
|
|
osurls, radurls, coreurls = generate_urls(softwareversion, osversion, radioversion, core) |
550
|
|
|
vzwos, vzwrad = generate_lazy_urls(softwareversion, osversion, radioversion, 2) |
551
|
|
|
osurls.append(vzwos) |
552
|
|
|
radurls.append(vzwrad) |
553
|
|
|
vzwcore = vzwos.replace("sfi.desktop", "sfi") |
554
|
|
|
if core: |
555
|
|
|
coreurls.append(vzwcore) |
556
|
|
|
osurls = list(set(osurls)) # pop duplicates |
557
|
|
|
radurls = list(set(radurls)) |
558
|
|
|
if core: |
559
|
|
|
coreurls = list(set(coreurls)) |
560
|
|
|
if alturl is not None: |
561
|
|
|
altbase = create_base_url(alturl) |
|
|
|
|
562
|
|
|
radiourls2 = [] |
563
|
|
|
for rad in radurls: |
564
|
|
|
radiourls2.append(rad.replace(baseurl, alturl)) |
565
|
|
|
radurls = radiourls2 |
566
|
|
|
del radiourls2 |
567
|
|
|
return osurls, coreurls, radurls |
568
|
|
|
|
569
|
|
|
|
570
|
|
|
def line_begin(): |
571
|
|
|
""" |
572
|
|
|
Go to beginning of line, to overwrite whatever's there. |
573
|
|
|
""" |
574
|
|
|
sys.stdout.write("\r") |
575
|
|
|
sys.stdout.flush() |
576
|
|
|
|
577
|
|
|
|
578
|
|
|
def spinner_clear(): |
579
|
|
|
""" |
580
|
|
|
Get rid of any spinner residue left in stdout. |
581
|
|
|
""" |
582
|
|
|
sys.stdout.write("\b \b") |
583
|
|
|
sys.stdout.flush() |
584
|
|
|
|
585
|
|
|
|
586
|
|
|
class Spinner(object): |
587
|
|
|
""" |
588
|
|
|
A basic spinner using itertools. No need for progress. |
589
|
|
|
""" |
590
|
|
|
|
591
|
|
|
def __init__(self): |
592
|
|
|
self.wheel = itertools.cycle(['-', '/', '|', '\\']) |
593
|
|
|
self.file = dummy.UselessStdout() |
594
|
|
|
|
595
|
|
|
def after(self): |
596
|
|
|
""" |
597
|
|
|
Iterate over itertools.cycle, write to file. |
598
|
|
|
""" |
599
|
|
|
try: |
600
|
|
|
self.file.write(next(self.wheel)) |
601
|
|
|
self.file.flush() |
602
|
|
|
self.file.write("\b\r") |
603
|
|
|
self.file.flush() |
604
|
|
|
except (KeyboardInterrupt, SystemExit): |
605
|
|
|
self.stop() |
606
|
|
|
|
607
|
|
|
def stop(self): |
608
|
|
|
""" |
609
|
|
|
Kill output. |
610
|
|
|
""" |
611
|
|
|
self.file = dummy.UselessStdout() |
612
|
|
|
|
613
|
|
|
|
614
|
|
|
class SpinManager(object): |
615
|
|
|
""" |
616
|
|
|
Wraps around the itertools spinner, runs it in another thread. |
617
|
|
|
""" |
618
|
|
|
|
619
|
|
|
def __init__(self): |
620
|
|
|
spinner = Spinner() |
621
|
|
|
self.spinner = spinner |
622
|
|
|
self.thread = threading.Thread(target=self.loop, args=()) |
623
|
|
|
self.thread.daemon = True |
624
|
|
|
self.scanning = False |
625
|
|
|
self.spinner.file = dummy.UselessStdout() |
626
|
|
|
|
627
|
|
|
def start(self): |
628
|
|
|
""" |
629
|
|
|
Begin the spinner. |
630
|
|
|
""" |
631
|
|
|
self.spinner.file = sys.stderr |
632
|
|
|
self.scanning = True |
633
|
|
|
self.thread.start() |
634
|
|
|
|
635
|
|
|
def loop(self): |
636
|
|
|
""" |
637
|
|
|
Spin if scanning, clean up if not. |
638
|
|
|
""" |
639
|
|
|
while self.scanning: |
640
|
|
|
time.sleep(0.5) |
641
|
|
|
try: |
642
|
|
|
line_begin() |
643
|
|
|
self.spinner.after() |
644
|
|
|
except (KeyboardInterrupt, SystemExit): |
645
|
|
|
self.scanning = False |
646
|
|
|
self.stop() |
647
|
|
|
|
648
|
|
|
def stop(self): |
649
|
|
|
""" |
650
|
|
|
Stop the spinner. |
651
|
|
|
""" |
652
|
|
|
self.spinner.stop() |
653
|
|
|
self.scanning = False |
654
|
|
|
spinner_clear() |
655
|
|
|
line_begin() |
656
|
|
|
if not is_windows(): |
657
|
|
|
print("\n") |
658
|
|
|
|
659
|
|
|
|
660
|
|
|
def return_and_delete(target): |
661
|
|
|
""" |
662
|
|
|
Read text file, then delete it. Return contents. |
663
|
|
|
|
664
|
|
|
:param target: Text file to read. |
665
|
|
|
:type target: str |
666
|
|
|
""" |
667
|
|
|
with open(target, "r") as thefile: |
668
|
|
|
content = thefile.read() |
669
|
|
|
os.remove(target) |
670
|
|
|
return content |
671
|
|
|
|
672
|
|
|
|
673
|
|
|
def verify_loader_integrity(loaderfile): |
674
|
|
|
""" |
675
|
|
|
Test for created loader integrity. Windows-only. |
676
|
|
|
|
677
|
|
|
:param loaderfile: Path to loader. |
678
|
|
|
:type loaderfile: str |
679
|
|
|
""" |
680
|
|
|
if not is_windows(): |
681
|
|
|
pass |
682
|
|
|
else: |
683
|
|
|
excode = None |
684
|
|
|
try: |
685
|
|
|
with open(os.devnull, 'rb') as dnull: |
686
|
|
|
cmd = "{0} fileinfo".format(loaderfile) |
687
|
|
|
excode = subprocess.call(cmd, stdout=dnull, stderr=subprocess.STDOUT) |
688
|
|
|
except OSError: |
689
|
|
|
excode = -1 |
690
|
|
|
return excode == 0 # 0 if OK, non-zero if something broke |
691
|
|
|
|
692
|
|
|
|
693
|
|
|
def verify_bulk_loaders(ldir): |
694
|
|
|
""" |
695
|
|
|
Run :func:`verify_loader_integrity` for all files in a dir. |
696
|
|
|
|
697
|
|
|
:param ldir: Directory to use. |
698
|
|
|
:type ldir: str |
699
|
|
|
""" |
700
|
|
|
if not is_windows(): |
701
|
|
|
pass |
702
|
|
|
else: |
703
|
|
|
files = [os.path.join(ldir, file) for file in os.listdir(ldir) if not os.path.isdir(file)] |
704
|
|
|
brokens = [] |
705
|
|
|
for file in files: |
706
|
|
|
fname = os.path.basename(file) |
707
|
|
|
if fname.endswith(".exe") and fname.startswith(bbconstants.PREFIXES): |
708
|
|
|
print("TESTING: {0}".format(fname)) |
709
|
|
|
if not verify_loader_integrity(file): |
710
|
|
|
brokens.append(fname) |
711
|
|
|
return brokens |
712
|
|
|
|
713
|
|
|
|
714
|
|
|
def workers(input_data): |
715
|
|
|
""" |
716
|
|
|
Count number of CPU workers, smaller of number of threads and length of data. |
717
|
|
|
|
718
|
|
|
:param input_data: Input data, some iterable. |
719
|
|
|
:type input_data: list |
720
|
|
|
""" |
721
|
|
|
runners = len(input_data) if len(input_data) < compat.enum_cpus() else compat.enum_cpus() |
722
|
|
|
return runners |
723
|
|
|
|
724
|
|
|
|
725
|
|
|
def prep_logfile(): |
726
|
|
|
""" |
727
|
|
|
Prepare log file, labeling it with current date. Select folder based on frozen status. |
728
|
|
|
""" |
729
|
|
|
logfile = "{0}.txt".format(time.strftime("%Y_%m_%d_%H%M%S")) |
730
|
|
|
root = os.getcwd() if getattr(sys, 'frozen', False) else os.path.expanduser("~") |
731
|
|
|
basefolder = os.path.join(root, "lookuplogs") |
732
|
|
|
os.makedirs(basefolder, exist_ok=True) |
733
|
|
|
record = os.path.join(basefolder, logfile) |
734
|
|
|
open(record, "w").close() |
735
|
|
|
return record |
736
|
|
|
|
737
|
|
|
|
738
|
|
|
def prepends(file, pre, suf): |
739
|
|
|
""" |
740
|
|
|
Check if filename starts with/ends with stuff. |
741
|
|
|
|
742
|
|
|
:param file: File to check. |
743
|
|
|
:type file: str |
744
|
|
|
|
745
|
|
|
:param pre: Prefix(es) to check. |
746
|
|
|
:type pre: str or list or tuple |
747
|
|
|
|
748
|
|
|
:param suf: Suffix(es) to check. |
749
|
|
|
:type suf: str or list or tuple |
750
|
|
|
""" |
751
|
|
|
return file.startswith(pre) and file.endswith(suf) |
752
|
|
|
|
753
|
|
|
|
754
|
|
|
def lprint(iterable): |
755
|
|
|
""" |
756
|
|
|
A oneliner for 'for item in x: print item'. |
757
|
|
|
|
758
|
|
|
:param iterable: Iterable to print. |
759
|
|
|
:type iterable: list/tuple |
760
|
|
|
""" |
761
|
|
|
for item in iterable: |
762
|
|
|
print(item) |
763
|
|
|
|
764
|
|
|
|
765
|
|
|
def cappath_config_loader(homepath=None): |
766
|
|
|
""" |
767
|
|
|
Read a ConfigParser file to get cap preferences. |
768
|
|
|
|
769
|
|
|
:param homepath: Folder containing ini file. Default is user directory. |
770
|
|
|
:type homepath: str |
771
|
|
|
""" |
772
|
|
|
capini = iniconfig.generic_loader('cappath', homepath) |
773
|
|
|
cappath = capini.get('path', fallback=bbconstants.CAP.location) |
774
|
|
|
return cappath |
775
|
|
|
|
776
|
|
|
|
777
|
|
|
def cappath_config_writer(cappath=None, homepath=None): |
778
|
|
|
""" |
779
|
|
|
Write a ConfigParser file to store cap preferences. |
780
|
|
|
|
781
|
|
|
:param cappath: Method to use. |
782
|
|
|
:type cappath: str |
783
|
|
|
|
784
|
|
|
:param homepath: Folder containing ini file. Default is user directory. |
785
|
|
|
:type homepath: str |
786
|
|
|
""" |
787
|
|
|
cappath = grab_cap() if cappath is None else cappath |
788
|
|
|
results = {"path": cappath} |
789
|
|
|
iniconfig.generic_writer("cappath", results, homepath) |
790
|
|
|
|