Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | r""" |
| 4 | This script will parse the input error log yaml file and generate |
| 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. |
| 12 | |
| 13 | """ |
| 14 | |
| 15 | from mako.template import Template |
| 16 | from optparse import OptionParser |
| 17 | import yaml |
| 18 | import sys |
| 19 | import os |
| 20 | |
Andrew Geissler | 184690d | 2016-11-03 08:06:31 -0500 | [diff] [blame] | 21 | |
| 22 | def gen_elog_hpp(i_elog_yaml, i_input_mako, i_output_hpp): |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 23 | r""" |
| 24 | Read the input yaml file, grab the relevant data and call the mako |
| 25 | template to generate the header file. |
| 26 | |
| 27 | Description of arguments: |
| 28 | i_elog_yaml yaml file describing the error logs |
| 29 | i_output_hpp header file to output the generated code to |
| 30 | """ |
| 31 | |
| 32 | # Input parameters to mako template |
Andrew Geissler | 184690d | 2016-11-03 08:06:31 -0500 | [diff] [blame] | 33 | errors = dict() # Main error codes |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 34 | error_msg = dict() # Error msg that corresponds to error code |
| 35 | error_lvl = dict() # Error code log level (debug, info, error, ...) |
Andrew Geissler | 184690d | 2016-11-03 08:06:31 -0500 | [diff] [blame] | 36 | meta = list() # The meta data names associated (ERRNO, FILE_NAME, ...) |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 37 | meta_data = dict() # The meta data info (type, format) |
| 38 | |
| 39 | # see elog.yaml for reference |
| 40 | ifile = yaml.safe_load(open(i_elog_yaml)) |
| 41 | err_count = 0; |
| 42 | for i in ifile['SW'].keys(): |
| 43 | # Grab the main error |
| 44 | errors[err_count] = i |
| 45 | # Grab it's sub-items |
| 46 | prop = ifile['SW'][i] |
| 47 | error_msg[i] = prop['msg'] |
| 48 | error_lvl[i] = prop['level'] |
Andrew Geissler | 184690d | 2016-11-03 08:06:31 -0500 | [diff] [blame] | 49 | tmp_meta = [] |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 50 | # grab all the meta data fields and info |
| 51 | for j in prop['meta']: |
| 52 | str_short = j['str'].split('=')[0] |
| 53 | tmp_meta.append(str_short) |
| 54 | meta_data[str_short] = {} |
| 55 | meta_data[str_short]['str'] = j['str'] |
| 56 | meta_data[str_short]['str_short'] = str_short |
| 57 | meta_data[str_short]['type'] = j['type'] |
| 58 | meta.append(tmp_meta) |
Andrew Geissler | 184690d | 2016-11-03 08:06:31 -0500 | [diff] [blame] | 59 | err_count += 1 |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 60 | |
| 61 | # Debug |
Andrew Geissler | 184690d | 2016-11-03 08:06:31 -0500 | [diff] [blame] | 62 | # for i in errors: |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 63 | # print "ERROR: " + errors[i] |
| 64 | # print " MSG: " + error_msg[errors[i]] |
| 65 | # print " LVL: " + error_lvl[errors[i]] |
| 66 | # print " META: " |
| 67 | # print meta[i] |
| 68 | |
| 69 | # Load the mako template and call it with the required data |
Andrew Geissler | 184690d | 2016-11-03 08:06:31 -0500 | [diff] [blame] | 70 | mytemplate = Template(filename=i_input_mako) |
| 71 | f = open(i_output_hpp, 'w') |
| 72 | f.write(mytemplate.render(errors=errors, error_msg=error_msg, |
| 73 | error_lvl=error_lvl, meta=meta, |
| 74 | meta_data=meta_data)) |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 75 | f.close() |
| 76 | |
| 77 | |
| 78 | def main(i_args): |
| 79 | parser = OptionParser() |
| 80 | |
Andrew Geissler | 184690d | 2016-11-03 08:06:31 -0500 | [diff] [blame] | 81 | parser.add_option("-e", "--elog", dest="elog_yaml", default="elog.yaml", |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 82 | help="input error yaml file to parse"); |
| 83 | |
Andrew Geissler | 184690d | 2016-11-03 08:06:31 -0500 | [diff] [blame] | 84 | parser.add_option("-m", "--mako", dest="elog_mako", |
| 85 | default="elog-gen-template.mako.hpp", |
| 86 | help="input mako template file to use"); |
| 87 | |
| 88 | parser.add_option("-o", "--output", dest="output_hpp", |
| 89 | default="elog-gen.hpp", |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 90 | help="output hpp to generate, elog-gen.hpp is default"); |
| 91 | |
| 92 | (options, args) = parser.parse_args(i_args) |
| 93 | |
| 94 | if (not (os.path.isfile(options.elog_yaml))): |
| 95 | print "Can not find input yaml file " + options.elog_yaml |
| 96 | exit(1); |
| 97 | |
Andrew Geissler | 184690d | 2016-11-03 08:06:31 -0500 | [diff] [blame] | 98 | gen_elog_hpp(options.elog_yaml, options.elog_mako, |
| 99 | options.output_hpp) |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 100 | |
| 101 | # Only run if it's a script |
| 102 | if __name__ == '__main__': |
Andrew Geissler | 184690d | 2016-11-03 08:06:31 -0500 | [diff] [blame] | 103 | main(sys.argv[1:]) |