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 | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 23 | if __name__ == '__main__': |
Brad Bishop | 79d8f7a | 2016-11-09 22:40:57 -0500 | [diff] [blame] | 24 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 25 | |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 26 | parser = argparse.ArgumentParser( |
| 27 | description='Phosphor Inventory Manager (PIM) YAML ' |
| 28 | 'scanner and code generator.') |
| 29 | parser.add_argument( |
Brad Bishop | 4d16b69 | 2016-11-12 11:45:35 -0500 | [diff] [blame] | 30 | '-o', '--output-dir', dest='outputdir', |
| 31 | default='.', help='Output directory.') |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 32 | parser.add_argument( |
| 33 | '-d', '--dir', dest='inputdir', |
Brad Bishop | f30db4c | 2016-11-09 22:47:05 -0500 | [diff] [blame] | 34 | default=os.path.join(script_dir, 'example'), |
Brad Bishop | 09fc256 | 2016-10-21 22:58:08 -0400 | [diff] [blame] | 35 | help='Location of files to process.') |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 36 | |
| 37 | args = parser.parse_args() |
| 38 | |
Brad Bishop | 3904cf4 | 2016-11-12 12:00:03 -0500 | [diff] [blame^] | 39 | # Aggregate all the event YAML in the events.d directory |
Brad Bishop | cfb3c89 | 2016-11-12 11:43:37 -0500 | [diff] [blame] | 40 | # into a single list of events. |
Brad Bishop | 3904cf4 | 2016-11-12 12:00:03 -0500 | [diff] [blame^] | 41 | events_dir = os.path.join(args.inputdir, 'events.d') |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 42 | yaml_files = filter( |
| 43 | lambda x: x.endswith('.yaml'), |
Brad Bishop | bbf13bd | 2016-11-09 22:29:09 -0500 | [diff] [blame] | 44 | os.listdir(events_dir)) |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 45 | |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 46 | events = [] |
| 47 | for x in yaml_files: |
Brad Bishop | bbf13bd | 2016-11-09 22:29:09 -0500 | [diff] [blame] | 48 | with open(os.path.join(events_dir, x), 'r') as fd: |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 49 | for e in yaml.load(fd.read()).get('events', {}): |
| 50 | events.append(parse_event(e)) |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 51 | |
Brad Bishop | cfb3c89 | 2016-11-12 11:43:37 -0500 | [diff] [blame] | 52 | # Import interfaces.yaml. |
Brad Bishop | bbf13bd | 2016-11-09 22:29:09 -0500 | [diff] [blame] | 53 | with open(os.path.join(args.inputdir, 'interfaces.yaml'), 'r') as fd: |
Brad Bishop | 561a565 | 2016-10-26 21:13:32 -0500 | [diff] [blame] | 54 | interfaces = yaml.load(fd.read()) |
| 55 | |
Brad Bishop | cfb3c89 | 2016-11-12 11:43:37 -0500 | [diff] [blame] | 56 | # Render the template with the provided events and interfaces. |
| 57 | template = os.path.join(script_dir, 'generated.mako.cpp') |
| 58 | t = Template(filename=template) |
Brad Bishop | 4d16b69 | 2016-11-12 11:45:35 -0500 | [diff] [blame] | 59 | with open(os.path.join(args.outputdir, 'generated.cpp'), 'w') as fd: |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 60 | fd.write( |
| 61 | t.render( |
Brad Bishop | 561a565 | 2016-10-26 21:13:32 -0500 | [diff] [blame] | 62 | interfaces=interfaces, |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 63 | events=events)) |
| 64 | |
Brad Bishop | cfb3c89 | 2016-11-12 11:43:37 -0500 | [diff] [blame] | 65 | # Invoke sdbus++ to generate any extra interface bindings for |
| 66 | # extra interfaces that aren't defined externally. |
| 67 | yaml_files = [] |
| 68 | extra_ifaces_dir = os.path.join(args.inputdir, 'interfaces') |
| 69 | if os.path.exists(extra_ifaces_dir): |
| 70 | for directory, _, files in os.walk(extra_ifaces_dir): |
| 71 | if not files: |
| 72 | continue |
| 73 | |
| 74 | yaml_files += map( |
| 75 | lambda f: os.path.relpath( |
| 76 | os.path.join(directory, f), |
| 77 | extra_ifaces_dir), |
| 78 | filter(lambda f: f.endswith('.interface.yaml'), files)) |
| 79 | |
| 80 | genfiles = { |
| 81 | 'server-cpp': lambda x: '%s.cpp' % ( |
| 82 | x.replace(os.sep, '.')), |
| 83 | 'server-header': lambda x: os.path.join( |
| 84 | os.path.join( |
| 85 | *x.split('.')), 'server.hpp') |
| 86 | } |
| 87 | |
| 88 | for i in yaml_files: |
| 89 | iface = i.replace('.interface.yaml', '').replace(os.sep, '.') |
| 90 | for process, f in genfiles.iteritems(): |
| 91 | |
| 92 | dest = os.path.join(args.outputdir, f(iface)) |
| 93 | parent = os.path.dirname(dest) |
| 94 | if parent and not os.path.exists(parent): |
| 95 | os.makedirs(parent) |
| 96 | |
| 97 | with open(dest, 'w') as fd: |
| 98 | subprocess.call([ |
| 99 | 'sdbus++', |
| 100 | '-r', |
| 101 | extra_ifaces_dir, |
| 102 | 'interface', |
| 103 | process, |
| 104 | iface], |
| 105 | stdout=fd) |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 106 | |
| 107 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |