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 | |
| 40 | def get_filename(): |
| 41 | """ |
| 42 | Constructs and returns the fully qualified header filename. |
| 43 | """ |
| 44 | script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 45 | header_file = os.path.join(script_dir, "fan_detect_defs.cpp") |
| 46 | |
| 47 | return header_file |
| 48 | |
| 49 | |
Matthew Barth | 1080b38 | 2017-02-17 15:50:43 -0600 | [diff] [blame] | 50 | if __name__ == '__main__': |
| 51 | parser = ArgumentParser() |
| 52 | # Input yaml containing how each fan's presence detection should be done |
| 53 | parser.add_argument("-y", "--yaml", dest="pres_yaml", |
| 54 | default= |
| 55 | "example/fan-detect.yaml", |
| 56 | help= |
| 57 | "Input fan presences definition yaml file to parse") |
| 58 | args = parser.parse_args(sys.argv[1:]) |
| 59 | |
| 60 | # Verify given yaml file exists |
| 61 | yaml_file = os.path.abspath(args.pres_yaml) |
| 62 | if not os.path.isfile(yaml_file): |
| 63 | print "Unable to find input yaml file " + yaml_file |
| 64 | exit(1) |
| 65 | |
Matt Spinler | 14476ae | 2017-03-14 14:50:53 -0500 | [diff] [blame] | 66 | with open(yaml_file, 'r') as yaml_input: |
| 67 | presence_data = yaml.safe_load(yaml_input) or {} |
| 68 | |
| 69 | output_file = get_filename() |
| 70 | |
| 71 | with open(output_file, 'w') as out: |
| 72 | out.write(Template(tmpl).render(presence=presence_data)) |