Skip to content

Commit 0df6b78

Browse files
CDN script fixes (ByteWelder#412)
1 parent 7918451 commit 0df6b78

File tree

2 files changed

+52
-39
lines changed

2 files changed

+52
-39
lines changed

Buildscripts/CDN/generate-files.py

Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from dataclasses import dataclass, asdict
55
import json
66
import shutil
7+
from configparser import ConfigParser, RawConfigParser
78

89
VERBOSE = False
910
DEVICES_FOLDER = "Boards"
@@ -69,9 +70,32 @@ def exit_with_error(message):
6970

7071
def read_properties_file(path):
7172
config = configparser.RawConfigParser()
73+
# Don't convert keys to lowercase
74+
config.optionxform = str
7275
config.read(path)
7376
return config
7477

78+
def get_property_or_none(properties: RawConfigParser, group: str, key: str):
79+
if group not in properties.sections():
80+
return None
81+
if key not in properties[group].keys():
82+
return None
83+
return properties[group][key]
84+
85+
def get_boolean_property_or_false(properties: RawConfigParser, group: str, key: str):
86+
if group not in properties.sections():
87+
return False
88+
if key not in properties[group].keys():
89+
return False
90+
return properties[group][key] == "true"
91+
92+
def get_property_or_exit(properties: RawConfigParser, group: str, key: str):
93+
if group not in properties.sections():
94+
exit_with_error(f"Device properties does not contain group: {group}")
95+
if key not in properties[group].keys():
96+
exit_with_error(f"Device properties does not contain key: {key}")
97+
return properties[group][key]
98+
7599
def read_device_properties(device_id):
76100
mapping_file_path = os.path.join(DEVICES_FOLDER, device_id, "device.properties")
77101
if not os.path.isfile(mapping_file_path):
@@ -106,18 +130,21 @@ def to_manifest_chip_name(name):
106130
return ""
107131

108132

109-
def process_device(in_path: str, out_path: str, device_directory: str, device_id: str, device_properties: configparser, version: str):
133+
def process_device(in_path: str, out_path: str, device_directory: str, device_id: str, device_properties: RawConfigParser, version: str):
110134
in_device_path = os.path.join(in_path, device_directory)
111135
in_device_binaries_path = os.path.join(in_device_path, "Binaries")
112-
assert os.path.isdir(in_device_binaries_path)
136+
if not os.path.isdir(in_device_binaries_path):
137+
exit_with_error(f"Could not find directory {in_device_binaries_path}")
113138
flasher_args_path = os.path.join(in_device_binaries_path, "flasher_args.json")
114-
assert os.path.isfile(flasher_args_path)
139+
if not os.path.isfile(flasher_args_path):
140+
exit_with_error(f"Could not find flasher arguments path {flasher_args_path}")
115141
with open(flasher_args_path) as json_data:
116142
flasher_args = json.load(json_data)
117-
json_data.close()
118143
flash_files = flasher_args["flash_files"]
144+
device_vendor = get_property_or_exit(device_properties, "general", "vendor")
145+
device_name = get_property_or_exit(device_properties, "general", "name")
119146
manifest = Manifest(
120-
name=f"Tactility for {device_properties["general"]["vendor"]} {device_properties["general"]["name"]}",
147+
name=f"Tactility for {device_vendor} {device_name}",
121148
version=version,
122149
new_install_prompt_erase="true",
123150
funding_url="https://github.com/sponsors/ByteWelder",
@@ -143,11 +170,9 @@ def process_device(in_path: str, out_path: str, device_directory: str, device_id
143170
offset=int(offset, 16)
144171
)
145172
)
146-
147173
json_manifest_path = os.path.join(out_path, f"{device_id}.json")
148174
with open(json_manifest_path, 'w') as json_manifest_file:
149175
json.dump(asdict(manifest), json_manifest_file, indent=2)
150-
json_manifest_file.close()
151176

152177
def main(in_path: str, out_path: str, version: str):
153178
if not os.path.exists(in_path):
@@ -158,39 +183,29 @@ def main(in_path: str, out_path: str, version: str):
158183
device_directories = os.listdir(in_path)
159184
device_index = DeviceIndex(version, [])
160185
for device_directory in device_directories:
161-
if not device_directory.endswith("-symbols"):
162-
device_id = device_directory[10:]
163-
device_properties = read_device_properties(device_id)
164-
device_properties_general = device_properties["general"]
165-
process_device(in_path, out_path, device_directory, device_id, device_properties, version)
166-
if device_properties.has_section("cdn"):
167-
device_properties_cdn = device_properties["cdn"]
168-
if "warningMessage" in device_properties_cdn.keys():
169-
warning_message = device_properties_cdn["warningMessage"]
170-
else:
171-
warning_message = None
172-
if "infoMessage" in device_properties_cdn.keys():
173-
info_message = device_properties_cdn["infoMessage"]
174-
else:
175-
info_message = None
176-
if "incubating" in device_properties_general.keys():
177-
incubating = device_properties_general["incubating"].lower() == 'true'
178-
else:
179-
incubating = False
180-
device_names = device_properties_general["name"].split(',')
181-
for device_name in device_names:
182-
device_index.devices.append(asdict(IndexEntry(
183-
id=device_id,
184-
name=device_name,
185-
vendor=device_properties_general["vendor"],
186-
incubating=incubating,
187-
warningMessage=warning_message,
188-
infoMessage=info_message
189-
)))
186+
if device_directory.endswith("-symbols"):
187+
continue
188+
device_id = device_directory.removeprefix("Tactility-")
189+
if not device_id:
190+
exit_with_error(f"Cannot derive device id from directory: {device_directory}")
191+
device_properties = read_device_properties(device_id)
192+
process_device(in_path, out_path, device_directory, device_id, device_properties, version)
193+
warning_message = get_property_or_none(device_properties, "cdn", "warningMessage")
194+
info_message = get_property_or_none(device_properties, "cdn", "infoMessage")
195+
incubating = get_boolean_property_or_false(device_properties, "general", "incubating")
196+
device_names = get_property_or_exit(device_properties, "general", "name").split(',')
197+
for device_name in device_names:
198+
device_index.devices.append(asdict(IndexEntry(
199+
id=device_id,
200+
name=device_name.strip(),
201+
vendor=get_property_or_exit(device_properties, "general", "vendor"),
202+
incubating=incubating,
203+
warningMessage=warning_message,
204+
infoMessage=info_message
205+
)))
190206
index_file_path = os.path.join(out_path, "index.json")
191207
with open(index_file_path, "w") as index_file:
192208
json.dump(asdict(device_index), index_file, indent=2)
193-
index_file.close()
194209

195210
if __name__ == "__main__":
196211
print("Tactility CDN File Generator")

device.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ def get_properties_file_path(device_id: str):
3737
def read_file(path: str):
3838
with open(path, "r") as file:
3939
result = file.read()
40-
file.close()
4140
return result
4241

4342
def read_properties_file(path):
@@ -229,7 +228,6 @@ def main(device_id: str, is_dev: bool):
229228
device_properties = read_device_properties(device_id)
230229
with open(output_file_path, "w") as output_file:
231230
write_properties(output_file, device_properties, device_id, is_dev)
232-
output_file.close()
233231

234232
if __name__ == "__main__":
235233
if "--help" in sys.argv:

0 commit comments

Comments
 (0)