1
|
|
|
#!/usr/bin/env python3 |
2
|
5 |
|
"""This module is used for JSON tools.""" |
3
|
|
|
|
4
|
5 |
|
import glob # filenames |
5
|
5 |
|
import os # path work |
6
|
5 |
|
import sys # frozen status |
7
|
|
|
|
8
|
5 |
|
from bbarchivist import bbconstants # file location |
9
|
5 |
|
from bbarchivist import decorators # enter to exit |
10
|
5 |
|
from bbarchivist import utilities # lprint |
11
|
|
|
|
12
|
5 |
|
try: |
13
|
5 |
|
import simplejson as json |
14
|
1 |
|
except ImportError: |
15
|
1 |
|
import json |
16
|
|
|
|
17
|
5 |
|
__author__ = "Thurask" |
18
|
5 |
|
__license__ = "WTFPL v2" |
19
|
5 |
|
__copyright__ = "2015-2019 Thurask" |
20
|
|
|
|
21
|
|
|
|
22
|
5 |
|
def grab_json(filename): |
23
|
|
|
""" |
24
|
|
|
Figure out where JSON is, local or system-supplied. |
25
|
|
|
|
26
|
|
|
:param filename: Desired JSON database name. |
27
|
|
|
:type filename: str |
28
|
|
|
""" |
29
|
5 |
|
jfile = None |
30
|
5 |
|
try: |
31
|
5 |
|
jfile = glob.glob(os.path.join(os.getcwd(), "json"))[0] |
32
|
5 |
|
except IndexError: |
33
|
5 |
|
jfile = bbconstants.JSONDIR |
34
|
5 |
|
jfile = os.path.join(jfile, "{0}.json".format(filename)) |
35
|
5 |
|
return os.path.abspath(jfile) |
36
|
|
|
|
37
|
|
|
|
38
|
5 |
|
def load_json(table, jfile=None): |
39
|
|
|
""" |
40
|
|
|
Load JSON file, return specific table (dict or list). |
41
|
|
|
|
42
|
|
|
:param table: Name of sub-structure to return. |
43
|
|
|
:type table: str |
44
|
|
|
|
45
|
|
|
:param jfile: Path to JSON file. |
46
|
|
|
:type jfile: str |
47
|
|
|
""" |
48
|
5 |
|
if jfile is None: |
49
|
5 |
|
jfile = grab_json(table) |
50
|
5 |
|
with open(jfile) as thefile: |
51
|
5 |
|
data = json.load(thefile) |
52
|
5 |
|
return data[table] |
53
|
|
|
|
54
|
|
|
|
55
|
5 |
|
def extract_cert_secret(key, device): |
56
|
|
|
""" |
57
|
|
|
Check if device is marked as secret. |
58
|
|
|
|
59
|
|
|
:param key: Device entry. |
60
|
|
|
:type key: dict |
61
|
|
|
|
62
|
|
|
:param device: HWID, FCCID or name of device. |
63
|
|
|
:param device: str |
64
|
|
|
""" |
65
|
5 |
|
not_secret = device in key['name'] and 'secret' not in key |
66
|
5 |
|
return not_secret |
67
|
|
|
|
68
|
|
|
|
69
|
5 |
|
def extract_cert_check(key, device, not_secret): |
70
|
|
|
""" |
71
|
|
|
Check function for extracting PTCRB info. |
72
|
|
|
|
73
|
|
|
:param key: Device entry. |
74
|
|
|
:type key: dict |
75
|
|
|
|
76
|
|
|
:param device: HWID, FCCID or name of device. |
77
|
|
|
:type device: str |
78
|
|
|
|
79
|
|
|
:param not_secret: If device is not market as secret. |
80
|
|
|
:type not_secret: bool |
81
|
|
|
""" |
82
|
5 |
|
keylist = key['hwid'], key['fccid'], key['ptcrbid'] |
83
|
5 |
|
goforit = (device in keylist or not_secret) and key['ptcrbid'] |
84
|
5 |
|
return goforit |
85
|
|
|
|
86
|
|
|
|
87
|
5 |
|
def extract_cert(table, device): |
88
|
|
|
""" |
89
|
|
|
Extract PTCRB info from a list of dicts. |
90
|
|
|
|
91
|
|
|
:param table: List of device entries. |
92
|
|
|
:type table: list(dict) |
93
|
|
|
|
94
|
|
|
:param device: HWID, FCCID or name of device. |
95
|
|
|
:type device: str |
96
|
|
|
""" |
97
|
5 |
|
for key in table: |
98
|
5 |
|
not_secret = extract_cert_secret(key, device) |
99
|
5 |
|
goforit = extract_cert_check(key, device, not_secret) |
100
|
5 |
|
if goforit: |
101
|
5 |
|
device = key['device'] |
102
|
5 |
|
name = key['name'] |
103
|
5 |
|
ptcrbid = key['ptcrbid'] |
104
|
5 |
|
hwid = key['hwid'] |
105
|
5 |
|
fccid = key['fccid'] |
106
|
5 |
|
break |
107
|
|
|
else: |
108
|
5 |
|
fubar("NO PTCRBID!") |
109
|
5 |
|
return name, ptcrbid, hwid, fccid |
110
|
|
|
|
111
|
|
|
|
112
|
5 |
|
def list_available_certs(table): |
113
|
|
|
""" |
114
|
|
|
List all certified devices in a device table. |
115
|
|
|
|
116
|
|
|
:param table: List of device entries. |
117
|
|
|
:type table: list(dict) |
118
|
|
|
""" |
119
|
5 |
|
for key in table: |
120
|
5 |
|
if key['ptcrbid']: |
121
|
5 |
|
hwid = "NO HWID" if not key['hwid'] else key['hwid'] |
122
|
5 |
|
print("{0} {1} - {2} - {3}".format(key['device'], key['name'], hwid, key['fccid'])) |
123
|
|
|
|
124
|
|
|
|
125
|
5 |
|
def list_devices(table): |
126
|
|
|
""" |
127
|
|
|
List all devices, certified or not, in a device table. |
128
|
|
|
|
129
|
|
|
:param table: List of device entries. |
130
|
|
|
:type table: list(dict) |
131
|
|
|
""" |
132
|
5 |
|
for key in table: |
133
|
5 |
|
hwid = "NO HWID" if not key['hwid'] else key['hwid'] |
134
|
5 |
|
fccid = "NO FCCID" if not key['fccid'] else key['fccid'] |
135
|
5 |
|
print("{0} {1} - {2} - {3}".format(key['device'], key['name'], hwid, fccid)) |
136
|
|
|
|
137
|
|
|
|
138
|
5 |
|
def list_prds(table): |
139
|
|
|
""" |
140
|
|
|
List all PRDs in a PRD table. |
141
|
|
|
|
142
|
|
|
:param table: Dictionary of device : PRD list pairs. |
143
|
|
|
:type table: dict(str: list) |
144
|
|
|
""" |
145
|
5 |
|
for key in table.keys(): |
146
|
5 |
|
print("~{0}~".format(key)) |
147
|
5 |
|
for prd in table[key]: |
148
|
5 |
|
print(prd) |
149
|
|
|
|
150
|
|
|
|
151
|
5 |
|
def certchecker_prep(table, device): |
152
|
|
|
""" |
153
|
|
|
Extract model, family and HWID from a device table. |
154
|
|
|
|
155
|
|
|
:param table: List of device entries. |
156
|
|
|
:type table: list(dict) |
157
|
|
|
|
158
|
|
|
:param device: HWID, FCCID or name of device. |
159
|
|
|
:type device: str |
160
|
|
|
""" |
161
|
5 |
|
for key in table: |
162
|
5 |
|
if 'secret' not in key and key['name'] == device: |
163
|
5 |
|
model = key['device'] |
164
|
5 |
|
family = key['family'] |
165
|
5 |
|
hwid = key['hwid'] |
166
|
5 |
|
break |
167
|
|
|
else: |
168
|
5 |
|
fubar("INVALID DEVICE!") |
169
|
5 |
|
return model, family, hwid |
170
|
|
|
|
171
|
|
|
|
172
|
5 |
|
def read_family(table, device): |
173
|
|
|
""" |
174
|
|
|
Get all devices of a given family in a device table. |
175
|
|
|
|
176
|
|
|
:param table: List of device entries. |
177
|
|
|
:type table: list(dict) |
178
|
|
|
|
179
|
|
|
:param device: HWID, FCCID or name of device. |
180
|
|
|
:type device: str |
181
|
|
|
""" |
182
|
5 |
|
famlist = [key['fccid'] for key in table if key['ptcrbid'] and key['device'] == device] |
183
|
5 |
|
return famlist |
184
|
|
|
|
185
|
|
|
|
186
|
5 |
|
def list_family(table): |
187
|
|
|
""" |
188
|
|
|
List all valid (certified) families in a device table. |
189
|
|
|
|
190
|
|
|
:param table: List of device entries. |
191
|
|
|
:type table: list(dict) |
192
|
|
|
""" |
193
|
5 |
|
famlist = list({key['device'] for key in table if 'secret' not in key and key['ptcrbid']}) |
194
|
5 |
|
utilities.lprint(famlist) |
195
|
|
|
|
196
|
|
|
|
197
|
5 |
|
def fubar(message): |
198
|
|
|
""" |
199
|
|
|
What to do when things go bad. |
200
|
|
|
|
201
|
|
|
:param message: Error message. |
202
|
|
|
:type message: str |
203
|
|
|
""" |
204
|
5 |
|
print(message) |
205
|
5 |
|
decorators.enter_to_exit(True) |
206
|
5 |
|
if not getattr(sys, 'frozen', False): |
207
|
|
|
raise SystemExit |
208
|
|
|
|