Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import sys |
| 4 | import os |
| 5 | import re |
| 6 | import argparse |
| 7 | import yaml |
Brad Bishop | cfb3c89 | 2016-11-12 11:43:37 -0500 | [diff] [blame] | 8 | import subprocess |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 9 | from mako.template import Template |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 10 | |
| 11 | valid_c_name_pattern = re.compile('[\W_]+') |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 12 | |
| 13 | |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 14 | def parse_event(e): |
| 15 | e['name'] = valid_c_name_pattern.sub('_', e['name']).lower() |
| 16 | if e.get('filter') is None: |
| 17 | e.setdefault('filter', {}).setdefault('type', 'none') |
| 18 | if e.get('action') is None: |
| 19 | e.setdefault('action', {}).setdefault('type', 'noop') |
| 20 | return e |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 21 | |
Brad Bishop | cfb3c89 | 2016-11-12 11:43:37 -0500 | [diff] [blame] | 22 | |
Brad Bishop | 14a9fe5 | 2016-11-12 12:51:26 -0500 | [diff] [blame] | 23 | def get_interfaces(args): |
| 24 | interfaces_dir = os.path.join(args.inputdir, 'interfaces.d') |
| 25 | yaml_files = filter( |
| 26 | lambda x: x.endswith('.yaml'), |
| 27 | os.listdir(interfaces_dir)) |
| 28 | |
| 29 | interfaces = [] |
| 30 | for x in yaml_files: |
| 31 | with open(os.path.join(interfaces_dir, x), 'r') as fd: |
| 32 | for i in yaml.load(fd.read()): |
| 33 | interfaces.append(i) |
| 34 | |
| 35 | return interfaces |
| 36 | |
| 37 | |
| 38 | def list_interfaces(args): |
| 39 | print ' '.join(get_interfaces(args)) |
| 40 | |
| 41 | |
Brad Bishop | 95dd98f | 2016-11-12 12:39:15 -0500 | [diff] [blame] | 42 | def generate_cpp(args): |
Brad Bishop | 3904cf4 | 2016-11-12 12:00:03 -0500 | [diff] [blame] | 43 | # Aggregate all the event YAML in the events.d directory |
Brad Bishop | cfb3c89 | 2016-11-12 11:43:37 -0500 | [diff] [blame] | 44 | # into a single list of events. |
Brad Bishop | 3904cf4 | 2016-11-12 12:00:03 -0500 | [diff] [blame] | 45 | events_dir = os.path.join(args.inputdir, 'events.d') |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 46 | yaml_files = filter( |
| 47 | lambda x: x.endswith('.yaml'), |
Brad Bishop | bbf13bd | 2016-11-09 22:29:09 -0500 | [diff] [blame] | 48 | os.listdir(events_dir)) |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 49 | |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 50 | events = [] |
| 51 | for x in yaml_files: |
Brad Bishop | bbf13bd | 2016-11-09 22:29:09 -0500 | [diff] [blame] | 52 | with open(os.path.join(events_dir, x), 'r') as fd: |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 53 | for e in yaml.load(fd.read()).get('events', {}): |
| 54 | events.append(parse_event(e)) |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 55 | |
Brad Bishop | 0a6a479 | 2016-11-12 12:10:07 -0500 | [diff] [blame] | 56 | # Aggregate all the interface YAML in the interfaces.d |
| 57 | # directory into a single list of interfaces. |
| 58 | template = os.path.join(script_dir, 'generated.mako.cpp') |
| 59 | t = Template(filename=template) |
| 60 | |
Brad Bishop | 14a9fe5 | 2016-11-12 12:51:26 -0500 | [diff] [blame] | 61 | interfaces = get_interfaces(args) |
Brad Bishop | 561a565 | 2016-10-26 21:13:32 -0500 | [diff] [blame] | 62 | |
Brad Bishop | cfb3c89 | 2016-11-12 11:43:37 -0500 | [diff] [blame] | 63 | # Render the template with the provided events and interfaces. |
| 64 | template = os.path.join(script_dir, 'generated.mako.cpp') |
| 65 | t = Template(filename=template) |
Brad Bishop | 4d16b69 | 2016-11-12 11:45:35 -0500 | [diff] [blame] | 66 | with open(os.path.join(args.outputdir, 'generated.cpp'), 'w') as fd: |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 67 | fd.write( |
| 68 | t.render( |
Brad Bishop | 561a565 | 2016-10-26 21:13:32 -0500 | [diff] [blame] | 69 | interfaces=interfaces, |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 70 | events=events)) |
| 71 | |
Brad Bishop | cfb3c89 | 2016-11-12 11:43:37 -0500 | [diff] [blame] | 72 | # Invoke sdbus++ to generate any extra interface bindings for |
| 73 | # extra interfaces that aren't defined externally. |
| 74 | yaml_files = [] |
Brad Bishop | ea6d93a | 2016-11-12 12:02:24 -0500 | [diff] [blame] | 75 | extra_ifaces_dir = os.path.join(args.inputdir, 'extra_interfaces.d') |
Brad Bishop | cfb3c89 | 2016-11-12 11:43:37 -0500 | [diff] [blame] | 76 | if os.path.exists(extra_ifaces_dir): |
| 77 | for directory, _, files in os.walk(extra_ifaces_dir): |
| 78 | if not files: |
| 79 | continue |
| 80 | |
| 81 | yaml_files += map( |
| 82 | lambda f: os.path.relpath( |
| 83 | os.path.join(directory, f), |
| 84 | extra_ifaces_dir), |
| 85 | filter(lambda f: f.endswith('.interface.yaml'), files)) |
| 86 | |
| 87 | genfiles = { |
| 88 | 'server-cpp': lambda x: '%s.cpp' % ( |
| 89 | x.replace(os.sep, '.')), |
| 90 | 'server-header': lambda x: os.path.join( |
| 91 | os.path.join( |
| 92 | *x.split('.')), 'server.hpp') |
| 93 | } |
| 94 | |
| 95 | for i in yaml_files: |
| 96 | iface = i.replace('.interface.yaml', '').replace(os.sep, '.') |
| 97 | for process, f in genfiles.iteritems(): |
| 98 | |
| 99 | dest = os.path.join(args.outputdir, f(iface)) |
| 100 | parent = os.path.dirname(dest) |
| 101 | if parent and not os.path.exists(parent): |
| 102 | os.makedirs(parent) |
| 103 | |
| 104 | with open(dest, 'w') as fd: |
| 105 | subprocess.call([ |
| 106 | 'sdbus++', |
| 107 | '-r', |
| 108 | extra_ifaces_dir, |
| 109 | 'interface', |
| 110 | process, |
| 111 | iface], |
| 112 | stdout=fd) |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 113 | |
Brad Bishop | 95dd98f | 2016-11-12 12:39:15 -0500 | [diff] [blame] | 114 | |
| 115 | if __name__ == '__main__': |
| 116 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
Brad Bishop | 14a9fe5 | 2016-11-12 12:51:26 -0500 | [diff] [blame] | 117 | valid_commands = { |
| 118 | 'generate-cpp': 'generate_cpp', |
| 119 | 'list-interfaces': 'list_interfaces'} |
Brad Bishop | 95dd98f | 2016-11-12 12:39:15 -0500 | [diff] [blame] | 120 | |
| 121 | parser = argparse.ArgumentParser( |
| 122 | description='Phosphor Inventory Manager (PIM) YAML ' |
| 123 | 'scanner and code generator.') |
| 124 | parser.add_argument( |
| 125 | '-o', '--output-dir', dest='outputdir', |
| 126 | default='.', help='Output directory.') |
| 127 | parser.add_argument( |
| 128 | '-d', '--dir', dest='inputdir', |
| 129 | default=os.path.join(script_dir, 'example'), |
| 130 | help='Location of files to process.') |
Brad Bishop | f4666f5 | 2016-11-12 12:44:42 -0500 | [diff] [blame] | 131 | parser.add_argument( |
| 132 | 'command', metavar='COMMAND', type=str, |
| 133 | choices=valid_commands.keys(), |
| 134 | help='Command to run.') |
Brad Bishop | 95dd98f | 2016-11-12 12:39:15 -0500 | [diff] [blame] | 135 | |
| 136 | args = parser.parse_args() |
Brad Bishop | f4666f5 | 2016-11-12 12:44:42 -0500 | [diff] [blame] | 137 | function = getattr(sys.modules[__name__], valid_commands[args.command]) |
| 138 | function(args) |
Brad Bishop | 95dd98f | 2016-11-12 12:39:15 -0500 | [diff] [blame] | 139 | |
| 140 | |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 141 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |