Brad Bishop | bec4ebc | 2022-08-03 09:55:16 -0400 | [diff] [blame^] | 1 | import json |
| 2 | import pathlib |
| 3 | import os |
| 4 | |
| 5 | |
| 6 | def get_image_directory(machine=None): |
| 7 | """ |
| 8 | Get the DEPLOY_DIR_IMAGE for the specified machine |
| 9 | (or the configured machine if not set). |
| 10 | """ |
| 11 | try: |
| 12 | import bb.tinfoil |
| 13 | except ImportError as e: |
| 14 | raise RuntimeError("Cannot connect to BitBake, did you oe-init-build-env?") from e |
| 15 | |
| 16 | if machine: |
| 17 | os.environ["MACHINE"] = machine |
| 18 | |
| 19 | with bb.tinfoil.Tinfoil() as tinfoil: |
| 20 | tinfoil.prepare(config_only=True) |
| 21 | image_dir = tinfoil.config_data.getVar("DEPLOY_DIR_IMAGE") |
| 22 | return pathlib.Path(image_dir) |
| 23 | |
| 24 | def find(machine): |
| 25 | image_dir = get_image_directory(machine) |
| 26 | # All .fvpconf configuration files |
| 27 | configs = image_dir.glob("*.fvpconf") |
| 28 | # Just the files |
| 29 | configs = [p for p in configs if p.is_file() and not p.is_symlink()] |
| 30 | if not configs: |
| 31 | print(f"Cannot find any .fvpconf in {image_dir}") |
| 32 | raise RuntimeError() |
| 33 | # Sorted by modification time |
| 34 | configs = sorted(configs, key=lambda p: p.stat().st_mtime) |
| 35 | return configs[-1] |
| 36 | |
| 37 | |
| 38 | def load(config_file): |
| 39 | with open(config_file) as f: |
| 40 | config = json.load(f) |
| 41 | |
| 42 | # Ensure that all expected keys are present |
| 43 | def sanitise(key, value): |
| 44 | if key not in config or config[key] is None: |
| 45 | config[key] = value |
| 46 | sanitise("fvp-bindir", "") |
| 47 | sanitise("exe", "") |
| 48 | sanitise("parameters", {}) |
| 49 | sanitise("data", {}) |
| 50 | sanitise("applications", {}) |
| 51 | sanitise("terminals", {}) |
| 52 | sanitise("args", []) |
| 53 | sanitise("consoles", {}) |
| 54 | |
| 55 | if not config["exe"]: |
| 56 | raise ValueError("Required value FVP_EXE not set in machine configuration") |
| 57 | |
| 58 | return config |