blob: 603851d1dcd98ddcd30d81b5e8598782b38704c8 [file] [log] [blame]
Brad Bishopbec4ebc2022-08-03 09:55:16 -04001import json
2import pathlib
3import os
4
5
6def 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
24def 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
38def 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", {})
Patrick Williams8dd68482022-10-04 07:57:18 -050054 sanitise("env", {})
Brad Bishopbec4ebc2022-08-03 09:55:16 -040055
56 if not config["exe"]:
57 raise ValueError("Required value FVP_EXE not set in machine configuration")
58
59 return config