Matthew Barth | 1080b38 | 2017-02-17 15:50:43 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | """ |
| 4 | This script parses the given fan presence definition yaml file and generates |
| 5 | a header file based on the defined methods for determining when a fan is |
| 6 | present. |
| 7 | """ |
| 8 | |
| 9 | import os |
| 10 | import sys |
| 11 | import yaml |
| 12 | from argparse import ArgumentParser |
Matt Spinler | 14476ae | 2017-03-14 14:50:53 -0500 | [diff] [blame] | 13 | from mako.template import Template |
| 14 | |
| 15 | tmpl = '''/* This is a generated file. */ |
| 16 | #include "fan_detect_defs.hpp" |
| 17 | |
| 18 | const std::map<std::string, std::set<phosphor::fan::Properties>> |
| 19 | fanDetectMap = { |
| 20 | %for methods in presence: |
| 21 | %for method in methods: |
| 22 | <% m = method.lower() %> \ |
| 23 | {"${m}", { |
| 24 | %for fan in methods[method]: |
| 25 | std::make_tuple("${fan['Inventory']}", |
Matt Spinler | 2b44a6c | 2017-03-27 11:02:46 -0500 | [diff] [blame] | 26 | "${fan['PrettyName']}", |
Matt Spinler | 14476ae | 2017-03-14 14:50:53 -0500 | [diff] [blame] | 27 | std::vector<std::string>{ |
| 28 | %for s in fan['Sensors']: |
| 29 | "${s}", |
| 30 | %endfor |
| 31 | }), |
| 32 | %endfor |
| 33 | %endfor |
| 34 | }}, |
| 35 | %endfor |
| 36 | }; |
| 37 | ''' |
Matthew Barth | 1080b38 | 2017-02-17 15:50:43 -0600 | [diff] [blame] | 38 | |
| 39 | |
Matthew Barth | 1080b38 | 2017-02-17 15:50:43 -0600 | [diff] [blame] | 40 | if __name__ == '__main__': |
| 41 | parser = ArgumentParser() |
| 42 | # Input yaml containing how each fan's presence detection should be done |
| 43 | parser.add_argument("-y", "--yaml", dest="pres_yaml", |
| 44 | default= |
| 45 | "example/fan-detect.yaml", |
| 46 | help= |
| 47 | "Input fan presences definition yaml file to parse") |
| 48 | args = parser.parse_args(sys.argv[1:]) |
| 49 | |
| 50 | # Verify given yaml file exists |
| 51 | yaml_file = os.path.abspath(args.pres_yaml) |
| 52 | if not os.path.isfile(yaml_file): |
| 53 | print "Unable to find input yaml file " + yaml_file |
| 54 | exit(1) |
| 55 | |
Matt Spinler | 14476ae | 2017-03-14 14:50:53 -0500 | [diff] [blame] | 56 | with open(yaml_file, 'r') as yaml_input: |
| 57 | presence_data = yaml.safe_load(yaml_input) or {} |
| 58 | |
Brad Bishop | 9c2994e | 2017-06-05 22:06:36 -0400 | [diff] [blame] | 59 | sys.stdout.write(Template(tmpl).render(presence=presence_data)) |