blob: 9301c761f72106559209b38987db4614d411d3b0 [file] [log] [blame]
Torsten Rasmussen8408af62021-11-22 10:29:56 +01001# Copyright (c) 2022 Nordic Semiconductor ASA
2#
3# SPDX-License-Identifier: Apache-2.0
4
5'''Domain handling for west extension commands.
6
7This provides parsing of domains yaml file and creation of objects of the
8Domain class.
9'''
10
11import yaml
12import pykwalify.core
13from west import log
14
15DOMAINS_SCHEMA = '''
16## A pykwalify schema for basic validation of the structure of a
17## domains YAML file.
18##
19# The domains.yaml file is a simple list of domains from a multi image build
20# along with the default domain to use.
21type: map
22mapping:
23 default:
24 required: true
25 type: str
26 build_dir:
27 required: true
28 type: str
29 domains:
30 required: false
31 type: seq
32 sequence:
33 - type: map
34 mapping:
35 name:
36 required: true
37 type: str
38 build_dir:
39 required: true
40 type: str
41'''
42
43schema = yaml.safe_load(DOMAINS_SCHEMA)
44
45
46class Domains:
47
48 def __init__(self, data):
49 self._domains = []
50 self._domain_names = []
51 self._domain_default = []
52
53 self._build_dir = data.get('build_dir')
54 domain_list = data.get('domains')
55 if not domain_list:
56 log.wrn("no domains defined; this probably won't work")
57
58 for d in domain_list:
59 domain = Domain(d['name'], d['build_dir'])
60 self._domains.append(domain)
61 self._domain_names.append(domain.name)
62 if domain.name == data['default']:
63 self._default_domain = domain
64
65 @staticmethod
66 def from_file(domains_file):
67 '''Load domains from domains.yaml.
68
69 Exception raised:
70 - ``FileNotFoundError`` if the domains file is not found.
71 '''
72 try:
73 with open(domains_file, 'r') as f:
74 domains = yaml.safe_load(f.read())
75 except FileNotFoundError:
76 log.die(f'domains.yaml file not found: {domains_file}')
77
78 try:
79 pykwalify.core.Core(source_data=domains, schema_data=schema)\
80 .validate()
81 except pykwalify.errors.SchemaError:
82 log.die(f'ERROR: Malformed yaml in file: {domains_file}')
83
84 return Domains(domains)
85
86 @staticmethod
87 def from_data(domains_data):
88 '''Load domains from domains dictionary.
89 '''
90 return Domains(domains_data)
91
92 def get_domains(self, names=None):
93 ret = []
94
95 if not names:
96 return self._domains
97
98 for n in names:
99 found = False
100 for d in self._domains:
101 if n == d.name:
102 ret.append(d)
103 found = True
104 break
105 # Getting here means the domain was not found.
106 # Todo: throw an error.
107 if not found:
108 log.die(f'domain {n} not found, '
109 f'valid domains are:', *self._domain_names)
110 return ret
111
112 def get_default_domain(self):
113 return self._default_domain
114
115 def get_top_build_dir(self):
116 return self._build_dir
117
118
119class Domain:
120
121 def __init__(self, name, build_dir):
122 self.name = name
123 self.build_dir = build_dir
124
125 @property
126 def name(self):
127 return self._name
128
129 @name.setter
130 def name(self, value):
131 self._name = value
132
133 @property
134 def build_dir(self):
135 return self._build_dir
136
137 @build_dir.setter
138 def build_dir(self, value):
139 self._build_dir = value