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

35 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-09-04 16:33 -0700

1"""GenericEmitter — pure layout planner for the EMIT phase. 

2 

3``plan_layout`` scans *output_dir* (files already materialised by the 

4executor) and returns a ``LayoutPlan`` describing where each file goes 

5in the final organised tree. No I/O is performed here. 

6 

7``emit_metadata`` is a no-op for targets that use ``MetadataDialect.NONE``. 

8 

9``post_process`` returns an empty list unless the profile opts in to 

10post-processing hooks (Phase 5 stub — add jdupes/rsync hooks here later). 

11""" 

12 

13from __future__ import annotations 

14 

15import logging 

16from pathlib import Path 

17from typing import TYPE_CHECKING 

18 

19from romfarmer.ir.layout import LayoutEntry, LayoutPlan 

20 

21if TYPE_CHECKING: 

22 from romfarmer.analysis.knowledge import KnowledgeBase 

23 from romfarmer.ir.actions import ArtifactDecl 

24 from romfarmer.targets.emitters.base import PostHook 

25 from romfarmer.targets.profiles.loader import ConcreteTargetProfile 

26 

27logger = logging.getLogger(__name__) 

28 

29# ROM file extensions to include in the layout 

30_ROM_EXTS: frozenset[str] = frozenset( 

31 { 

32 ".chd", 

33 ".m3u", 

34 ".iso", 

35 ".cso", 

36 ".cue", 

37 ".bin", 

38 ".rvz", 

39 ".wua", 

40 ".wbfs", 

41 ".gcm", 

42 ".xiso", 

43 ".wux", 

44 ".7z", 

45 ".zip", 

46 ".nes", 

47 ".sfc", 

48 ".smc", 

49 ".gb", 

50 ".gbc", 

51 ".gba", 

52 ".nds", 

53 ".3ds", 

54 ".nsp", 

55 ".xci", 

56 ".n64", 

57 ".z64", 

58 ".v64", 

59 ".rom", 

60 ".img", 

61 ".a26", 

62 ".a52", 

63 ".lnx", 

64 ".ps3", # PS3 JB folder marker 

65 } 

66) 

67 

68 

69class GenericEmitter: 

70 """Layout planner for generic file-based targets. 

71 

72 Works for any target where the organisation style is determined by 

73 the ``ConcreteTargetProfile.organisation_style`` field. 

74 """ 

75 

76 def plan_layout( 

77 self, 

78 output_dir: Path, 

79 profile: ConcreteTargetProfile, 

80 ) -> LayoutPlan: 

81 """Scan *output_dir* and return a ``LayoutPlan``. 

82 

83 The layout maps each artifact's sha256 to a relative output path. 

84 Files not in ``_ROM_EXTS`` (e.g. .xml, .jpg) are excluded from 

85 the plan — metadata emitters handle those separately. 

86 """ 

87 

88 entries: list[LayoutEntry] = [] 

89 if not output_dir.exists(): 

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

91 

92 for f in sorted(output_dir.iterdir()): 

93 if not f.is_file(): 

94 continue 

95 if f.suffix.lower() not in _ROM_EXTS: 

96 continue 

97 try: 

98 sha256 = _sha256_file(f) 

99 rel = Path(f.name) 

100 entries.append(LayoutEntry(artifact_sha256=sha256, relative_path=rel)) 

101 except OSError as exc: 

102 logger.warning("GenericEmitter.plan_layout: could not hash %s: %s", f, exc) 

103 

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

105 

106 def emit_metadata( 

107 self, 

108 layout: LayoutPlan, 

109 output_dir: Path, 

110 kb: KnowledgeBase, 

111 profile: ConcreteTargetProfile, 

112 ) -> list[ArtifactDecl]: 

113 """No-op for targets with ``MetadataDialect.NONE``.""" 

114 return [] 

115 

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

117 return [] 

118 

119 

120def _sha256_file(path: Path) -> str: 

121 import hashlib 

122 

123 h = hashlib.sha256() 

124 with open(path, "rb") as f: 

125 for chunk in iter(lambda: f.read(1_048_576), b""): 

126 h.update(chunk) 

127 return h.hexdigest()