Skip to content

Commit

Permalink
format fingerprints: simplify sorting (commaai#30826)
Browse files Browse the repository at this point in the history
* unique + sort + don't modify FW_VERSIONS

* clean up

* not here

* self expl
  • Loading branch information
sshane authored Dec 20, 2023
1 parent cfc85f0 commit b38c580
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions selfdrive/debug/format_fingerprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
{% for key, fw_versions in FW_VERSIONS[brand][car].items() %}
(Ecu.{{ECU_NAME[key[0]]}}, 0x{{"%0x" | format(key[1] | int)}}, \
{% if key[2] %}0x{{"%0x" | format(key[2] | int)}}{% else %}{{key[2]}}{% endif %}): [
{% for fw_version in fw_versions %}
{% for fw_version in (fw_versions + extra_fw_versions.get(car, {}).get(key, [])) | unique | sort %}
{{fw_version}},
{% endfor %}
],
Expand All @@ -65,26 +65,16 @@


def format_brand_fw_versions(brand, extra_fw_versions: None | dict[str, dict[tuple, list[bytes]]] = None):
fw_versions = FW_VERSIONS.copy()
# TODO: this modifies FW_VERSIONS in place
if extra_fw_versions is not None:
for platform, ecus in extra_fw_versions.items():
for ecu, fws in ecus.items():
fw_versions[brand][platform][ecu] += set(fws) - set(fw_versions[brand][platform][ecu])
extra_fw_versions = extra_fw_versions or {}

fingerprints_file = os.path.join(BASEDIR, f"selfdrive/car/{brand}/fingerprints.py")
with open(fingerprints_file, "r") as f:
comments = [line for line in f.readlines() if line.startswith("#") and "noqa" not in line]

# sort fw versions
if fw_versions[brand] is not None:
for platform in fw_versions[brand]:
for ecu in fw_versions[brand][platform]:
fw_versions[brand][platform][ecu] = sorted(fw_versions[brand][platform][ecu])

with open(fingerprints_file, "w") as f:
f.write(FINGERPRINTS_PY_TEMPLATE.render(brand=brand, comments=comments, ECU_NAME=ECU_NAME,
FINGERPRINTS=FINGERPRINTS, FW_VERSIONS=fw_versions))
FINGERPRINTS=FINGERPRINTS, FW_VERSIONS=FW_VERSIONS,
extra_fw_versions=extra_fw_versions))


if __name__ == "__main__":
Expand Down

0 comments on commit b38c580

Please sign in to comment.