Coverage for src/romfarmer/targets/emitters/extras.py: 0%

31 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-09-01 23:08 -0700

1"""ExtrasEmitter — places DLC, updates, and extras alongside the main output. 

2 

3Ports the core of ``stages/emit_extras.py``. An 'extras' platform is one 

4where ``resolved.extras`` is set in the build config; its files are not 

5DAT-filtered but placed in a subdirectory alongside the main platform output. 

6 

7For Phase 5, this is a minimal port — it copies extras files to a 

8``_extras/`` subdirectory under *output_dir* and returns a ``LayoutPlan`` 

9covering those files. 

10""" 

11 

12from __future__ import annotations 

13 

14import logging 

15from pathlib import Path 

16from typing import TYPE_CHECKING 

17 

18from romfarmer.ir.layout import LayoutEntry, LayoutPlan 

19 

20if TYPE_CHECKING: 

21 from romfarmer.analysis.knowledge import KnowledgeBase 

22 from romfarmer.ir.actions import ArtifactDecl 

23 from romfarmer.targets.emitters.base import PostHook 

24 from romfarmer.targets.profiles.loader import ConcreteTargetProfile 

25 

26logger = logging.getLogger(__name__) 

27 

28 

29class ExtrasEmitter: 

30 """Places extras/DLC files into ``<output_dir>/_extras/``.""" 

31 

32 def __init__(self, extras_source_dir: Path | None = None) -> None: 

33 self._source = extras_source_dir 

34 

35 def plan_layout( 

36 self, 

37 output_dir: Path, 

38 profile: ConcreteTargetProfile, 

39 ) -> LayoutPlan: 

40 """Return a LayoutPlan for extras files — no I/O performed here.""" 

41 import hashlib 

42 

43 if self._source is None or not self._source.exists(): 

44 return LayoutPlan(root_name=output_dir.name, entries=()) 

45 

46 entries: list[LayoutEntry] = [] 

47 for f in sorted(self._source.rglob("*")): 

48 if not f.is_file(): 

49 continue 

50 try: 

51 h = hashlib.sha256() 

52 with open(f, "rb") as fh: 

53 for chunk in iter(lambda: fh.read(65536), b""): 

54 h.update(chunk) 

55 rel = Path("_extras") / f.relative_to(self._source) 

56 entries.append(LayoutEntry(artifact_sha256=h.hexdigest(), relative_path=rel)) 

57 except OSError as exc: 

58 logger.debug("ExtrasEmitter.plan_layout: %s: %s", f, exc) 

59 

60 return LayoutPlan(root_name=output_dir.name, entries=tuple(entries)) 

61 

62 def emit_metadata( 

63 self, 

64 layout: LayoutPlan, 

65 output_dir: Path, 

66 kb: KnowledgeBase, 

67 profile: ConcreteTargetProfile, 

68 ) -> list[ArtifactDecl]: 

69 """No-op — extras don't need gamelist entries.""" 

70 return [] 

71 

72 def post_process(self, root: Path) -> list[PostHook]: 

73 return []