1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
#!/usr/bin/env python
"""
This script parses the given fan presence definition yaml file and generates
a header file based on the defined methods for determining when a fan is
present.
"""
import os
import sys
import yaml
from argparse import ArgumentParser
from mako.template import Template
tmpl = '''/* This is a generated file. */
#include "fan_detect_defs.hpp"
const std::map<std::string, std::set<phosphor::fan::Properties>>
fanDetectMap = {
%for methods in presence:
%for method in methods:
<% m = method.lower() %> \
{"${m}", {
%for fan in methods[method]:
std::make_tuple("${fan['Inventory']}",
"${fan['PrettyName']}",
std::vector<std::string>{
%for s in fan['Sensors']:
"${s}",
%endfor
}),
%endfor
%endfor
}},
%endfor
};
'''
def get_filename():
"""
Constructs and returns the fully qualified header filename.
"""
script_dir = os.path.dirname(os.path.abspath(__file__))
header_file = os.path.join(script_dir, "fan_detect_defs.cpp")
return header_file
if __name__ == '__main__':
parser = ArgumentParser()
# Input yaml containing how each fan's presence detection should be done
parser.add_argument("-y", "--yaml", dest="pres_yaml",
default=
"example/fan-detect.yaml",
help=
"Input fan presences definition yaml file to parse")
args = parser.parse_args(sys.argv[1:])
# Verify given yaml file exists
yaml_file = os.path.abspath(args.pres_yaml)
if not os.path.isfile(yaml_file):
print "Unable to find input yaml file " + yaml_file
exit(1)
with open(yaml_file, 'r') as yaml_input:
presence_data = yaml.safe_load(yaml_input) or {}
output_file = get_filename()
with open(output_file, 'w') as out:
out.write(Template(tmpl).render(presence=presence_data))
|