Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | r""" |
Deepak Kodihalli | 160d3e0 | 2017-01-17 04:25:22 -0600 | [diff] [blame] | 4 | This script will parse error log yaml file(s) and generate |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 5 | a header file which will then be used by the error logging client and |
| 6 | server to collect and validate the error information generated by the |
| 7 | openbmc software components. |
| 8 | |
| 9 | This code uses a mako template to provide the basic template of the header |
| 10 | file we're going to generate. We then call it with information from the |
| 11 | yaml to generate the header file. |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 12 | """ |
| 13 | |
| 14 | from mako.template import Template |
| 15 | from optparse import OptionParser |
| 16 | import yaml |
| 17 | import sys |
| 18 | import os |
| 19 | |
Andrew Geissler | 184690d | 2016-11-03 08:06:31 -0500 | [diff] [blame] | 20 | |
Deepak Kodihalli | 5d1aace | 2017-01-18 00:17:39 -0600 | [diff] [blame] | 21 | def order_inherited_errors(i_errors, i_parents): |
| 22 | # the ordered list of errors |
| 23 | errors = list() |
| 24 | has_inheritance = False |
| 25 | for error in i_errors: |
| 26 | if(i_parents[error] is not None): |
| 27 | has_inheritance = True |
| 28 | break |
| 29 | |
| 30 | if(has_inheritance): |
| 31 | # Order the error codes list such that an error is never placed |
| 32 | # before it's parent. This way generated code can ensure parent |
| 33 | # definitions preceed child error definitions. |
| 34 | while(len(errors) < len(i_errors)): |
| 35 | for error in i_errors: |
| 36 | if(error in errors): |
| 37 | # already ordererd |
| 38 | continue |
| 39 | if((not i_parents[error]) or (i_parents[error] in errors)): |
| 40 | # parent present, or has no parent, either way this error |
| 41 | # can be added |
| 42 | errors.append(error) |
| 43 | else: |
| 44 | # no inherited errors |
| 45 | errors = i_errors |
| 46 | |
| 47 | return errors |
| 48 | |
| 49 | |
Deepak Kodihalli | cbd12c8 | 2017-01-17 08:50:55 -0600 | [diff] [blame] | 50 | def check_error_inheritance(i_errors, i_parents): |
Deepak Kodihalli | 5d1aace | 2017-01-18 00:17:39 -0600 | [diff] [blame] | 51 | for error in i_errors: |
| 52 | if(i_parents[error] and (i_parents[error] not in i_errors)): |
| 53 | print (error + " inherits " + i_parents[error] + |
| 54 | " but the latter is not defined") |
Deepak Kodihalli | cbd12c8 | 2017-01-17 08:50:55 -0600 | [diff] [blame] | 55 | return False |
| 56 | return True |
| 57 | |
| 58 | |
Deepak Kodihalli | 160d3e0 | 2017-01-17 04:25:22 -0600 | [diff] [blame] | 59 | def get_error_yaml_files(i_yaml_dir): |
| 60 | yaml_files = filter( |
| 61 | lambda file: file.endswith('.errors.yaml'), |
| 62 | os.listdir(i_yaml_dir)) |
| 63 | return yaml_files |
| 64 | |
| 65 | |
| 66 | def get_meta_yaml_file(i_error_yaml_file): |
| 67 | # the meta data will be defined in file name where we replace |
| 68 | # <Interface>.errors.yaml with <Interface>.metadata.yaml |
| 69 | meta_yaml = i_error_yaml_file.replace("errors", "metadata") |
| 70 | return meta_yaml |
| 71 | |
| 72 | |
Deepak Kodihalli | 9916119 | 2017-01-16 04:00:07 -0600 | [diff] [blame] | 73 | def get_cpp_type(i_type): |
| 74 | typeMap = { |
| 75 | 'int16': 'int16_t', |
| 76 | 'int32': 'int32_t', |
| 77 | 'int64': 'int64_t', |
| 78 | 'uint16': 'uint16_t', |
| 79 | 'uint32': 'uint32_t', |
| 80 | 'uint64': 'uint64_t', |
| 81 | 'double': 'double', |
| 82 | # const char* aids usage of constexpr |
| 83 | 'string': 'const char*'} |
| 84 | |
| 85 | return typeMap[i_type] |
| 86 | |
| 87 | |
Deepak Kodihalli | 160d3e0 | 2017-01-17 04:25:22 -0600 | [diff] [blame] | 88 | def gen_elog_hpp(i_yaml_dir, i_output_hpp, |
| 89 | i_template_dir, i_elog_mako, i_error_namespace): |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 90 | r""" |
Deepak Kodihalli | 160d3e0 | 2017-01-17 04:25:22 -0600 | [diff] [blame] | 91 | Read yaml file(s) under input yaml dir, grab the relevant data and call |
| 92 | the mako template to generate the output header file. |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 93 | |
| 94 | Description of arguments: |
Deepak Kodihalli | 160d3e0 | 2017-01-17 04:25:22 -0600 | [diff] [blame] | 95 | i_yaml_dir directory containing error yaml files |
| 96 | i_output_hpp name of the to be generated output hpp |
| 97 | i_template_dir directory containing error mako templates |
| 98 | i_elog_mako error mako template to render |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 99 | """ |
| 100 | |
| 101 | # Input parameters to mako template |
Deepak Kodihalli | 160d3e0 | 2017-01-17 04:25:22 -0600 | [diff] [blame] | 102 | errors = list() # Main error codes |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 103 | error_msg = dict() # Error msg that corresponds to error code |
| 104 | error_lvl = dict() # Error code log level (debug, info, error, ...) |
Deepak Kodihalli | 5d1aace | 2017-01-18 00:17:39 -0600 | [diff] [blame] | 105 | meta = dict() # The meta data names associated (ERRNO, FILE_NAME, ...) |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 106 | meta_data = dict() # The meta data info (type, format) |
Deepak Kodihalli | 5d1aace | 2017-01-18 00:17:39 -0600 | [diff] [blame] | 107 | parents = dict() |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 108 | |
Deepak Kodihalli | 160d3e0 | 2017-01-17 04:25:22 -0600 | [diff] [blame] | 109 | error_yamls = get_error_yaml_files(i_yaml_dir) |
| 110 | |
| 111 | for error_yaml in error_yamls: |
| 112 | # Verify the error yaml file |
| 113 | error_yaml = "/".join((i_yaml_dir, error_yaml)) |
| 114 | if (not (os.path.isfile(error_yaml))): |
| 115 | print "Can not find input yaml file " + error_yaml |
| 116 | exit(1) |
| 117 | |
| 118 | # Verify the metadata yaml file |
| 119 | meta_yaml = get_meta_yaml_file(error_yaml) |
| 120 | if (not (os.path.isfile(meta_yaml))): |
| 121 | print "Can not find meta yaml file " + meta_yaml |
| 122 | exit(1) |
| 123 | |
| 124 | # Verify the input mako file |
| 125 | template_path = "/".join((i_template_dir, i_elog_mako)) |
| 126 | if (not (os.path.isfile(template_path))): |
| 127 | print "Can not find input template file " + template_path |
| 128 | exit(1) |
| 129 | |
| 130 | get_elog_data(error_yaml, |
| 131 | meta_yaml, |
| 132 | # 3rd arg is a tuple |
| 133 | (errors, |
| 134 | error_msg, |
| 135 | error_lvl, |
| 136 | meta, |
Deepak Kodihalli | 82b7de7 | 2017-01-17 07:59:29 -0600 | [diff] [blame] | 137 | meta_data, |
| 138 | parents)) |
Deepak Kodihalli | 160d3e0 | 2017-01-17 04:25:22 -0600 | [diff] [blame] | 139 | |
Deepak Kodihalli | cbd12c8 | 2017-01-17 08:50:55 -0600 | [diff] [blame] | 140 | if(not check_error_inheritance(errors, parents)): |
| 141 | print "Error - failed to validate error inheritance" |
| 142 | exit(1) |
| 143 | |
Deepak Kodihalli | 5d1aace | 2017-01-18 00:17:39 -0600 | [diff] [blame] | 144 | errors = order_inherited_errors(errors, parents) |
| 145 | |
Deepak Kodihalli | 160d3e0 | 2017-01-17 04:25:22 -0600 | [diff] [blame] | 146 | # Load the mako template and call it with the required data |
| 147 | yaml_dir = i_yaml_dir.strip("./") |
| 148 | yaml_dir = yaml_dir.strip("../") |
| 149 | template = Template(filename=template_path) |
| 150 | f = open(i_output_hpp, 'w') |
| 151 | f.write(template.render( |
| 152 | errors=errors, error_msg=error_msg, |
| 153 | error_lvl=error_lvl, meta=meta, |
Deepak Kodihalli | f2462f0 | 2017-01-19 03:40:12 -0600 | [diff] [blame] | 154 | meta_data=meta_data, error_namespace=i_error_namespace, |
| 155 | parents=parents)) |
Deepak Kodihalli | 160d3e0 | 2017-01-17 04:25:22 -0600 | [diff] [blame] | 156 | f.close() |
| 157 | |
| 158 | |
| 159 | def get_elog_data(i_elog_yaml, |
| 160 | i_elog_meta_yaml, |
| 161 | o_elog_data): |
| 162 | r""" |
| 163 | Parse the error and metadata yaml files in order to pull out |
| 164 | error metadata. |
| 165 | |
| 166 | Description of arguments: |
| 167 | i_elog_yaml error yaml file |
| 168 | i_elog_meta_yaml metadata yaml file |
| 169 | o_elog_data error metadata |
| 170 | """ |
Deepak Kodihalli | 82b7de7 | 2017-01-17 07:59:29 -0600 | [diff] [blame] | 171 | errors, error_msg, error_lvl, meta, meta_data, parents = o_elog_data |
Deepak Kodihalli | 160d3e0 | 2017-01-17 04:25:22 -0600 | [diff] [blame] | 172 | ifile = yaml.safe_load(open(i_elog_yaml)) |
Andrew Geissler | e859630 | 2016-11-21 16:06:53 -0600 | [diff] [blame] | 173 | mfile = yaml.safe_load(open(i_elog_meta_yaml)) |
Andrew Geissler | 5e81bfb | 2016-11-21 12:49:01 -0600 | [diff] [blame] | 174 | for i in ifile: |
Andrew Geissler | e859630 | 2016-11-21 16:06:53 -0600 | [diff] [blame] | 175 | match = None |
| 176 | # Find the corresponding meta data for this entry |
| 177 | for m in mfile: |
| 178 | if m['name'] == i['name']: |
| 179 | match = m |
| 180 | break |
| 181 | if (match is None): |
| 182 | print "Error - Did not find meta data for " + i['name'] |
| 183 | exit(1) |
Andrew Geissler | df048c1 | 2016-11-10 16:50:35 -0600 | [diff] [blame] | 184 | # Grab the main error and it's info |
Deepak Kodihalli | 160d3e0 | 2017-01-17 04:25:22 -0600 | [diff] [blame] | 185 | errors.append(i['name']) |
Deepak Kodihalli | 82b7de7 | 2017-01-17 07:59:29 -0600 | [diff] [blame] | 186 | parent = None |
| 187 | if('inherits' in i): |
| 188 | # xyz.openbmc.Foo, we need Foo |
| 189 | # Get 0th inherited error (current support - single inheritance) |
| 190 | parent = i['inherits'][0].split(".").pop() |
Deepak Kodihalli | 5d1aace | 2017-01-18 00:17:39 -0600 | [diff] [blame] | 191 | parents[i['name']] = parent |
Andrew Geissler | 5e81bfb | 2016-11-21 12:49:01 -0600 | [diff] [blame] | 192 | error_msg[i['name']] = i['description'] |
Andrew Geissler | e859630 | 2016-11-21 16:06:53 -0600 | [diff] [blame] | 193 | error_lvl[i['name']] = match['level'] |
Andrew Geissler | 184690d | 2016-11-03 08:06:31 -0500 | [diff] [blame] | 194 | tmp_meta = [] |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 195 | # grab all the meta data fields and info |
Andrew Geissler | e859630 | 2016-11-21 16:06:53 -0600 | [diff] [blame] | 196 | for j in match['meta']: |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 197 | str_short = j['str'].split('=')[0] |
| 198 | tmp_meta.append(str_short) |
| 199 | meta_data[str_short] = {} |
| 200 | meta_data[str_short]['str'] = j['str'] |
| 201 | meta_data[str_short]['str_short'] = str_short |
Deepak Kodihalli | 9916119 | 2017-01-16 04:00:07 -0600 | [diff] [blame] | 202 | meta_data[str_short]['type'] = get_cpp_type(j['type']) |
Deepak Kodihalli | 5d1aace | 2017-01-18 00:17:39 -0600 | [diff] [blame] | 203 | meta[i['name']] = tmp_meta |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 204 | |
| 205 | # Debug |
Andrew Geissler | 184690d | 2016-11-03 08:06:31 -0500 | [diff] [blame] | 206 | # for i in errors: |
Andrew Geissler | df048c1 | 2016-11-10 16:50:35 -0600 | [diff] [blame] | 207 | # print "ERROR: " + errors[i] |
| 208 | # print " MSG: " + error_msg[errors[i]] |
| 209 | # print " LVL: " + error_lvl[errors[i]] |
| 210 | # print " META: " |
| 211 | # print meta[i] |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 212 | |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 213 | |
| 214 | def main(i_args): |
| 215 | parser = OptionParser() |
| 216 | |
Andrew Geissler | 184690d | 2016-11-03 08:06:31 -0500 | [diff] [blame] | 217 | parser.add_option("-m", "--mako", dest="elog_mako", |
| 218 | default="elog-gen-template.mako.hpp", |
Andrew Geissler | df048c1 | 2016-11-10 16:50:35 -0600 | [diff] [blame] | 219 | help="input mako template file to use") |
Andrew Geissler | 184690d | 2016-11-03 08:06:31 -0500 | [diff] [blame] | 220 | |
| 221 | parser.add_option("-o", "--output", dest="output_hpp", |
| 222 | default="elog-gen.hpp", |
Andrew Geissler | df048c1 | 2016-11-10 16:50:35 -0600 | [diff] [blame] | 223 | help="output hpp to generate, elog-gen.hpp is default") |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 224 | |
Deepak Kodihalli | 160d3e0 | 2017-01-17 04:25:22 -0600 | [diff] [blame] | 225 | parser.add_option("-y", "--yamldir", dest="yamldir", |
| 226 | default="./example/xyz/openbmc_project/Example", |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 227 | help="Base directory of yaml files to process") |
| 228 | |
| 229 | parser.add_option("-t", "--templatedir", dest="templatedir", |
| 230 | default="phosphor-logging/templates/", |
| 231 | help="Base directory of files to process") |
| 232 | |
Deepak Kodihalli | 160d3e0 | 2017-01-17 04:25:22 -0600 | [diff] [blame] | 233 | parser.add_option("-n", "--namespace", dest="error_namespace", |
| 234 | default="example/xyz/openbmc_project/Example", |
| 235 | help="Error d-bus namespace") |
| 236 | |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 237 | (options, args) = parser.parse_args(i_args) |
| 238 | |
Deepak Kodihalli | 160d3e0 | 2017-01-17 04:25:22 -0600 | [diff] [blame] | 239 | gen_elog_hpp(options.yamldir, |
| 240 | options.output_hpp, |
| 241 | options.templatedir, |
| 242 | options.elog_mako, |
| 243 | options.error_namespace) |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 244 | |
| 245 | # Only run if it's a script |
| 246 | if __name__ == '__main__': |
Andrew Geissler | 184690d | 2016-11-03 08:06:31 -0500 | [diff] [blame] | 247 | main(sys.argv[1:]) |