Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 4 | This module provides functions which are useful for running plug-ins from a robot program. |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 5 | """ |
| 6 | |
| 7 | import sys |
| 8 | import subprocess |
| 9 | from robot.libraries.BuiltIn import BuiltIn |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 10 | import os |
| 11 | import tempfile |
| 12 | |
| 13 | import gen_print as gp |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 14 | import gen_misc as gm |
Michael Walsh | 957a2b2 | 2017-05-30 17:56:13 -0500 | [diff] [blame] | 15 | import gen_cmd as gc |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 16 | |
| 17 | |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 18 | def rvalidate_plug_ins(plug_in_dir_paths, |
| 19 | quiet=1): |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 20 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 21 | Call the external validate_plug_ins.py program which validates the plug-in dir paths given to it. Return |
| 22 | a list containing a normalized path for each plug-in selected. |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 23 | |
| 24 | Description of arguments: |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 25 | plug_in_dir_paths A colon-separated list of plug-in directory paths. |
| 26 | quiet If quiet is set to 1, this function will NOT write status messages to |
| 27 | stdout. |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 28 | """ |
| 29 | |
| 30 | cmd_buf = "validate_plug_ins.py \"" + plug_in_dir_paths + "\"" |
George Keishing | 36efbc0 | 2018-12-12 10:18:23 -0600 | [diff] [blame] | 31 | rc, out_buf = gc.shell_cmd(cmd_buf, print_output=0) |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 32 | if rc != 0: |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 33 | BuiltIn().fail(gp.sprint_error("Validate plug ins call failed. See" |
| 34 | + " stderr text for details.\n")) |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 35 | |
George Keishing | 36efbc0 | 2018-12-12 10:18:23 -0600 | [diff] [blame] | 36 | # plug_in_packages_list = out_buf.split("\n") |
| 37 | plug_in_packages_list = list(filter(None, out_buf.split("\n"))) |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 38 | if len(plug_in_packages_list) == 1 and plug_in_packages_list[0] == "": |
| 39 | return [] |
| 40 | |
| 41 | return plug_in_packages_list |
| 42 | |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 43 | |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 44 | def rprocess_plug_in_packages(plug_in_packages_list=None, |
| 45 | call_point="setup", |
| 46 | shell_rc="0x00000000", |
| 47 | stop_on_plug_in_failure=1, |
| 48 | stop_on_non_zero_rc=0, |
| 49 | release_type="obmc", |
| 50 | quiet=None, |
Michael Walsh | fa74bbb | 2018-10-30 13:21:03 -0500 | [diff] [blame] | 51 | debug=None, |
| 52 | return_history=False): |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 53 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 54 | Call the external process_plug_in_packages.py to process the plug-in packages. Return the following: |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 55 | rc The return code - 0 = PASS, 1 = FAIL. |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 56 | shell_rc The shell return code returned by process_plug_in_packages.py. |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 57 | failed_plug_in_name The failed plug in name (if any). |
| 58 | |
| 59 | Description of arguments: |
| 60 | plug_in_packages_list A python list of plug-in directory paths. |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 61 | call_point The call point program to be called for each plug-in package (e.g. |
| 62 | post_boot). This name should not include the "cp_" prefix. |
| 63 | shell_rc The user may supply a value other than zero to indicate an acceptable |
| 64 | non-zero return code. For example, if this value equals 0x00000200, it |
| 65 | means that for each plug-in call point that runs, a 0x00000200 will not |
| 66 | be counted as a failure. |
| 67 | stop_on_plug_in_failure If this parameter is set to 1, this program will stop and return non-zero |
| 68 | if the call point program from any plug-in directory fails. Conversely, |
| 69 | if it is set to false, this program will run the call point program from |
| 70 | each and every plug-in directory regardless of their return values. |
| 71 | Typical example cases where you'd want to run all plug-in call points |
| 72 | regardless of success or failure would be "cleanup" or "ffdc" call points. |
| 73 | stop_on_non_zero_rc If this parm is set to 1 and a plug-in call point program returns a valid |
| 74 | non-zero return code (see "shell_rc" parm above), this program will stop |
| 75 | processing and return 0 (success). Since this constitutes a successful |
| 76 | exit, this would normally be used where the caller wishes to stop |
| 77 | processing if one of the plug-in directory call point programs returns a |
| 78 | special value indicating that some special case has been found. An |
| 79 | example might be in calling some kind of "check_errl" call point program. |
| 80 | Such a call point program might return a 2 (i.e. 0x00000200) to indicate |
| 81 | that a given error log entry was found in an "ignore" list and is |
| 82 | therefore to be ignored. That being the case, no other "check_errl" call |
| 83 | point program would need to be called. |
| 84 | release_type The type of release being tested (e.g. "obmc", "op", "fips"). This |
| 85 | influences which integrated plug-ins are selected. |
| 86 | quiet If quiet is set to 1, this function will NOT write status messages to |
| 87 | stdout. This will default to the global quiet program parm or to 0. |
| 88 | debug If this parameter is set to 1, this function will print additional debug |
| 89 | information. This is mainly to be used by the developer of this |
| 90 | function. This will default to the global quiet program parm or to 0. |
| 91 | return_history In addition to rc, shell_rc and failed_plug_in_name, return a list |
| 92 | containing historical output that looks like the following: |
Michael Walsh | fa74bbb | 2018-10-30 13:21:03 -0500 | [diff] [blame] | 93 | |
| 94 | history: |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 95 | history[0]: #(CDT) 2018/10/30 12:25:49 - Running OBMC_Sample/cp_post_stack |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 96 | """ |
| 97 | |
| 98 | rc = 0 |
| 99 | |
Michael Walsh | fa74bbb | 2018-10-30 13:21:03 -0500 | [diff] [blame] | 100 | plug_in_packages_list = gp.get_var_value(plug_in_packages_list, []) |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 101 | |
| 102 | # If there are no plug-in packages to process, return successfully. |
| 103 | if len(plug_in_packages_list) == 0: |
Michael Walsh | fa74bbb | 2018-10-30 13:21:03 -0500 | [diff] [blame] | 104 | if return_history: |
| 105 | return 0, 0, "", [] |
| 106 | else: |
| 107 | return 0, 0, "" |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 108 | |
Michael Walsh | fa74bbb | 2018-10-30 13:21:03 -0500 | [diff] [blame] | 109 | quiet = int(gp.get_var_value(quiet, 0)) |
| 110 | debug = int(gp.get_var_value(debug, 0)) |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 111 | |
| 112 | # Create string from list. |
| 113 | plug_in_dir_paths = ':'.join(plug_in_packages_list) |
| 114 | |
| 115 | temp = tempfile.NamedTemporaryFile() |
| 116 | temp_file_path = temp.name |
| 117 | temp2 = tempfile.NamedTemporaryFile() |
| 118 | temp_properties_file_path = temp2.name |
| 119 | |
Michael Walsh | fa74bbb | 2018-10-30 13:21:03 -0500 | [diff] [blame] | 120 | if debug: |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 121 | os.environ["PERF_TRACE"] = "1" |
| 122 | debug_string = " --quiet=0" |
| 123 | else: |
| 124 | debug_string = "" |
| 125 | |
| 126 | loc_shell_rc = 0 |
| 127 | |
| 128 | sub_cmd_buf = "process_plug_in_packages.py" + debug_string +\ |
Michael Walsh | ed18ec7 | 2017-06-27 10:15:31 -0500 | [diff] [blame] | 129 | " --call_point=" + call_point + " --allow_shell_rc=" +\ |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 130 | str(shell_rc) + " --stop_on_plug_in_failure=" +\ |
| 131 | str(stop_on_plug_in_failure) + " --stop_on_non_zero_rc=" +\ |
| 132 | str(stop_on_non_zero_rc) + " " + plug_in_dir_paths |
Michael Walsh | fa74bbb | 2018-10-30 13:21:03 -0500 | [diff] [blame] | 133 | if quiet: |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 134 | cmd_buf = sub_cmd_buf + " > " + temp_file_path + " 2>&1" |
| 135 | else: |
Michael Walsh | 4bb56c5 | 2016-12-09 13:57:15 -0600 | [diff] [blame] | 136 | cmd_buf = "set -o pipefail ; " + sub_cmd_buf + " 2>&1 | tee " +\ |
| 137 | temp_file_path |
Michael Walsh | fa74bbb | 2018-10-30 13:21:03 -0500 | [diff] [blame] | 138 | if debug: |
| 139 | gp.print_issuing(cmd_buf) |
Michael Walsh | 0930518 | 2017-02-20 16:10:03 -0600 | [diff] [blame] | 140 | else: |
Michael Walsh | fa74bbb | 2018-10-30 13:21:03 -0500 | [diff] [blame] | 141 | gp.print_timen("Processing " + call_point |
| 142 | + " call point programs.") |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 143 | |
Michael Walsh | 35c4c62 | 2019-12-05 16:57:12 -0600 | [diff] [blame] | 144 | sub_proc = subprocess.Popen(cmd_buf, shell=True, executable='/bin/bash') |
| 145 | sub_proc.communicate() |
| 146 | proc_plug_pkg_rc = sub_proc.returncode |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 147 | |
Michael Walsh | fa74bbb | 2018-10-30 13:21:03 -0500 | [diff] [blame] | 148 | if return_history: |
| 149 | # Get the "Running" statements from the output. |
| 150 | regex = " Running [^/]+/cp_" |
| 151 | cmd_buf = "egrep '" + regex + "' " + temp_file_path |
| 152 | _, history = gc.shell_cmd(cmd_buf, quiet=(not debug), print_output=0, |
| 153 | show_err=0, ignore_err=1) |
| 154 | history = [x + "\n" for x in filter(None, history.split("\n"))] |
| 155 | else: |
| 156 | history = [] |
| 157 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 158 | # As process_plug_in_packages.py help text states, it will print the values of failed_plug_in_name and |
| 159 | # shell_rc in the following format: |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 160 | # failed_plug_in_name: <failed plug-in value, if any> |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 161 | # shell_rc: <shell return code value of last call point program> |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 162 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 163 | # We want to obtain those values from the output. To make the task simpler, we'll start by grepping the |
| 164 | # output for lines that might fit such a format: |
Michael Walsh | fa74bbb | 2018-10-30 13:21:03 -0500 | [diff] [blame] | 165 | # A valid bash variable against the left margin followed by... |
| 166 | # - A colon followed by... |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 167 | # - Zero or more spaces |
Michael Walsh | 957a2b2 | 2017-05-30 17:56:13 -0500 | [diff] [blame] | 168 | bash_var_regex = "[_[:alpha:]][_[:alnum:]]*" |
| 169 | regex = "^" + bash_var_regex + ":[ ]*" |
| 170 | cmd_buf = "egrep '" + regex + "' " + temp_file_path + " > " +\ |
| 171 | temp_properties_file_path |
Michael Walsh | fa74bbb | 2018-10-30 13:21:03 -0500 | [diff] [blame] | 172 | gp.dprint_issuing(cmd_buf) |
Michael Walsh | 957a2b2 | 2017-05-30 17:56:13 -0500 | [diff] [blame] | 173 | grep_rc = os.system(cmd_buf) |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 174 | |
| 175 | # Next we call my_parm_file to create a properties dictionary. |
| 176 | properties = gm.my_parm_file(temp_properties_file_path) |
| 177 | |
| 178 | # Finally, we access the 2 values that we need. |
Michael Walsh | fa74bbb | 2018-10-30 13:21:03 -0500 | [diff] [blame] | 179 | shell_rc = int(properties.get('shell_rc', '0x0000000000000000'), 16) |
| 180 | failed_plug_in_name = properties.get('failed_plug_in_name', '') |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 181 | |
Michael Walsh | 957a2b2 | 2017-05-30 17:56:13 -0500 | [diff] [blame] | 182 | if proc_plug_pkg_rc != 0: |
Michael Walsh | 0a3bdb4 | 2019-01-31 16:21:44 +0000 | [diff] [blame] | 183 | if quiet: |
| 184 | os.system("cat " + temp_file_path + " >&2") |
Michael Walsh | fa74bbb | 2018-10-30 13:21:03 -0500 | [diff] [blame] | 185 | if grep_rc != 0: |
Michael Walsh | 0d5f96a | 2019-05-20 10:09:57 -0500 | [diff] [blame] | 186 | gp.print_var(grep_rc, gp.hexa()) |
| 187 | gp.print_var(proc_plug_pkg_rc, gp.hexa()) |
Michael Walsh | fa74bbb | 2018-10-30 13:21:03 -0500 | [diff] [blame] | 188 | gp.print_timen("Re-cap of plug-in failures:") |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 189 | gc.cmd_fnc_u("egrep -A 1 '^failed_plug_in_name:[ ]+' " |
Michael Walsh | fa74bbb | 2018-10-30 13:21:03 -0500 | [diff] [blame] | 190 | + temp_properties_file_path + " | egrep -v '^\\--'", |
| 191 | quiet=1, show_err=0) |
Michael Walsh | 4082237 | 2017-01-10 11:33:08 -0600 | [diff] [blame] | 192 | rc = 1 |
Michael Walsh | ffee58a | 2016-11-22 11:28:33 -0600 | [diff] [blame] | 193 | |
Michael Walsh | fa74bbb | 2018-10-30 13:21:03 -0500 | [diff] [blame] | 194 | if return_history: |
| 195 | return rc, shell_rc, failed_plug_in_name, history |
| 196 | else: |
| 197 | return rc, shell_rc, failed_plug_in_name |