-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow parsing of RAUC files using `PySquashFsImage`. DB still needs migration.
- Loading branch information
1 parent
f7a0ce7
commit 31ec99d
Showing
15 changed files
with
152 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import logging | ||
|
||
import aiofiles | ||
import httpx | ||
from anyio import Path, open_file | ||
|
||
from ...db.models import SoftwareImageFormat | ||
from . import rauc, swu | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
async def parse_remote(url: str): | ||
async with httpx.AsyncClient() as c: | ||
file = await c.get(url) | ||
async with aiofiles.tempfile.NamedTemporaryFile("w+b") as f: | ||
await f.write(file.content) | ||
return await parse_file(Path(str(f.name))) | ||
|
||
|
||
async def parse_file(file: Path): | ||
async with await open_file(file, "r+b") as f: | ||
magic = await f.read(4) | ||
if magic == swu.MAGIC: | ||
image_format = SoftwareImageFormat.SWU | ||
attributes = await swu.parse_file(file) | ||
elif magic == rauc.MAGIC: | ||
image_format = SoftwareImageFormat.RAUC | ||
attributes = await rauc.parse_file(file) | ||
else: | ||
logger.warning(f"Unknown file format, magic={magic}") | ||
raise ValueError(f"Unknown file format, magic={magic}") | ||
attributes["image_format"] = image_format | ||
return attributes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import hashlib | ||
|
||
from anyio import AsyncFile | ||
|
||
|
||
async def sha1_hash_file(fileobj: AsyncFile): | ||
last = await fileobj.tell() | ||
await fileobj.seek(0) | ||
sha1_hash = hashlib.sha1() | ||
buf = bytearray(2**18) | ||
view = memoryview(buf) | ||
while True: | ||
size = await fileobj.readinto(buf) | ||
if size == 0: | ||
break | ||
sha1_hash.update(view[:size]) | ||
|
||
await fileobj.seek(last) | ||
return sha1_hash.hexdigest() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import configparser | ||
import logging | ||
|
||
import semver | ||
from anyio import Path, open_file | ||
from PySquashfsImage import SquashFsImage | ||
|
||
from .func import sha1_hash_file | ||
|
||
MAGIC = b"hsqs" | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
async def parse_file(file: Path): | ||
async with await open_file(file, "r+b") as f: | ||
image_data = await f.read() | ||
|
||
image = SquashFsImage.from_bytes(image_data) | ||
manifest = image.select("manifest.raucm") | ||
manifest_str = manifest.read_bytes().decode("utf-8") | ||
config = configparser.ConfigParser() | ||
config.read_string(manifest_str) | ||
swdesc_attrs = parse_descriptor(config) | ||
|
||
stat = await file.stat() | ||
swdesc_attrs["size"] = stat.st_size | ||
swdesc_attrs["hash"] = await sha1_hash_file(f) | ||
return swdesc_attrs | ||
|
||
|
||
def parse_descriptor(manifest: configparser.ConfigParser): | ||
swdesc_attrs = {} | ||
try: | ||
swdesc_attrs["version"] = semver.Version.parse(manifest["system"].get("version")) | ||
swdesc_attrs["compatibility"] = [{"hw_model": "default", "hw_revision": manifest["system"]["compatible"]}] | ||
except KeyError: | ||
try: | ||
swdesc_attrs["version"] = semver.Version.parse(manifest["update"].get("version")) | ||
swdesc_attrs["compatibility"] = [{"hw_model": "default", "hw_revision": manifest["update"]["compatible"]}] | ||
except KeyError as e: | ||
logger.warning(f"Parsing RAUC descriptor failed, error={e}") | ||
raise ValueError("Parsing RAUC descriptor failed", e) | ||
|
||
return swdesc_attrs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import pytest | ||
from anyio import Path | ||
|
||
from goosebit.updates.swdesc.rauc import parse_file | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_parse_software_header(): | ||
resolved = await Path(__file__).resolve() | ||
swdesc_attrs = await parse_file(resolved.parent / "software-header.raucb") | ||
assert str(swdesc_attrs["version"]) == "8.8.1-11-g8c926e5+188370" | ||
assert swdesc_attrs["compatibility"] == [ | ||
{"hw_model": "default", "hw_revision": "rauc-test-goosebit"}, | ||
] |