Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 3 | '''Phosphor Inventory Manager YAML parser and code generator. |
| 4 | |
| 5 | The parser workflow is broken down as follows: |
| 6 | 1 - Import YAML files as native python type(s) instance(s). |
| 7 | 2 - Create an instance of the Everything class from the |
| 8 | native python type instance(s) with the Everything.load |
| 9 | method. |
| 10 | 3 - The Everything class constructor orchestrates conversion of the |
| 11 | native python type(s) instances(s) to render helper types. |
| 12 | Each render helper type constructor imports its attributes |
| 13 | from the native python type(s) instances(s). |
| 14 | 4 - Present the converted YAML to the command processing method |
| 15 | requested by the script user. |
| 16 | ''' |
| 17 | |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 18 | import sys |
| 19 | import os |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 20 | import argparse |
Brad Bishop | cfb3c89 | 2016-11-12 11:43:37 -0500 | [diff] [blame] | 21 | import subprocess |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 22 | import yaml |
| 23 | import mako.lookup |
| 24 | import sdbusplus.property |
| 25 | from sdbusplus.namedelement import NamedElement |
| 26 | from sdbusplus.renderer import Renderer |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 27 | |
| 28 | |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 29 | def cppTypeName(yaml_type): |
| 30 | ''' Convert yaml types to cpp types.''' |
| 31 | return sdbusplus.property.Property(type=yaml_type).cppTypeName |
| 32 | |
| 33 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 34 | class Interface(list): |
| 35 | '''Provide various interface transformations.''' |
| 36 | |
| 37 | def __init__(self, iface): |
| 38 | super(Interface, self).__init__(iface.split('.')) |
| 39 | |
| 40 | def namespace(self): |
| 41 | '''Represent as an sdbusplus namespace.''' |
| 42 | return '::'.join(['sdbusplus'] + self[:-1] + ['server', self[-1]]) |
| 43 | |
| 44 | def header(self): |
| 45 | '''Represent as an sdbusplus server binding header.''' |
| 46 | return os.sep.join(self + ['server.hpp']) |
| 47 | |
| 48 | def __str__(self): |
| 49 | return '.'.join(self) |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 50 | |
Brad Bishop | cfb3c89 | 2016-11-12 11:43:37 -0500 | [diff] [blame] | 51 | |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 52 | class Indent(object): |
| 53 | '''Help templates be depth agnostic.''' |
| 54 | |
| 55 | def __init__(self, depth=0): |
| 56 | self.depth = depth |
| 57 | |
| 58 | def __add__(self, depth): |
| 59 | return Indent(self.depth + depth) |
| 60 | |
| 61 | def __call__(self, depth): |
| 62 | '''Render an indent at the current depth plus depth.''' |
| 63 | return 4*' '*(depth + self.depth) |
| 64 | |
| 65 | |
| 66 | class Template(NamedElement): |
| 67 | '''Associate a template name with its namespace.''' |
| 68 | |
| 69 | def __init__(self, **kw): |
| 70 | self.namespace = kw.pop('namespace') |
| 71 | super(Template, self).__init__(**kw) |
| 72 | |
| 73 | def qualified(self): |
| 74 | return '::'.join(self.namespace + [self.name]) |
| 75 | |
| 76 | |
| 77 | class Quote(object): |
| 78 | '''Decorate an argument by quoting it.''' |
| 79 | |
| 80 | def __call__(self, arg): |
| 81 | return '"{0}"'.format(arg) |
| 82 | |
| 83 | |
| 84 | class Cast(object): |
| 85 | '''Decorate an argument by casting it.''' |
| 86 | |
| 87 | def __init__(self, cast, target): |
| 88 | '''cast is the cast type (static, const, etc...). |
| 89 | target is the cast target type.''' |
| 90 | self.cast = cast |
| 91 | self.target = target |
| 92 | |
| 93 | def __call__(self, arg): |
| 94 | return '{0}_cast<{1}>({2})'.format(self.cast, self.target, arg) |
| 95 | |
| 96 | |
| 97 | class Literal(object): |
| 98 | '''Decorate an argument with a literal operator.''' |
| 99 | |
| 100 | literals = { |
| 101 | 'string': 's', |
| 102 | 'int64': 'll', |
| 103 | 'uint64': 'ull' |
| 104 | } |
| 105 | |
| 106 | def __init__(self, type): |
| 107 | self.type = type |
| 108 | |
| 109 | def __call__(self, arg): |
| 110 | literal = self.literals.get(self.type) |
| 111 | |
| 112 | if literal: |
| 113 | return '{0}{1}'.format(arg, literal) |
| 114 | |
| 115 | return arg |
| 116 | |
| 117 | |
Brad Bishop | 75800cf | 2017-01-21 15:24:18 -0500 | [diff] [blame] | 118 | class Argument(NamedElement, Renderer): |
| 119 | '''Define argument type inteface.''' |
| 120 | |
| 121 | def __init__(self, **kw): |
| 122 | self.type = kw.pop('type', None) |
| 123 | super(Argument, self).__init__(**kw) |
| 124 | |
| 125 | def argument(self, loader, indent): |
| 126 | raise NotImplementedError |
| 127 | |
| 128 | |
| 129 | class TrivialArgument(Argument): |
| 130 | '''Non-array type arguments.''' |
Brad Bishop | 14a9fe5 | 2016-11-12 12:51:26 -0500 | [diff] [blame] | 131 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 132 | def __init__(self, **kw): |
| 133 | self.value = kw.pop('value') |
Brad Bishop | 75800cf | 2017-01-21 15:24:18 -0500 | [diff] [blame] | 134 | self.decorators = kw.pop('decorators', []) |
| 135 | if kw.get('type', None) == 'string': |
| 136 | self.decorators.insert(0, Quote()) |
| 137 | |
Brad Bishop | 75800cf | 2017-01-21 15:24:18 -0500 | [diff] [blame] | 138 | super(TrivialArgument, self).__init__(**kw) |
| 139 | |
| 140 | def argument(self, loader, indent): |
| 141 | a = str(self.value) |
| 142 | for d in self.decorators: |
| 143 | a = d(a) |
| 144 | |
| 145 | return a |
Brad Bishop | 14a9fe5 | 2016-11-12 12:51:26 -0500 | [diff] [blame] | 146 | |
Brad Bishop | 14a9fe5 | 2016-11-12 12:51:26 -0500 | [diff] [blame] | 147 | |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 148 | class InitializerList(Argument): |
| 149 | '''Initializer list arguments.''' |
| 150 | |
| 151 | def __init__(self, **kw): |
| 152 | self.values = kw.pop('values') |
| 153 | super(InitializerList, self).__init__(**kw) |
| 154 | |
| 155 | def argument(self, loader, indent): |
| 156 | return self.render( |
| 157 | loader, |
| 158 | 'argument.mako.cpp', |
| 159 | arg=self, |
| 160 | indent=indent) |
| 161 | |
| 162 | |
Brad Bishop | 75800cf | 2017-01-21 15:24:18 -0500 | [diff] [blame] | 163 | class DbusSignature(Argument): |
| 164 | '''DBus signature arguments.''' |
| 165 | |
| 166 | def __init__(self, **kw): |
| 167 | self.sig = {x: y for x, y in kw.iteritems()} |
| 168 | kw.clear() |
| 169 | super(DbusSignature, self).__init__(**kw) |
| 170 | |
| 171 | def argument(self, loader, indent): |
| 172 | return self.render( |
| 173 | loader, |
| 174 | 'signature.mako.cpp', |
| 175 | signature=self, |
| 176 | indent=indent) |
| 177 | |
| 178 | |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 179 | class MethodCall(Argument): |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 180 | '''Render syntatically correct c++ method calls.''' |
| 181 | |
| 182 | def __init__(self, **kw): |
| 183 | self.namespace = kw.pop('namespace', []) |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 184 | self.templates = kw.pop('templates', []) |
| 185 | self.args = kw.pop('args', []) |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 186 | super(MethodCall, self).__init__(**kw) |
| 187 | |
Brad Bishop | cab2bdd | 2017-01-21 15:00:54 -0500 | [diff] [blame] | 188 | def call(self, loader, indent): |
| 189 | return self.render( |
| 190 | loader, |
| 191 | 'method.mako.cpp', |
| 192 | method=self, |
| 193 | indent=indent) |
| 194 | |
| 195 | def argument(self, loader, indent): |
| 196 | return self.call(loader, indent) |
| 197 | |
Brad Bishop | 14a9fe5 | 2016-11-12 12:51:26 -0500 | [diff] [blame] | 198 | |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 199 | class Vector(MethodCall): |
| 200 | '''Convenience type for vectors.''' |
| 201 | |
| 202 | def __init__(self, **kw): |
| 203 | kw['name'] = 'vector' |
| 204 | kw['namespace'] = ['std'] |
| 205 | kw['args'] = [InitializerList(values=kw.pop('args'))] |
| 206 | super(Vector, self).__init__(**kw) |
| 207 | |
| 208 | |
| 209 | class Wrapper(MethodCall): |
| 210 | '''Convenience type for functions that wrap other functions.''' |
| 211 | |
| 212 | def __init__(self, **kw): |
| 213 | m = MethodCall( |
| 214 | name=kw.pop('name'), |
| 215 | namespace=kw.pop('namespace', []), |
| 216 | templates=kw.pop('templates', []), |
| 217 | args=kw.pop('args', [])) |
| 218 | |
| 219 | kw['name'] = kw.pop('wrapper_name') |
| 220 | kw['namespace'] = kw.pop('wrapper_namespace', []) |
| 221 | kw['args'] = [m] |
| 222 | super(Wrapper, self).__init__(**kw) |
| 223 | |
| 224 | |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 225 | class Filter(Wrapper): |
| 226 | '''Convenience type for filters''' |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 227 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 228 | def __init__(self, **kw): |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 229 | kw['wrapper_name'] = 'make_filter' |
| 230 | kw['wrapper_namespace'] = ['details'] |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 231 | kw['namespace'] = ['filters'] |
| 232 | super(Filter, self).__init__(**kw) |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 233 | |
Brad Bishop | 0a6a479 | 2016-11-12 12:10:07 -0500 | [diff] [blame] | 234 | |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 235 | class Action(Wrapper): |
| 236 | '''Convenience type for actions''' |
Brad Bishop | 561a565 | 2016-10-26 21:13:32 -0500 | [diff] [blame] | 237 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 238 | def __init__(self, **kw): |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 239 | kw['wrapper_name'] = 'make_action' |
| 240 | kw['wrapper_namespace'] = ['details'] |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 241 | kw['namespace'] = ['actions'] |
| 242 | super(Action, self).__init__(**kw) |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 243 | |
Brad Bishop | cfb3c89 | 2016-11-12 11:43:37 -0500 | [diff] [blame] | 244 | |
Brad Bishop | 7b7e712 | 2017-01-21 21:21:46 -0500 | [diff] [blame] | 245 | class DestroyObjects(Action): |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 246 | '''Assemble a destroyObject action.''' |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 247 | |
| 248 | def __init__(self, **kw): |
Brad Bishop | 7b7e712 | 2017-01-21 21:21:46 -0500 | [diff] [blame] | 249 | values = [{'value': x, 'type': 'string'} for x in kw.pop('paths')] |
| 250 | args = [InitializerList( |
| 251 | values=[TrivialArgument(**x) for x in values])] |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 252 | kw['args'] = args |
Brad Bishop | 7b7e712 | 2017-01-21 21:21:46 -0500 | [diff] [blame] | 253 | super(DestroyObjects, self).__init__(**kw) |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 254 | |
| 255 | |
Brad Bishop | e2e402f | 2016-11-30 18:00:17 -0500 | [diff] [blame] | 256 | class SetProperty(Action): |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 257 | '''Assemble a setProperty action.''' |
Brad Bishop | e2e402f | 2016-11-30 18:00:17 -0500 | [diff] [blame] | 258 | |
| 259 | def __init__(self, **kw): |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 260 | args = [] |
| 261 | |
| 262 | value = kw.pop('value') |
| 263 | prop = kw.pop('property') |
| 264 | iface = kw.pop('interface') |
| 265 | iface = Interface(iface) |
| 266 | namespace = iface.namespace().split('::')[:-1] |
| 267 | name = iface[-1] |
| 268 | t = Template(namespace=namespace, name=iface[-1]) |
| 269 | |
Brad Bishop | e2e402f | 2016-11-30 18:00:17 -0500 | [diff] [blame] | 270 | member = '&%s' % '::'.join( |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 271 | namespace + [name, NamedElement(name=prop).camelCase]) |
| 272 | member_type = cppTypeName(value['type']) |
| 273 | member_cast = '{0} ({1}::*)({0})'.format(member_type, t.qualified()) |
Brad Bishop | e2e402f | 2016-11-30 18:00:17 -0500 | [diff] [blame] | 274 | |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 275 | args.append(TrivialArgument(value=kw.pop('path'), type='string')) |
| 276 | args.append(TrivialArgument(value=str(iface), type='string')) |
| 277 | args.append(TrivialArgument( |
| 278 | value=member, decorators=[Cast('static', member_cast)])) |
| 279 | args.append(TrivialArgument(**value)) |
Brad Bishop | e2e402f | 2016-11-30 18:00:17 -0500 | [diff] [blame] | 280 | |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 281 | kw['templates'] = [Template(name=name, namespace=namespace)] |
| 282 | kw['args'] = args |
Brad Bishop | e2e402f | 2016-11-30 18:00:17 -0500 | [diff] [blame] | 283 | super(SetProperty, self).__init__(**kw) |
| 284 | |
| 285 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 286 | class PropertyChanged(Filter): |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 287 | '''Assemble a propertyChanged filter.''' |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 288 | |
| 289 | def __init__(self, **kw): |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 290 | args = [] |
| 291 | args.append(TrivialArgument(value=kw.pop('interface'), type='string')) |
| 292 | args.append(TrivialArgument(value=kw.pop('property'), type='string')) |
| 293 | args.append(TrivialArgument( |
| 294 | decorators=[ |
| 295 | Literal(kw['value'].get('type', None))], **kw.pop('value'))) |
| 296 | kw['args'] = args |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 297 | super(PropertyChanged, self).__init__(**kw) |
| 298 | |
| 299 | |
Brad Bishop | 040e18b | 2017-01-21 22:04:00 -0500 | [diff] [blame^] | 300 | class PropertyIs(Filter): |
| 301 | '''Assemble a propertyIs filter.''' |
| 302 | |
| 303 | def __init__(self, **kw): |
| 304 | args = [] |
| 305 | args.append(TrivialArgument(value=kw.pop('path'), type='string')) |
| 306 | args.append(TrivialArgument(value=kw.pop('interface'), type='string')) |
| 307 | args.append(TrivialArgument(value=kw.pop('property'), type='string')) |
| 308 | args.append(TrivialArgument( |
| 309 | decorators=[ |
| 310 | Literal(kw['value'].get('type', None))], **kw.pop('value'))) |
| 311 | |
| 312 | service = kw.pop('service', None) |
| 313 | if service: |
| 314 | args.append(TrivialArgument(value=service, type='string')) |
| 315 | |
| 316 | kw['args'] = args |
| 317 | super(PropertyIs, self).__init__(**kw) |
| 318 | |
| 319 | |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 320 | class Event(MethodCall): |
| 321 | '''Assemble an inventory manager event.''' |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 322 | |
| 323 | action_map = { |
Brad Bishop | 7b7e712 | 2017-01-21 21:21:46 -0500 | [diff] [blame] | 324 | 'destroyObjects': DestroyObjects, |
Brad Bishop | e2e402f | 2016-11-30 18:00:17 -0500 | [diff] [blame] | 325 | 'setProperty': SetProperty, |
Brad Bishop | cfb3c89 | 2016-11-12 11:43:37 -0500 | [diff] [blame] | 326 | } |
| 327 | |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 328 | filter_map = { |
| 329 | 'propertyChangedTo': PropertyChanged, |
Brad Bishop | 040e18b | 2017-01-21 22:04:00 -0500 | [diff] [blame^] | 330 | 'propertyIs': PropertyIs, |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 331 | } |
| 332 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 333 | def __init__(self, **kw): |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 334 | self.summary = kw.pop('name') |
| 335 | |
| 336 | filters = [ |
| 337 | self.filter_map[x['name']](**x) for x in kw.pop('filters', [])] |
Brad Bishop | 064c94a | 2017-01-21 21:33:30 -0500 | [diff] [blame] | 338 | filters = Vector( |
| 339 | templates=[Template(name='FilterBasePtr', namespace=['details'])], |
| 340 | args=filters) |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 341 | |
| 342 | event = MethodCall( |
| 343 | name='make_shared', |
| 344 | namespace=['std'], |
| 345 | templates=[Template( |
| 346 | name=kw.pop('event'), |
| 347 | namespace=kw.pop('event_namespace', []))], |
Brad Bishop | 064c94a | 2017-01-21 21:33:30 -0500 | [diff] [blame] | 348 | args=kw.pop('event_args', []) + [filters]) |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 349 | |
| 350 | events = Vector( |
| 351 | templates=[Template(name='EventBasePtr', namespace=['details'])], |
| 352 | args=[event]) |
| 353 | |
| 354 | action_type = Template(name='ActionBasePtr', namespace=['details']) |
| 355 | action_args = [ |
| 356 | self.action_map[x['name']](**x) for x in kw.pop('actions', [])] |
| 357 | actions = Vector( |
| 358 | templates=[action_type], |
| 359 | args=action_args) |
| 360 | |
| 361 | kw['name'] = 'make_tuple' |
| 362 | kw['namespace'] = ['std'] |
| 363 | kw['args'] = [events, actions] |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 364 | super(Event, self).__init__(**kw) |
Brad Bishop | cfb3c89 | 2016-11-12 11:43:37 -0500 | [diff] [blame] | 365 | |
Brad Bishop | cfb3c89 | 2016-11-12 11:43:37 -0500 | [diff] [blame] | 366 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 367 | class MatchEvent(Event): |
| 368 | '''Associate one or more dbus signal match signatures with |
| 369 | a filter.''' |
| 370 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 371 | def __init__(self, **kw): |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 372 | kw['event'] = 'DbusSignal' |
| 373 | kw['event_namespace'] = ['details'] |
| 374 | kw['event_args'] = [ |
| 375 | DbusSignature(**x) for x in kw.pop('signatures', [])] |
| 376 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 377 | super(MatchEvent, self).__init__(**kw) |
| 378 | |
| 379 | |
| 380 | class Everything(Renderer): |
| 381 | '''Parse/render entry point.''' |
| 382 | |
| 383 | class_map = { |
| 384 | 'match': MatchEvent, |
| 385 | } |
| 386 | |
| 387 | @staticmethod |
| 388 | def load(args): |
| 389 | # Invoke sdbus++ to generate any extra interface bindings for |
| 390 | # extra interfaces that aren't defined externally. |
| 391 | yaml_files = [] |
| 392 | extra_ifaces_dir = os.path.join(args.inputdir, 'extra_interfaces.d') |
| 393 | if os.path.exists(extra_ifaces_dir): |
| 394 | for directory, _, files in os.walk(extra_ifaces_dir): |
| 395 | if not files: |
| 396 | continue |
| 397 | |
| 398 | yaml_files += map( |
| 399 | lambda f: os.path.relpath( |
| 400 | os.path.join(directory, f), |
| 401 | extra_ifaces_dir), |
| 402 | filter(lambda f: f.endswith('.interface.yaml'), files)) |
| 403 | |
| 404 | genfiles = { |
| 405 | 'server-cpp': lambda x: '%s.cpp' % ( |
| 406 | x.replace(os.sep, '.')), |
| 407 | 'server-header': lambda x: os.path.join( |
| 408 | os.path.join( |
| 409 | *x.split('.')), 'server.hpp') |
| 410 | } |
| 411 | |
| 412 | for i in yaml_files: |
| 413 | iface = i.replace('.interface.yaml', '').replace(os.sep, '.') |
| 414 | for process, f in genfiles.iteritems(): |
| 415 | |
| 416 | dest = os.path.join(args.outputdir, f(iface)) |
| 417 | parent = os.path.dirname(dest) |
| 418 | if parent and not os.path.exists(parent): |
| 419 | os.makedirs(parent) |
| 420 | |
| 421 | with open(dest, 'w') as fd: |
| 422 | subprocess.call([ |
| 423 | 'sdbus++', |
| 424 | '-r', |
| 425 | extra_ifaces_dir, |
| 426 | 'interface', |
| 427 | process, |
| 428 | iface], |
| 429 | stdout=fd) |
| 430 | |
| 431 | # Aggregate all the event YAML in the events.d directory |
| 432 | # into a single list of events. |
| 433 | |
| 434 | events_dir = os.path.join(args.inputdir, 'events.d') |
| 435 | yaml_files = filter( |
| 436 | lambda x: x.endswith('.yaml'), |
| 437 | os.listdir(events_dir)) |
| 438 | |
| 439 | events = [] |
| 440 | for x in yaml_files: |
| 441 | with open(os.path.join(events_dir, x), 'r') as fd: |
Brad Bishop | effbd93 | 2017-01-22 11:07:54 -0500 | [diff] [blame] | 442 | for e in yaml.safe_load(fd.read()).get('events', {}): |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 443 | events.append(e) |
| 444 | |
| 445 | return Everything( |
| 446 | *events, |
| 447 | interfaces=Everything.get_interfaces(args)) |
| 448 | |
| 449 | @staticmethod |
| 450 | def get_interfaces(args): |
| 451 | '''Aggregate all the interface YAML in the interfaces.d |
| 452 | directory into a single list of interfaces.''' |
| 453 | |
| 454 | interfaces_dir = os.path.join(args.inputdir, 'interfaces.d') |
| 455 | yaml_files = filter( |
| 456 | lambda x: x.endswith('.yaml'), |
| 457 | os.listdir(interfaces_dir)) |
| 458 | |
| 459 | interfaces = [] |
| 460 | for x in yaml_files: |
| 461 | with open(os.path.join(interfaces_dir, x), 'r') as fd: |
Brad Bishop | effbd93 | 2017-01-22 11:07:54 -0500 | [diff] [blame] | 462 | for i in yaml.safe_load(fd.read()): |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 463 | interfaces.append(i) |
| 464 | |
| 465 | return interfaces |
| 466 | |
| 467 | def __init__(self, *a, **kw): |
| 468 | self.interfaces = \ |
| 469 | [Interface(x) for x in kw.pop('interfaces', [])] |
| 470 | self.events = [ |
| 471 | self.class_map[x['type']](**x) for x in a] |
| 472 | super(Everything, self).__init__(**kw) |
| 473 | |
| 474 | def list_interfaces(self, *a): |
| 475 | print ' '.join([str(i) for i in self.interfaces]) |
| 476 | |
| 477 | def generate_cpp(self, loader): |
| 478 | '''Render the template with the provided events and interfaces.''' |
| 479 | with open(os.path.join( |
| 480 | args.outputdir, |
| 481 | 'generated.cpp'), 'w') as fd: |
| 482 | fd.write( |
| 483 | self.render( |
| 484 | loader, |
| 485 | 'generated.mako.cpp', |
| 486 | events=self.events, |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 487 | interfaces=self.interfaces, |
| 488 | indent=Indent())) |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 489 | |
Brad Bishop | 95dd98f | 2016-11-12 12:39:15 -0500 | [diff] [blame] | 490 | |
| 491 | if __name__ == '__main__': |
| 492 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
Brad Bishop | 14a9fe5 | 2016-11-12 12:51:26 -0500 | [diff] [blame] | 493 | valid_commands = { |
| 494 | 'generate-cpp': 'generate_cpp', |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 495 | 'list-interfaces': 'list_interfaces' |
| 496 | } |
Brad Bishop | 95dd98f | 2016-11-12 12:39:15 -0500 | [diff] [blame] | 497 | |
| 498 | parser = argparse.ArgumentParser( |
| 499 | description='Phosphor Inventory Manager (PIM) YAML ' |
| 500 | 'scanner and code generator.') |
| 501 | parser.add_argument( |
| 502 | '-o', '--output-dir', dest='outputdir', |
| 503 | default='.', help='Output directory.') |
| 504 | parser.add_argument( |
| 505 | '-d', '--dir', dest='inputdir', |
| 506 | default=os.path.join(script_dir, 'example'), |
| 507 | help='Location of files to process.') |
Brad Bishop | f4666f5 | 2016-11-12 12:44:42 -0500 | [diff] [blame] | 508 | parser.add_argument( |
| 509 | 'command', metavar='COMMAND', type=str, |
| 510 | choices=valid_commands.keys(), |
Brad Bishop | c029f6a | 2017-01-18 19:43:26 -0500 | [diff] [blame] | 511 | help='%s.' % " | ".join(valid_commands.keys())) |
Brad Bishop | 95dd98f | 2016-11-12 12:39:15 -0500 | [diff] [blame] | 512 | |
| 513 | args = parser.parse_args() |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 514 | |
| 515 | if sys.version_info < (3, 0): |
| 516 | lookup = mako.lookup.TemplateLookup( |
| 517 | directories=[script_dir], |
| 518 | disable_unicode=True) |
| 519 | else: |
| 520 | lookup = mako.lookup.TemplateLookup( |
| 521 | directories=[script_dir]) |
| 522 | |
| 523 | function = getattr( |
| 524 | Everything.load(args), |
| 525 | valid_commands[args.command]) |
| 526 | function(lookup) |
Brad Bishop | 95dd98f | 2016-11-12 12:39:15 -0500 | [diff] [blame] | 527 | |
| 528 | |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 529 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |