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