Coverage for src/romfarmer/config/build_spec.py: 95%
41 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-09-05 02:19 -0700
« prev ^ index » next coverage.py v7.15.4, created at 2026-09-05 02:19 -0700
1"""Build specification — declarative build intent.
3A BuildSpec declares WHAT to build: which recipes, which target, what
4constraints. It's intentionally small and declarative.
6Compare with the old BuildConfig god object (30+ Optional fields):
7a BuildSpec is ~10 fields that express pure intent.
9Examples:
10 # Full production build: ~10 lines
11 name: batocera-1tb
12 target: batocera-pc
13 recipes:
14 - nointro-7z
15 - redump-chd
16 - nintendo-disc-rvz
17 - arcade-fbneo
19 # Test build: 6 lines
20 name: test-saturn-10
21 target: batocera-pc
22 recipes: [redump-chd, test-10]
23 platforms: [saturn]
24"""
26from pathlib import Path
27from typing import Any
29from pydantic import BaseModel, Field, model_validator
31from .models import (
32 GenerationFilterConfig,
33 SelectionConfig,
34)
37class DeployConfig(BaseModel):
38 """Deployment configuration for build outputs."""
40 method: str = Field(default="rsync", description="Deployment method: rsync, copy, symlink")
41 destination: str = Field(description="Deployment destination path or host:path")
42 options: dict[str, Any] = Field(
43 default_factory=dict, description="Method-specific options (e.g., rsync flags)"
44 )
47class PostBuildHook(BaseModel):
48 """A post-build action to run after all platforms complete."""
50 name: str = Field(description="Hook identifier")
51 command: str | None = Field(None, description="Shell command to run")
52 type: str = Field(
53 default="command", description="Hook type: command, jdupes, generation_filter"
54 )
55 options: dict[str, Any] = Field(default_factory=dict, description="Hook-specific options")
58class BuildSpec(BaseModel):
59 """Declarative build specification.
61 A BuildSpec is intentionally small. It declares:
62 1. WHERE: target (frontend + device)
63 2. HOW: recipes (processing templates)
64 3. WHAT: platform list (optional filters/overrides)
65 4. CONSTRAINTS: budget, generation filter
67 Everything else is derived by the resolver.
68 """
70 # Identity
71 name: str = Field(description="Build identifier (e.g., 'batocera-1tb')")
72 description: str = Field(default="", description="Human-readable description")
74 # WHERE — deployment target
75 target: str = Field(
76 description="Target name (e.g., 'batocera-pc', 'rocknix-r36s'). "
77 "Must match a file in config/targets/."
78 )
80 # HOW — processing recipes (applied in order)
81 recipes: list[str] = Field(
82 description="Recipe names to apply, in order. Later recipes override earlier "
83 "ones for overlapping platforms. Must match files in config/recipes/."
84 )
86 # WHAT — platform selection (optional)
87 platforms: list[str] | None = Field(
88 None, description="Explicit platform list. None = union of all recipe platform lists."
89 )
90 exclude: list[str] = Field(
91 default_factory=list, description="Platforms to exclude from the build"
92 )
94 # Global overrides (rarely needed)
95 selection: SelectionConfig | None = Field(
96 None, description="Global selection override for all platforms (e.g., limit to 10 games)"
97 )
99 # Cross-platform deduplication
100 generation_filter: GenerationFilterConfig | None = Field(
101 None, description="Cross-platform generation deduplication (1G1Gen)"
102 )
104 # Storage constraints
105 storage_budget: str | None = Field(
106 None, description="Total storage budget: '512gb', '1tb', 'unlimited', or None (unlimited)"
107 )
108 platform_budgets: dict[str, str] = Field(
109 default_factory=dict,
110 description="Per-platform storage budgets (e.g., {'psx': '80gb', '3ds': '50gb'})",
111 )
113 # Optimizer output — per-generation min_rating thresholds produced by
114 # ``romfarmer farmhand optimize`` / ``build-fill``. The resolver expands
115 # these into per-platform SelectionConfig overrides at build time.
116 optimizer_thresholds: dict[str, float] = Field(
117 default_factory=dict,
118 description=(
119 "Per-generation rating thresholds written by the budget optimizer. "
120 "Keys are generation names (gen5, gen6, gen7, …); values are "
121 "min_rating floats (0.0–1.0). Expanded to per-platform selection "
122 "overrides by the config resolver."
123 ),
124 )
126 # Output path override (rarely needed — usually derived from target + build name)
127 output_base: str | None = Field(
128 None, description="Override output base directory. Default: 'output/{build_name}/'"
129 )
131 # Post-build hooks
132 post_build: list[PostBuildHook] = Field(
133 default_factory=list,
134 description="Actions to run after all platforms complete (jdupes, deploy, etc.)",
135 )
137 # Deployment
138 deploy: DeployConfig | None = Field(
139 None, description="Deployment configuration (rsync to target device)"
140 )
142 @model_validator(mode="after")
143 def validate_build_spec(self) -> "BuildSpec":
144 """Validate build spec consistency."""
145 if not self.recipes:
146 raise ValueError("BuildSpec requires at least one recipe")
148 # Default storage_budget to unlimited
149 if self.storage_budget is None:
150 object.__setattr__(self, "storage_budget", "unlimited")
152 return self
154 def get_output_base(self) -> Path:
155 """Get the output base directory for this build.
157 Returns:
158 Path to output base (e.g., output/batocera-1tb/)
159 """
160 if self.output_base:
161 return Path(self.output_base)
162 return Path("output") / self.name
164 def has_budget_constraint(self) -> bool:
165 """Check if this build has a storage budget."""
166 return self.storage_budget is not None and self.storage_budget != "unlimited"