Files
romm/backend/handler/filesystem/firmware_handler.py
Georges-Antoine Assi 568aa818de refactor: tighten the library structure naming and docstrings
Rename `FirmwareTemplate.platform_path` to `firmware_dir`: it returns the
firmware folder, while the same name on `StructureTemplate` returns the platform
folder. Name the watcher's index for the segment it points at.

Fold the two reads of a raw structure key into `_raw_template`, and drop the
docstrings that only restated a one-line delegation or a signature.

Remove `footer-create-summary-default`, which no component references and whose
copy still named Structure A.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-08 08:53:55 -04:00

55 lines
1.9 KiB
Python

import binascii
import hashlib
from config import LIBRARY_BASE_PATH
from config.config_manager import config_manager as cm
from exceptions.fs_exceptions import FirmwareNotFoundException
from utils.hashing import crc32_to_hex
from .base_handler import FSHandler
class FSFirmwareHandler(FSHandler):
def __init__(self) -> None:
super().__init__(base_path=LIBRARY_BASE_PATH)
def get_firmware_fs_structure(self, fs_slug: str) -> str:
return cm.get_config().firmware_structure.firmware_dir(fs_slug)
async def get_firmware(self, platform_fs_slug: str):
"""Gets all filesystem firmware for a platform
Args:
platform: platform where firmware belong
Returns:
list with all the filesystem firmware for a platform
"""
firmware_path = self.get_firmware_fs_structure(platform_fs_slug)
try:
fs_firmware_files = await self.list_files(path=firmware_path)
except FileNotFoundError as e:
raise FirmwareNotFoundException(
f"Firmware not found for platform {platform_fs_slug}"
) from e
return [f for f in self.exclude_single_files(fs_firmware_files)]
async def calculate_file_hashes(self, firmware_path: str, file_name: str) -> dict:
file_path = f"{firmware_path}/{file_name}"
async with await self.stream_file(file_path=file_path) as f:
crc_c = 0
md5_h = hashlib.md5(usedforsecurity=False)
sha1_h = hashlib.sha1(usedforsecurity=False)
# Read in chunks to avoid memory issues
while chunk := await f.read(8192):
md5_h.update(chunk)
sha1_h.update(chunk)
crc_c = binascii.crc32(chunk, crc_c)
return {
"crc_hash": crc32_to_hex(crc_c),
"md5_hash": md5_h.hexdigest(),
"sha1_hash": sha1_h.hexdigest(),
}