Files
phyllomeos/cook/bin/ksflatten-relative
T
Lukas Greve 3ffb079981 refactor: Move recipe generator to cook/ directory and fix flattening bugs
- Move recipe-generator/ to cook/ for cleaner structure

- Fix ksflatten-relative path conversion to handle all %include paths

- Fix validation exit code to only fail on actual errors

- All recipes now generate and flatten successfully via make all
2026-03-27 14:02:59 +01:00

91 lines
2.9 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Flatten kickstart recipes with relative %include paths.
Converts %include ingredients/... to %include ../ingredients/... relative to recipes/ location.
"""
import os
import sys
import subprocess
import tempfile
def flatten_recipe(recipe_path, output_path):
"""Flatten a recipe file, converting relative paths."""
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Read the recipe
with open(recipe_path, 'r') as f:
content = f.read()
# Convert %include paths to relative (from recipes/ perspective)
lines = []
for line in content.split('\n'):
if line.startswith('%include '):
# Get the fragment path from %include path
parts = line.split(None, 1)
if len(parts) >= 2:
fragment_path = parts[1]
# Make it relative to recipes/
# From recipes/, ingredients/ is at ../ingredients/
if fragment_path.startswith('ingredients/'):
relative_path = '../' + fragment_path
else:
relative_path = '../ingredients/' + fragment_path
lines.append('%include ' + relative_path)
else:
lines.append(line)
else:
lines.append(line)
# Write to temp file in recipes directory
temp_fd, temp_path = tempfile.mkstemp(suffix='.ks', dir=os.path.dirname(recipe_path))
try:
os.write(temp_fd, '\n'.join(lines).encode('utf-8'))
os.close(temp_fd)
# Call ksflatten
cmd = ['ksflatten', '-c', temp_path, '-o', output_path]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
print(f"Error flattening {os.path.basename(recipe_path)}:", file=sys.stderr)
print(result.stderr, file=sys.stderr)
return False
return True
finally:
os.unlink(temp_path)
def main():
if len(sys.argv) < 3:
print("Usage: ksflatten-relative RECIPE_DIR DISH_DIR", file=sys.stderr)
print("Example: ksflatten-relative recipes dishes", file=sys.stderr)
sys.exit(1)
recipe_dir = sys.argv[1]
dish_dir = sys.argv[2]
if not os.path.isdir(recipe_dir):
print(f"Error: {recipe_dir} is not a directory", file=sys.stderr)
sys.exit(1)
os.makedirs(dish_dir, exist_ok=True)
recipes = [f for f in os.listdir(recipe_dir) if f.endswith('.cfg')]
for recipe_name in sorted(recipes):
recipe_path = os.path.join(recipe_dir, recipe_name)
dish_path = os.path.join(dish_dir, recipe_name)
print(f" Processing: {recipe_name}")
if not flatten_recipe(recipe_path, dish_path):
sys.exit(1)
print("✓ All recipes flattened to dishes")
if __name__ == '__main__':
main()