blob: 2e51626eca922baa1bf84789e8d52d48ceac0bf2 [file] [log] [blame]
George Keishinge7e91712021-09-03 11:28:44 -05001#!/usr/bin/env python3
Michael Walsh3ba8ecd2018-04-24 11:33:25 -05002
3r"""
Michael Walsh410b1782019-10-22 15:56:18 -05004This module provides functions which are useful to plug-ins call-point programs that wish to make external
5robot program calls.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -05006"""
7
Patrick Williams57318182022-12-08 06:18:26 -06008import sys
George Keishinge635ddc2022-12-08 07:38:02 -06009import os
10import subprocess
11import re
Patrick Williams57318182022-12-08 06:18:26 -060012import time
George Keishinge635ddc2022-12-08 07:38:02 -060013import imp
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050014
15import gen_print as gp
16import gen_valid as gv
George Keishinge635ddc2022-12-08 07:38:02 -060017import gen_misc as gm
18import gen_cmd as gc
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050019
George Keishinge635ddc2022-12-08 07:38:02 -060020base_path = \
21 os.path.dirname(os.path.dirname(imp.find_module("gen_robot_print")[1])) +\
22 os.sep
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050023
24
25def init_robot_out_parms(extra_prefix=""):
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050026 r"""
27 Initialize robot output parms such as outputdir, output, etc.
28
29 This function will set global values for the following robot output parms.
30
Michael Walsh1f961b72020-06-16 15:54:11 -050031 outputdir, output, log, report, loglevel, consolecolors, consolemarkers
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050032
Michael Walsh410b1782019-10-22 15:56:18 -050033 This function would typically be called prior to calling create_robot_cmd_string.
Michael Walshf33140f2018-11-01 14:05:56 -050034
35 Description of argument(s):
Michael Walsh410b1782019-10-22 15:56:18 -050036 extra_prefix An extra prefix to be appended to the default prefix for output file
37 names.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050038 """
39
Michael Walsha0ce75a2018-07-31 13:54:29 -050040 gp.dprint_executing()
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050041 AUTOBOOT_OPENBMC_NICKNAME = gm.get_mod_global("AUTOBOOT_OPENBMC_NICKNAME")
42
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050043 # Set values for call to create_robot_cmd_string.
Michael Walsh410b1782019-10-22 15:56:18 -050044 # Environment variable TMP_ROBOT_DIR_PATH can be set by the user to indicate that robot-generated output
45 # should initially be written to the specified temporary directory and then moved to the normal output
Michael Walsha0ce75a2018-07-31 13:54:29 -050046 # location after completion.
George Keishinge635ddc2022-12-08 07:38:02 -060047 outputdir =\
48 os.environ.get("TMP_ROBOT_DIR_PATH",
49 os.environ.get("STATUS_DIR_PATH",
50 os.environ.get("HOME", ".")
51 + "/status"))
Michael Walsha0ce75a2018-07-31 13:54:29 -050052 outputdir = gm.add_trailing_slash(outputdir)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050053 seconds = time.time()
54 loc_time = time.localtime(seconds)
55 time_string = time.strftime("%y%m%d.%H%M%S", loc_time)
George Keishinge635ddc2022-12-08 07:38:02 -060056 file_prefix = AUTOBOOT_OPENBMC_NICKNAME + "." + extra_prefix +\
57 time_string + "."
Michael Walsh410b1782019-10-22 15:56:18 -050058 # Environment variable SAVE_STATUS_POLICY governs when robot-generated output files (e.g. the log.html)
59 # will be moved from TMP_ROBOT_DIR_PATH to FFDC_DIR_PATH. Valid values are "ALWAYS", "NEVER" and "FAIL".
Michael Walsha0ce75a2018-07-31 13:54:29 -050060 SAVE_STATUS_POLICY = os.environ.get("SAVE_STATUS_POLICY", "ALWAYS")
61 if SAVE_STATUS_POLICY == "NEVER":
62 output = "NONE"
63 log = "NONE"
64 report = "NONE"
65 else:
66 output = file_prefix + "output.xml"
67 log = file_prefix + "log.html"
68 report = file_prefix + "report.html"
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050069 loglevel = "TRACE"
George Keishinge635ddc2022-12-08 07:38:02 -060070 consolecolors = 'off'
71 consolemarkers = 'off'
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050072
73 # Make create_robot_cmd_string values global.
74 gm.set_mod_global(outputdir)
75 gm.set_mod_global(output)
76 gm.set_mod_global(log)
77 gm.set_mod_global(report)
78 gm.set_mod_global(loglevel)
Michael Walsh1f961b72020-06-16 15:54:11 -050079 gm.set_mod_global(consolecolors)
80 gm.set_mod_global(consolemarkers)
81
George Keishinge635ddc2022-12-08 07:38:02 -060082 return outputdir, output, log, report, loglevel, consolecolors, consolemarkers
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050083
84
85def init_robot_test_base_dir_path():
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050086 r"""
Michael Walsh410b1782019-10-22 15:56:18 -050087 Initialize and validate the environment variable, ROBOT_TEST_BASE_DIR_PATH and set corresponding global
88 variable ROBOT_TEST_RUNNING_FROM_SB.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050089
Michael Walsh410b1782019-10-22 15:56:18 -050090 If ROBOT_TEST_BASE_DIR_PATH is already set, this function will merely validate it. This function will
91 also set environment variable ROBOT_TEST_RUNNING_FROM_SB when ROBOT_TEST_BASE_DIR_PATH is not pre-set.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050092 """
93
94 # ROBOT_TEST_BASE_DIR_PATH will be set as follows:
Michael Walsh410b1782019-10-22 15:56:18 -050095 # This function will determine whether we are running in a user sandbox or from a standard apolloxxx
96 # environment.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050097 # - User sandbox:
Michael Walsh410b1782019-10-22 15:56:18 -050098 # If there is a <developer's home dir>/git/openbmc-test-automation/, ROBOT_TEST_BASE_DIR_PATH will be
99 # set to that path. Otherwise, we set it to <program dir path>/git/openbmc-test-automation/
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500100 # - Not in user sandbox:
Michael Walsh410b1782019-10-22 15:56:18 -0500101 # ROBOT_TEST_BASE_DIR_PATH will be set to <program dir path>/git/openbmc-test-automation/
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500102
George Keishinge635ddc2022-12-08 07:38:02 -0600103 ROBOT_TEST_BASE_DIR_PATH = os.environ.get('ROBOT_TEST_BASE_DIR_PATH', "")
104 ROBOT_TEST_RUNNING_FROM_SB = \
105 int(os.environ.get('ROBOT_TEST_RUNNING_FROM_SB', "0"))
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500106 if ROBOT_TEST_BASE_DIR_PATH == "":
107 # ROBOT_TEST_BASE_DIR_PATH was not set by user/caller.
George Keishinge635ddc2022-12-08 07:38:02 -0600108 AUTOIPL_VERSION = os.environ.get('AUTOIPL_VERSION', '')
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500109 if AUTOIPL_VERSION == "":
110 ROBOT_TEST_BASE_DIR_PATH = base_path
111 else:
112 suffix = "git/openbmc-test-automation/"
113
Michael Walsh410b1782019-10-22 15:56:18 -0500114 # Determine whether we're running out of a developer sandbox or simply out of an apolloxxx/bin
115 # path.
George Keishinge635ddc2022-12-08 07:38:02 -0600116 shell_rc, out_buf = gc.shell_cmd('dirname $(which gen_print.py)',
117 quiet=(not debug), print_output=0)
Michael Walshbffaa1d2018-06-08 15:09:27 -0500118 executable_base_dir_path = os.path.realpath(out_buf.rstrip()) + "/"
George Keishinge635ddc2022-12-08 07:38:02 -0600119 apollo_dir_path = os.environ['AUTO_BASE_PATH'] + AUTOIPL_VERSION +\
120 "/bin/"
121 developer_home_dir_path = re.sub('/sandbox.*', '',
122 executable_base_dir_path)
123 developer_home_dir_path = \
124 gm.add_trailing_slash(developer_home_dir_path)
125 gp.dprint_vars(executable_base_dir_path, developer_home_dir_path,
126 apollo_dir_path)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500127
128 ROBOT_TEST_RUNNING_FROM_SB = 0
129 if executable_base_dir_path != apollo_dir_path:
130 ROBOT_TEST_RUNNING_FROM_SB = 1
131 gp.dprint_vars(ROBOT_TEST_RUNNING_FROM_SB)
132 ROBOT_TEST_BASE_DIR_PATH = developer_home_dir_path + suffix
133 if not os.path.isdir(ROBOT_TEST_BASE_DIR_PATH):
George Keishinge635ddc2022-12-08 07:38:02 -0600134 gp.dprint_timen("NOTE: Sandbox directory "
135 + ROBOT_TEST_BASE_DIR_PATH + " does not"
136 + " exist.")
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500137 # Fall back to the apollo dir path.
138 ROBOT_TEST_BASE_DIR_PATH = apollo_dir_path + suffix
139 else:
140 # Use to the apollo dir path.
141 ROBOT_TEST_BASE_DIR_PATH = apollo_dir_path + suffix
142
George Keishinge635ddc2022-12-08 07:38:02 -0600143 OBMC_TOOLS_BASE_DIR_PATH = \
144 os.path.dirname(ROBOT_TEST_BASE_DIR_PATH.rstrip("/")) \
Michael Walsh8dc99a32020-04-02 13:45:13 -0500145 + "/openbmc-tools/"
George Keishinga7b19122020-10-04 23:03:04 -0500146 OPENBMCTOOL_DIR_PATH = OBMC_TOOLS_BASE_DIR_PATH + "openbmctool/"
George Keishinge635ddc2022-12-08 07:38:02 -0600147 JSON_CHECKER_TOOLS_DIR_PATH = OBMC_TOOLS_BASE_DIR_PATH + "expectedJsonChecker/"
Michael Walsh8dc99a32020-04-02 13:45:13 -0500148
Michael Walsh2ea965c2019-08-01 16:14:25 -0500149 gv.valid_value(ROBOT_TEST_BASE_DIR_PATH)
George Keishinge635ddc2022-12-08 07:38:02 -0600150 gp.dprint_vars(ROBOT_TEST_RUNNING_FROM_SB, ROBOT_TEST_BASE_DIR_PATH, OBMC_TOOLS_BASE_DIR_PATH,
151 OPENBMCTOOL_DIR_PATH, JSON_CHECKER_TOOLS_DIR_PATH)
Michael Walsh2ea965c2019-08-01 16:14:25 -0500152 gv.valid_dir_path(ROBOT_TEST_BASE_DIR_PATH)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500153
154 ROBOT_TEST_BASE_DIR_PATH = gm.add_trailing_slash(ROBOT_TEST_BASE_DIR_PATH)
155 gm.set_mod_global(ROBOT_TEST_BASE_DIR_PATH)
George Keishinge635ddc2022-12-08 07:38:02 -0600156 os.environ['ROBOT_TEST_BASE_DIR_PATH'] = ROBOT_TEST_BASE_DIR_PATH
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500157
158 gm.set_mod_global(ROBOT_TEST_RUNNING_FROM_SB)
George Keishinge635ddc2022-12-08 07:38:02 -0600159 os.environ['ROBOT_TEST_RUNNING_FROM_SB'] = str(ROBOT_TEST_RUNNING_FROM_SB)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500160
Michael Walsh8dc99a32020-04-02 13:45:13 -0500161 gm.set_mod_global(OBMC_TOOLS_BASE_DIR_PATH)
George Keishinge635ddc2022-12-08 07:38:02 -0600162 os.environ['OBMC_TOOLS_BASE_DIR_PATH'] = str(OBMC_TOOLS_BASE_DIR_PATH)
Michael Walsh8dc99a32020-04-02 13:45:13 -0500163
164 gm.set_mod_global(OPENBMCTOOL_DIR_PATH)
George Keishinge635ddc2022-12-08 07:38:02 -0600165 os.environ['OPENBMCTOOL_DIR_PATH'] = str(OPENBMCTOOL_DIR_PATH)
Michael Walsh8dc99a32020-04-02 13:45:13 -0500166
George Keishinga7b19122020-10-04 23:03:04 -0500167 gm.set_mod_global(JSON_CHECKER_TOOLS_DIR_PATH)
George Keishinge635ddc2022-12-08 07:38:02 -0600168 os.environ['JSON_CHECKER_TOOLS_DIR_PATH'] = str(JSON_CHECKER_TOOLS_DIR_PATH)
Michael Walsh8dc99a32020-04-02 13:45:13 -0500169
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500170
George Keishinge635ddc2022-12-08 07:38:02 -0600171raw_robot_file_search_path = "${ROBOT_TEST_BASE_DIR_PATH}:" +\
172 "${ROBOT_TEST_BASE_DIR_PATH}tests:${ROBOT_TEST_BASE_DIR_PATH}extended:" +\
173 "${ROBOT_TEST_BASE_DIR_PATH}scratch:${PATH}"
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500174
175
176def init_robot_file_path(robot_file_path):
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500177 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500178 Determine full path name for the file path passed in robot_file_path and return it.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500179
Michael Walsh410b1782019-10-22 15:56:18 -0500180 If robot_file_path contains a fully qualified path name, this function will verify that the file exists.
181 If robot_file_path contains a relative path, this function will search for the file and set
182 robot_file_path so that it contains the absolute path to the robot file. This function will search for
183 the robot file using the raw_robot_file_search_path (defined above). Note that if
184 ROBOT_TEST_BASE_DIR_PATH is not set, this function will call init_robot_test_base_dir_path to set it.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500185
186 Description of arguments:
Michael Walsh410b1782019-10-22 15:56:18 -0500187 robot_file_path The absolute or relative path to a robot file.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500188 """
189
Michael Walsh2ea965c2019-08-01 16:14:25 -0500190 gv.valid_value(robot_file_path)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500191
192 try:
193 if ROBOT_TEST_BASE_DIR_PATH is NONE:
194 init_robot_test_base_dir_path()
195 except NameError:
196 init_robot_test_base_dir_path()
197
198 if not re.match(r".*\.(robot|py)$", robot_file_path):
199 # No suffix so we'll assign one of "\.robot".
200 robot_file_path = robot_file_path + ".robot"
201
202 abs_path = 0
203 if robot_file_path[0:1] == "/":
204 abs_path = 1
205
206 gp.dprint_vars(abs_path, robot_file_path)
207
208 if not abs_path:
George Keishinge635ddc2022-12-08 07:38:02 -0600209 cmd_buf = "echo -n \"" + raw_robot_file_search_path + "\""
210 shell_rc, out_buf = gc.shell_cmd(cmd_buf, quiet=(not debug),
211 print_output=0)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500212 robot_file_search_paths = out_buf
Michael Walsha0ce75a2018-07-31 13:54:29 -0500213 gp.dprint_var(robot_file_search_paths)
George Keishinge635ddc2022-12-08 07:38:02 -0600214 robot_file_search_paths_list = robot_file_search_paths.split(':')
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500215 for search_path in robot_file_search_paths_list:
216 search_path = gm.add_trailing_slash(search_path)
217 candidate_file_path = search_path + robot_file_path
218 gp.dprint_var(candidate_file_path)
219 if os.path.isfile(candidate_file_path):
220 gp.dprint_timen("Found full path to " + robot_file_path + ".")
221 robot_file_path = candidate_file_path
222 break
223
224 gp.dprint_var(robot_file_path)
Michael Walsh2ea965c2019-08-01 16:14:25 -0500225 gv.valid_file_path(robot_file_path)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500226
227 return robot_file_path
228
229
230def get_robot_parm_names():
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500231 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500232 Return a list containing all of the long parm names (e.g. --outputdir) supported by the robot program.
233 Double dashes are not included in the names returned.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500234 """
235
George Keishinge635ddc2022-12-08 07:38:02 -0600236 cmd_buf = "robot -h | egrep " +\
237 "'^([ ]\\-[a-zA-Z0-9])?[ ]+--[a-zA-Z0-9]+[ ]+' | sed -re" +\
238 " s'/.*\\-\\-//g' -e s'/ .*//g' | sort -u"
Michael Walshbffaa1d2018-06-08 15:09:27 -0500239 shell_rc, out_buf = gc.shell_cmd(cmd_buf, quiet=1, print_output=0)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500240
241 return out_buf.split("\n")
242
243
244def create_robot_cmd_string(robot_file_path, *parms):
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500245 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500246 Create a robot command string and return it. On failure, return an empty string.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500247
248 Description of arguments:
249 robot_file_path The path to the robot file to be run.
Michael Walsh410b1782019-10-22 15:56:18 -0500250 parms The list of parms to be included in the command string. The name of each
251 variable in this list must be the same as the name of the corresponding
252 parm. This function figures out that name. This function is also able
253 to distinguish robot parms (e.g. --outputdir) from robot program parms
254 (all other parms which will be passed as "-v PARM_NAME:parm_value")..
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500255
256 Example:
257
258 The following call to this function...
Michael Walsh410b1782019-10-22 15:56:18 -0500259 cmd_buf = create_robot_cmd_string("tools/start_sol_console.robot", OPENBMC_HOST, quiet, test_mode, debug,
260 outputdir, output, log, report)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500261
262 Would return a string something like this.
263 robot -v OPENBMC_HOST:beye6 -v quiet:0 -v test_mode:1 -v debug:1
Michael Walsh410b1782019-10-22 15:56:18 -0500264 --outputdir=/gsa/ausgsa/projects/a/status --output=beye6.OS_Console.output.xml
265 --log=beye6.OS_Console.log.html --report=beye6.OS_Console.report.html tools/start_sol_console.robot
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500266 """
267
268 robot_file_path = init_robot_file_path(robot_file_path)
269
270 robot_parm_names = get_robot_parm_names()
271
272 robot_parm_list = []
273
274 stack_frame = 2
275 ix = 2
276 for arg in parms:
277 parm = arg
278 parm = gm.quote_bash_parm(gm.escape_bash_quotes(str(parm)))
279 var_name = gp.get_arg_name(None, ix, stack_frame)
280 if var_name in robot_parm_names:
281 p_string = "--" + var_name + "=" + str(parm)
282 robot_parm_list.append(p_string)
283 else:
284 p_string = "-v " + var_name + ":" + str(parm)
285 robot_parm_list.append(p_string)
286 ix += 1
287
George Keishinge635ddc2022-12-08 07:38:02 -0600288 robot_cmd_buf = "robot " + ' '.join(robot_parm_list) + " " +\
289 robot_file_path
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500290
291 return robot_cmd_buf
292
293
Michael Walsha0ce75a2018-07-31 13:54:29 -0500294# Global variables to aid in cleanup after running robot_cmd_fnc.
295gcr_last_robot_cmd_buf = ""
296gcr_last_robot_rc = 0
297
298
George Keishinge635ddc2022-12-08 07:38:02 -0600299def process_robot_output_files(robot_cmd_buf=None,
300 robot_rc=None,
301 gzip=None):
Michael Walsha0ce75a2018-07-31 13:54:29 -0500302 r"""
303 Process robot output files which can involve several operations:
Michael Walsh410b1782019-10-22 15:56:18 -0500304 - If the files are in a temporary location, using SAVE_STATUS_POLICY to decide whether to move them to a
305 permanent location or to delete them.
Michael Walsha0ce75a2018-07-31 13:54:29 -0500306 - Gzipping them.
307
308 Description of argument(s):
Michael Walsh410b1782019-10-22 15:56:18 -0500309 robot_cmd_buf The complete command string used to invoke robot.
310 robot_rc The return code from running the robot command string.
311 gzip Indicates whether robot-generated output should be gzipped.
Michael Walsha0ce75a2018-07-31 13:54:29 -0500312 """
313
314 robot_cmd_buf = gm.dft(robot_cmd_buf, gcr_last_robot_cmd_buf)
315 robot_rc = gm.dft(robot_rc, gcr_last_robot_rc)
Michael Walshf33140f2018-11-01 14:05:56 -0500316 gzip = gm.dft(gzip, int(os.environ.get("GZIP_ROBOT", "1")))
Michael Walsha0ce75a2018-07-31 13:54:29 -0500317
318 if robot_cmd_buf == "":
Michael Walsh410b1782019-10-22 15:56:18 -0500319 # This can legitimately occur if this function is called from an exit_function without the program
320 # having ever run robot_cmd_fnc.
Michael Walsha0ce75a2018-07-31 13:54:29 -0500321 return
322
323 SAVE_STATUS_POLICY = os.environ.get("SAVE_STATUS_POLICY", "ALWAYS")
324 gp.qprint_vars(SAVE_STATUS_POLICY)
325
Michael Walsh410b1782019-10-22 15:56:18 -0500326 # When SAVE_STATUS_POLICY is "NEVER" robot output files don't even get generated.
Michael Walsha0ce75a2018-07-31 13:54:29 -0500327 if SAVE_STATUS_POLICY == "NEVER":
328 return
329
330 # Compose file_list based on robot command buffer passed in.
331 robot_cmd_buf_dict = gc.parse_command_string(robot_cmd_buf)
George Keishinge635ddc2022-12-08 07:38:02 -0600332 outputdir = robot_cmd_buf_dict['outputdir']
Michael Walsha0ce75a2018-07-31 13:54:29 -0500333 outputdir = gm.add_trailing_slash(outputdir)
George Keishinge635ddc2022-12-08 07:38:02 -0600334 file_list = outputdir + robot_cmd_buf_dict['output'] + " " + outputdir\
335 + robot_cmd_buf_dict['log'] + " " + outputdir\
336 + robot_cmd_buf_dict['report']
Michael Walsha0ce75a2018-07-31 13:54:29 -0500337
338 # Double checking that files are present.
George Keishinge635ddc2022-12-08 07:38:02 -0600339 shell_rc, out_buf = gc.shell_cmd("ls -1 " + file_list + " 2>/dev/null",
340 show_err=0)
Michael Walsha0ce75a2018-07-31 13:54:29 -0500341 file_list = re.sub("\n", " ", out_buf.rstrip("\n"))
342
343 if file_list == "":
George Keishinge635ddc2022-12-08 07:38:02 -0600344 gp.qprint_timen("No robot output files were found in " + outputdir
345 + ".")
Michael Walsha0ce75a2018-07-31 13:54:29 -0500346 return
Michael Walsh0d5f96a2019-05-20 10:09:57 -0500347 gp.qprint_var(robot_rc, gp.hexa())
Michael Walsha0ce75a2018-07-31 13:54:29 -0500348 if SAVE_STATUS_POLICY == "FAIL" and robot_rc == 0:
George Keishinge635ddc2022-12-08 07:38:02 -0600349 gp.qprint_timen("The call to robot produced no failures."
350 + " Deleting robot output files.")
Michael Walsha0ce75a2018-07-31 13:54:29 -0500351 gc.shell_cmd("rm -rf " + file_list)
352 return
353
354 if gzip:
Michael Walshe53dfec2018-08-07 15:02:56 -0500355 gc.shell_cmd("gzip -f " + file_list)
Michael Walsha0ce75a2018-07-31 13:54:29 -0500356 # Update the values in file_list.
357 file_list = re.sub(" ", ".gz ", file_list) + ".gz"
358
Michael Walsh410b1782019-10-22 15:56:18 -0500359 # It TMP_ROBOT_DIR_PATH is set, it means the caller wanted the robot output initially directed to
360 # TMP_ROBOT_DIR_PATH but later moved to FFDC_DIR_PATH. Otherwise, we're done.
Michael Walsha0ce75a2018-07-31 13:54:29 -0500361
George Keishinge7e91712021-09-03 11:28:44 -0500362 if os.environ.get("TMP_ROBOT_DIR_PATH", "") == "":
Michael Walsha0ce75a2018-07-31 13:54:29 -0500363 return
364
Michael Walsh410b1782019-10-22 15:56:18 -0500365 # We're directing these to the FFDC dir path so that they'll be subjected to FFDC cleanup.
George Keishinge635ddc2022-12-08 07:38:02 -0600366 target_dir_path = os.environ.get("FFDC_DIR_PATH",
367 os.environ.get("HOME", ".")
368 + "/ffdc")
Michael Walsha0ce75a2018-07-31 13:54:29 -0500369 target_dir_path = gm.add_trailing_slash(target_dir_path)
370
George Keishinge635ddc2022-12-08 07:38:02 -0600371 targ_file_list = [re.sub(".*/", target_dir_path, x)
372 for x in file_list.split(" ")]
Michael Walsha0ce75a2018-07-31 13:54:29 -0500373
George Keishinge635ddc2022-12-08 07:38:02 -0600374 gc.shell_cmd("mv " + file_list + " " + target_dir_path + " >/dev/null",
375 time_out=600)
Michael Walsha0ce75a2018-07-31 13:54:29 -0500376
377 gp.qprint_timen("New robot log file locations:")
George Keishinge635ddc2022-12-08 07:38:02 -0600378 gp.qprintn('\n'.join(targ_file_list))
Michael Walsha0ce75a2018-07-31 13:54:29 -0500379
380
George Keishinge635ddc2022-12-08 07:38:02 -0600381def robot_cmd_fnc(robot_cmd_buf,
382 robot_jail=os.environ.get('ROBOT_JAIL', ''), quiet=None, test_mode=0):
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500383 r"""
384 Run the robot command string.
385
Michael Walsh410b1782019-10-22 15:56:18 -0500386 This function will set the various PATH variables correctly so that you are running the proper version of
387 all imported files, etc.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500388
389 Description of argument(s):
390 robot_cmd_buf The complete robot command string.
Michael Walsh410b1782019-10-22 15:56:18 -0500391 robot_jail Indicates that this is to run in "robot jail" meaning without visibility
392 to any apolloxxx import files, programs, etc.
Michael Walsh8dc99a32020-04-02 13:45:13 -0500393 test_mode If test_mode is set, this function will not actually run the command.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500394 """
395
George Keishinge635ddc2022-12-08 07:38:02 -0600396 quiet = int(gm.dft(quiet, gp.get_stack_var('quiet', 0)))
Michael Walsh2ea965c2019-08-01 16:14:25 -0500397 gv.valid_value(robot_cmd_buf)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500398
Michael Walsha0ce75a2018-07-31 13:54:29 -0500399 # Set global variables to aid in cleanup with process_robot_output_files.
400 global gcr_last_robot_cmd_buf
401 global gcr_last_robot_rc
402 gcr_last_robot_cmd_buf = robot_cmd_buf
403
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500404 # Get globals set by init_robot_test_base_dir_path().
405 module = sys.modules["__main__"]
406 try:
407 ROBOT_TEST_BASE_DIR_PATH = getattr(module, "ROBOT_TEST_BASE_DIR_PATH")
408 except NameError:
409 init_robot_test_base_dir_path()
410 ROBOT_TEST_BASE_DIR_PATH = getattr(module, "ROBOT_TEST_BASE_DIR_PATH")
411
George Keishinge635ddc2022-12-08 07:38:02 -0600412 ROBOT_TEST_RUNNING_FROM_SB = gm.get_mod_global("ROBOT_TEST_RUNNING_FROM_SB")
Michael Walsh8dc99a32020-04-02 13:45:13 -0500413 OPENBMCTOOL_DIR_PATH = gm.get_mod_global("OPENBMCTOOL_DIR_PATH")
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500414
415 if robot_jail == "":
416 if ROBOT_TEST_RUNNING_FROM_SB:
417 robot_jail = 0
418 else:
419 robot_jail = 1
420
421 robot_jail = int(robot_jail)
George Keishinge635ddc2022-12-08 07:38:02 -0600422 ROBOT_JAIL = os.environ.get('ROBOT_JAIL', '')
423 gp.dprint_vars(ROBOT_TEST_BASE_DIR_PATH, ROBOT_TEST_RUNNING_FROM_SB,
424 ROBOT_JAIL, robot_jail)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500425
426 # Save PATH and PYTHONPATH to be restored later.
427 os.environ["SAVED_PYTHONPATH"] = os.environ.get("PYTHONPATH", "")
428 os.environ["SAVED_PATH"] = os.environ.get("PATH", "")
429
430 if robot_jail:
Michael Walsh410b1782019-10-22 15:56:18 -0500431 # Make sure required programs like python and robot can be found in the new restricted PATH.
Michael Walsha5d29bc2019-06-05 15:29:18 -0500432 required_programs = "python robot"
Michael Walsh410b1782019-10-22 15:56:18 -0500433 # It is expected that there will be a "python" program in the tool base bin path which is really a
434 # link to select_version. Ditto for "robot". Call each with the --print_only option to get the
435 # paths to the "real" programs.
George Keishinge635ddc2022-12-08 07:38:02 -0600436 cmd_buf = "for program in " + required_programs \
Michael Walsha5d29bc2019-06-05 15:29:18 -0500437 + " ; do dirname $(${program} --print_only) ; done 2>/dev/null"
438 rc, out_buf = gc.shell_cmd(cmd_buf, quiet=1, print_output=0)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500439 PYTHONPATH = ROBOT_TEST_BASE_DIR_PATH + "lib"
440 NEW_PATH_LIST = [ROBOT_TEST_BASE_DIR_PATH + "bin"]
Michael Walsha5d29bc2019-06-05 15:29:18 -0500441 NEW_PATH_LIST.extend(list(set(out_buf.rstrip("\n").split("\n"))))
George Keishinge635ddc2022-12-08 07:38:02 -0600442 NEW_PATH_LIST.extend(["/usr/local/sbin", "/usr/local/bin", "/usr/sbin",
443 "/usr/bin", "/sbin", "/bin",
444 OPENBMCTOOL_DIR_PATH.rstrip('/')])
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500445 PATH = ":".join(NEW_PATH_LIST)
446 else:
George Keishinge635ddc2022-12-08 07:38:02 -0600447 PYTHONPATH = os.environ.get('PYTHONPATH', '') + ":" +\
448 ROBOT_TEST_BASE_DIR_PATH + "lib"
449 PATH = os.environ.get('PATH', '') + ":" + ROBOT_TEST_BASE_DIR_PATH +\
450 "bin" + ":" + OPENBMCTOOL_DIR_PATH.rstrip('/')
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500451
George Keishinge635ddc2022-12-08 07:38:02 -0600452 os.environ['PYTHONPATH'] = PYTHONPATH
453 os.environ['PATH'] = PATH
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500454 gp.dprint_vars(PATH, PYTHONPATH)
455
George Keishinge635ddc2022-12-08 07:38:02 -0600456 os.environ['FFDC_DIR_PATH_STYLE'] = os.environ.get('FFDC_DIR_PATH_STYLE',
457 '1')
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500458 gp.qpissuing(robot_cmd_buf, test_mode)
459 if test_mode:
460 os.environ["PATH"] = os.environ.get("SAVED_PATH", "")
461 os.environ["PYTHONPATH"] = os.environ.get("SAVED_PYTHONPATH", "")
462 return True
463
464 if quiet:
George Keishinge635ddc2022-12-08 07:38:02 -0600465 DEVNULL = open(os.devnull, 'wb')
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500466 stdout = DEVNULL
467 else:
468 stdout = None
469 sub_proc = subprocess.Popen(robot_cmd_buf, stdout=stdout, shell=True)
470 sub_proc.communicate()
471 shell_rc = sub_proc.returncode
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500472 os.environ["PATH"] = os.environ.get("SAVED_PATH", "")
473 os.environ["PYTHONPATH"] = os.environ.get("SAVED_PYTHONPATH", "")
Michael Walsha0ce75a2018-07-31 13:54:29 -0500474 gcr_last_robot_rc = shell_rc
475 process_robot_output_files()
476 if shell_rc != 0:
Michael Walsh0d5f96a2019-05-20 10:09:57 -0500477 gp.print_var(shell_rc, gp.hexa())
Michael Walsha0ce75a2018-07-31 13:54:29 -0500478 return False
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500479
480 return True