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 | |
Andrew Geissler | e859630 | 2016-11-21 16:06:53 -0600 | [diff] [blame] | 22 | def gen_elog_hpp(i_rootdir, i_elog_yaml, i_elog_meta_yaml, |
| 23 | i_input_mako, i_output_hpp): |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 24 | r""" |
| 25 | Read the input yaml file, grab the relevant data and call the mako |
| 26 | template to generate the header file. |
| 27 | |
| 28 | Description of arguments: |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 29 | i_rootdir base directory to search for yaml files |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 30 | i_elog_yaml yaml file describing the error logs |
Andrew Geissler | e859630 | 2016-11-21 16:06:53 -0600 | [diff] [blame] | 31 | i_elog_meta_yaml yaml file describing the meta data for elogs |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 32 | i_input_mako input mako template file to use |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 33 | i_output_hpp header file to output the generated code to |
| 34 | """ |
| 35 | |
| 36 | # Input parameters to mako template |
Andrew Geissler | 184690d | 2016-11-03 08:06:31 -0500 | [diff] [blame] | 37 | errors = dict() # Main error codes |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 38 | error_msg = dict() # Error msg that corresponds to error code |
| 39 | error_lvl = dict() # Error code log level (debug, info, error, ...) |
Andrew Geissler | 184690d | 2016-11-03 08:06:31 -0500 | [diff] [blame] | 40 | meta = list() # The meta data names associated (ERRNO, FILE_NAME, ...) |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 41 | meta_data = dict() # The meta data info (type, format) |
| 42 | |
| 43 | # see elog.yaml for reference |
Andrew Geissler | e859630 | 2016-11-21 16:06:53 -0600 | [diff] [blame] | 44 | ifile = yaml.safe_load(open("/".join((i_rootdir, i_elog_yaml)))) |
| 45 | mfile = yaml.safe_load(open(i_elog_meta_yaml)) |
Andrew Geissler | df048c1 | 2016-11-10 16:50:35 -0600 | [diff] [blame] | 46 | err_count = 0 |
Andrew Geissler | 5e81bfb | 2016-11-21 12:49:01 -0600 | [diff] [blame] | 47 | for i in ifile: |
Andrew Geissler | e859630 | 2016-11-21 16:06:53 -0600 | [diff] [blame] | 48 | match = None |
| 49 | # Find the corresponding meta data for this entry |
| 50 | for m in mfile: |
| 51 | if m['name'] == i['name']: |
| 52 | match = m |
| 53 | break |
| 54 | if (match is None): |
| 55 | print "Error - Did not find meta data for " + i['name'] |
| 56 | exit(1) |
Andrew Geissler | df048c1 | 2016-11-10 16:50:35 -0600 | [diff] [blame] | 57 | # Grab the main error and it's info |
| 58 | errors[err_count] = i['name'] |
Andrew Geissler | 5e81bfb | 2016-11-21 12:49:01 -0600 | [diff] [blame] | 59 | error_msg[i['name']] = i['description'] |
Andrew Geissler | e859630 | 2016-11-21 16:06:53 -0600 | [diff] [blame] | 60 | error_lvl[i['name']] = match['level'] |
Andrew Geissler | 184690d | 2016-11-03 08:06:31 -0500 | [diff] [blame] | 61 | tmp_meta = [] |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 62 | # grab all the meta data fields and info |
Andrew Geissler | e859630 | 2016-11-21 16:06:53 -0600 | [diff] [blame] | 63 | for j in match['meta']: |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 64 | str_short = j['str'].split('=')[0] |
| 65 | tmp_meta.append(str_short) |
| 66 | meta_data[str_short] = {} |
| 67 | meta_data[str_short]['str'] = j['str'] |
| 68 | meta_data[str_short]['str_short'] = str_short |
| 69 | meta_data[str_short]['type'] = j['type'] |
| 70 | meta.append(tmp_meta) |
Andrew Geissler | 184690d | 2016-11-03 08:06:31 -0500 | [diff] [blame] | 71 | err_count += 1 |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 72 | |
| 73 | # Debug |
Andrew Geissler | 184690d | 2016-11-03 08:06:31 -0500 | [diff] [blame] | 74 | # for i in errors: |
Andrew Geissler | df048c1 | 2016-11-10 16:50:35 -0600 | [diff] [blame] | 75 | # print "ERROR: " + errors[i] |
| 76 | # print " MSG: " + error_msg[errors[i]] |
| 77 | # print " LVL: " + error_lvl[errors[i]] |
| 78 | # print " META: " |
| 79 | # print meta[i] |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 80 | |
| 81 | # Load the mako template and call it with the required data |
Andrew Geissler | 184690d | 2016-11-03 08:06:31 -0500 | [diff] [blame] | 82 | mytemplate = Template(filename=i_input_mako) |
| 83 | f = open(i_output_hpp, 'w') |
| 84 | f.write(mytemplate.render(errors=errors, error_msg=error_msg, |
| 85 | error_lvl=error_lvl, meta=meta, |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 86 | meta_data=meta_data,elog_yaml=i_elog_yaml)) |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 87 | f.close() |
| 88 | |
| 89 | |
| 90 | def main(i_args): |
| 91 | parser = OptionParser() |
| 92 | |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 93 | parser.add_option("-e", "--elog", dest="elog_yaml", |
| 94 | default="xyz/openbmc_project/Example/Elog.errors.yaml", |
Andrew Geissler | df048c1 | 2016-11-10 16:50:35 -0600 | [diff] [blame] | 95 | help="input error yaml file to parse") |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 96 | |
Andrew Geissler | 184690d | 2016-11-03 08:06:31 -0500 | [diff] [blame] | 97 | parser.add_option("-m", "--mako", dest="elog_mako", |
| 98 | default="elog-gen-template.mako.hpp", |
Andrew Geissler | df048c1 | 2016-11-10 16:50:35 -0600 | [diff] [blame] | 99 | help="input mako template file to use") |
Andrew Geissler | 184690d | 2016-11-03 08:06:31 -0500 | [diff] [blame] | 100 | |
| 101 | parser.add_option("-o", "--output", dest="output_hpp", |
| 102 | default="elog-gen.hpp", |
Andrew Geissler | df048c1 | 2016-11-10 16:50:35 -0600 | [diff] [blame] | 103 | help="output hpp to generate, elog-gen.hpp is default") |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 104 | |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 105 | parser.add_option("-r", "--rootdir", dest="rootdir", |
| 106 | default="example", |
| 107 | help="Base directory of yaml files to process") |
| 108 | |
| 109 | parser.add_option("-t", "--templatedir", dest="templatedir", |
| 110 | default="phosphor-logging/templates/", |
| 111 | help="Base directory of files to process") |
| 112 | |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 113 | (options, args) = parser.parse_args(i_args) |
| 114 | |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 115 | # Verify the input yaml file |
Andrew Geissler | e859630 | 2016-11-21 16:06:53 -0600 | [diff] [blame] | 116 | yaml_path = "/".join((options.rootdir, options.elog_yaml)) |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 117 | if (not (os.path.isfile(yaml_path))): |
| 118 | print "Can not find input yaml file " + yaml_path |
Andrew Geissler | df048c1 | 2016-11-10 16:50:35 -0600 | [diff] [blame] | 119 | exit(1) |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 120 | |
Andrew Geissler | e859630 | 2016-11-21 16:06:53 -0600 | [diff] [blame] | 121 | # the meta data will be defined in a similar file name where we replace |
| 122 | # <Interface>.errors.yaml with <Interface>.logging.yaml |
| 123 | meta_file = options.elog_yaml.replace("errors", "logging") |
| 124 | meta_file = "/".join((options.rootdir, meta_file)) |
| 125 | if (not (os.path.isfile(meta_file))): |
| 126 | print "Can not find meta yaml file " + meta_file |
| 127 | exit(1) |
| 128 | |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 129 | # Verify the input mako file |
Andrew Geissler | e859630 | 2016-11-21 16:06:53 -0600 | [diff] [blame] | 130 | template_path = "/".join((options.templatedir, options.elog_mako)) |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 131 | if (not (os.path.isfile(template_path))): |
| 132 | print "Can not find input template file " + template_path |
| 133 | exit(1) |
| 134 | |
| 135 | gen_elog_hpp(options.rootdir, |
| 136 | options.elog_yaml, |
Andrew Geissler | e859630 | 2016-11-21 16:06:53 -0600 | [diff] [blame] | 137 | meta_file, |
Andrew Geissler | f1f2cfa | 2016-11-21 15:16:45 -0600 | [diff] [blame] | 138 | template_path, |
Andrew Geissler | 184690d | 2016-11-03 08:06:31 -0500 | [diff] [blame] | 139 | options.output_hpp) |
Andrew Geissler | c830e0f | 2016-10-18 12:51:29 -0500 | [diff] [blame] | 140 | |
| 141 | # Only run if it's a script |
| 142 | if __name__ == '__main__': |
Andrew Geissler | 184690d | 2016-11-03 08:06:31 -0500 | [diff] [blame] | 143 | main(sys.argv[1:]) |