Skip to content

Commit 8c8d3bd

Browse files
committed
flash: move metadata extension to where it is used
There is only one use case that uses the extended metadata.json, so we can simplify things by extending it only where it is actually needed.
1 parent 50e1f0e commit 8c8d3bd

File tree

3 files changed

+9
-23
lines changed

3 files changed

+9
-23
lines changed

pybricksdev/cli/flash.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Copyright (c) 2019-2022 The Pybricks Authors
33

44
import asyncio
5+
import hashlib
56
import json
67
import logging
78
import sys
@@ -310,10 +311,15 @@ async def flash_firmware(firmware_zip: BinaryIO, new_name: Optional[str]) -> Non
310311
bytearray(archive.open("install_pybricks.py").read()),
311312
)
312313

314+
extended_metadata = metadata.copy()
315+
316+
# Add extended metadata needed by install_pybricks.py
317+
extended_metadata["firmware-sha256"] = hashlib.sha256(firmware).hexdigest()
318+
313319
# Upload metadata.
314320
await hub.upload_file(
315321
"_firmware/firmware.metadata.json",
316-
json.dumps(metadata, indent=4).encode(),
322+
json.dumps(extended_metadata, indent=4).encode(),
317323
)
318324

319325
# Upload Pybricks firmware

pybricksdev/firmware.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,3 @@ def firmware_metadata_is_v2(
9797
metadata: AnyFirmwareMetadata,
9898
) -> TypeGuard[AnyFirmwareV2Metadata]:
9999
return metadata["metadata-version"].startswith("2.")
100-
101-
102-
class ExtendedFirmwareMetadata(
103-
FirmwareMetadataV110, TypedDict("Extended", {"firmware-sha256": str})
104-
):
105-
# NB: Ideally, this should inherit from AnyFirmwareMetadata instead of
106-
# FirmwareMetadataV110 but that is not technically possible because being
107-
# a Union it has a different meta class from TypedDict
108-
"""
109-
Type for data contained in ``firmware.metadata.json`` files of any version
110-
with extended data added.
111-
112-
The extended data is used by the ``install_pybricks.py`` script.
113-
"""

pybricksdev/flash.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# Copyright (c) 2019-2022 The Pybricks Authors
33

44
import asyncio
5-
import hashlib
65
import io
76
import json
87
import logging
@@ -24,7 +23,6 @@
2423
from .firmware import (
2524
AnyFirmwareV1Metadata,
2625
AnyFirmwareV2Metadata,
27-
ExtendedFirmwareMetadata,
2826
AnyFirmwareMetadata,
2927
firmware_metadata_is_v1,
3028
firmware_metadata_is_v2,
@@ -132,7 +130,7 @@ async def _create_firmware_v2(
132130

133131
async def create_firmware(
134132
firmware_zip: Union[str, os.PathLike, BinaryIO], name: Optional[str] = None
135-
) -> Tuple[bytes, ExtendedFirmwareMetadata]:
133+
) -> Tuple[bytes, AnyFirmwareMetadata]:
136134
"""Creates a firmware blob from base firmware and an optional custom name.
137135
138136
Arguments:
@@ -142,8 +140,7 @@ async def create_firmware(
142140
A custom name for the hub.
143141
144142
Returns:
145-
Composite binary blob with correct padding and checksum and
146-
extended metadata for this firmware file.
143+
Tuple of composite binary blob for flashing and the metadata.
147144
148145
Raises:
149146
ValueError:
@@ -166,9 +163,6 @@ async def create_firmware(
166163
f"unsupported metadata version: {metadata['metadata-version']}"
167164
)
168165

169-
# Add extended metadata needed by install_pybricks.py
170-
metadata["firmware-sha256"] = hashlib.sha256(firmware).hexdigest()
171-
172166
return firmware, metadata
173167

174168

0 commit comments

Comments
 (0)