Skip to content

Commit e126d80

Browse files
committed
build-scripts: enforce non-zero exit on OVAL file read errors
Thread an error count from process_oval_file through build_rule_variable_mapping to main(), and return 1 if any OVAL files could not be read. Validated against a full build of all 38 products — zero errors reported.
1 parent 40b19a0 commit e126d80

1 file changed

Lines changed: 17 additions & 13 deletions

File tree

build-scripts/extract_rule_variable_mapping.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def extract_variables_from_oval_content(content: str) -> Set[str]:
160160
return result
161161

162162

163-
def process_oval_file(oval_file: Path) -> Dict[str, Set[str]]:
163+
def process_oval_file(oval_file: Path) -> tuple[Dict[str, Set[str]], bool]:
164164
"""
165165
Process a single OVAL file and extract rule-variable mappings.
166166
@@ -172,7 +172,7 @@ def process_oval_file(oval_file: Path) -> Dict[str, Set[str]]:
172172
oval_file: Path to the OVAL XML file
173173
174174
Returns:
175-
Dictionary mapping the rule ID to the set of variable IDs it depends on
175+
Tuple of (mapping dict, had_error flag)
176176
"""
177177
# The filename stem is the rule ID for both OVAL file formats. Using the
178178
# filename avoids the need to parse definition IDs, which differ between
@@ -186,15 +186,16 @@ def process_oval_file(oval_file: Path) -> Dict[str, Set[str]]:
186186

187187
var_refs = extract_variables_from_oval_content(content)
188188
if var_refs:
189-
return {rule_id: var_refs}
189+
return {rule_id: var_refs}, False
190190

191191
except (IOError, UnicodeDecodeError) as e:
192192
print(f"Warning: Could not process {oval_file}: {e}", file=sys.stderr)
193+
return {}, True
193194

194-
return {}
195+
return {}, False
195196

196197

197-
def build_rule_variable_mapping(product: str, build_dir: Path) -> Dict[str, list]:
198+
def build_rule_variable_mapping(product: str, build_dir: Path) -> tuple[Dict[str, list], int]:
198199
"""
199200
Build complete rule-variable mapping for a product.
200201
@@ -203,19 +204,22 @@ def build_rule_variable_mapping(product: str, build_dir: Path) -> Dict[str, list
203204
build_dir: Path to the build directory
204205
205206
Returns:
206-
Dictionary mapping rule IDs to lists of variable IDs
207+
Tuple of (mapping dict, error count)
207208
"""
208209
product_dir = build_dir / product
209210
checks_dir = product_dir / "checks" / "oval"
210211
templates_dir = product_dir / "checks_from_templates" / "oval"
211212

212213
# Aggregate all rule-variable mappings
213214
all_mappings: Dict[str, Set[str]] = {}
215+
errors = 0
214216

215217
# Process regular OVAL checks
216218
if checks_dir.exists():
217219
for oval_file in checks_dir.glob("*.xml"):
218-
rule_vars = process_oval_file(oval_file)
220+
rule_vars, had_error = process_oval_file(oval_file)
221+
if had_error:
222+
errors += 1
219223
for rule_id, var_ids in rule_vars.items():
220224
if rule_id not in all_mappings:
221225
all_mappings[rule_id] = set()
@@ -224,7 +228,9 @@ def build_rule_variable_mapping(product: str, build_dir: Path) -> Dict[str, list
224228
# Process template-generated OVAL checks
225229
if templates_dir.exists():
226230
for oval_file in templates_dir.glob("*.xml"):
227-
rule_vars = process_oval_file(oval_file)
231+
rule_vars, had_error = process_oval_file(oval_file)
232+
if had_error:
233+
errors += 1
228234
for rule_id, var_ids in rule_vars.items():
229235
if rule_id not in all_mappings:
230236
all_mappings[rule_id] = set()
@@ -233,7 +239,7 @@ def build_rule_variable_mapping(product: str, build_dir: Path) -> Dict[str, list
233239
# Convert sets to sorted lists for JSON serialization
234240
result = {rule_id: sorted(list(var_ids)) for rule_id, var_ids in all_mappings.items()}
235241

236-
return result
242+
return result, errors
237243

238244

239245
def main():
@@ -251,7 +257,7 @@ def main():
251257
return 1
252258

253259
# Build the mapping
254-
mapping = build_rule_variable_mapping(product, build_dir)
260+
mapping, errors = build_rule_variable_mapping(product, build_dir)
255261

256262
# Ensure output directory exists
257263
output_file.parent.mkdir(parents=True, exist_ok=True)
@@ -260,9 +266,7 @@ def main():
260266
with open(output_file, 'w', encoding='utf-8') as f:
261267
json.dump(mapping, f, indent=2, sort_keys=True)
262268

263-
# Always return 0 so build failures in individual OVAL files (logged as
264-
# warnings) don't break the overall build. Enforcement can be added later.
265-
return 0
269+
return 1 if errors else 0
266270

267271

268272
if __name__ == '__main__':

0 commit comments

Comments
 (0)