blob: 91231bffed8c8908bc13e00df6fc2b5d2c841830 [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
George Keishinge635ddc2022-12-08 07:38:02 -06008import imp
Patrick Williams20f38712022-12-08 06:18:26 -06009import os
10import re
11import subprocess
12import sys
13import time
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050014
Patrick Williams20f38712022-12-08 06:18:26 -060015import gen_cmd as gc
16import gen_misc as gm
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050017import gen_print as gp
18import gen_valid as gv
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050019
Patrick Williams20f38712022-12-08 06:18:26 -060020base_path = (
21 os.path.dirname(os.path.dirname(imp.find_module("gen_robot_print")[1]))
22 + os.sep
23)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050024
25
26def init_robot_out_parms(extra_prefix=""):
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050027 r"""
28 Initialize robot output parms such as outputdir, output, etc.
29
30 This function will set global values for the following robot output parms.
31
Michael Walsh1f961b72020-06-16 15:54:11 -050032 outputdir, output, log, report, loglevel, consolecolors, consolemarkers
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050033
Michael Walsh410b1782019-10-22 15:56:18 -050034 This function would typically be called prior to calling create_robot_cmd_string.
Michael Walshf33140f2018-11-01 14:05:56 -050035
36 Description of argument(s):
Michael Walsh410b1782019-10-22 15:56:18 -050037 extra_prefix An extra prefix to be appended to the default prefix for output file
38 names.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050039 """
40
Michael Walsha0ce75a2018-07-31 13:54:29 -050041 gp.dprint_executing()
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050042 AUTOBOOT_OPENBMC_NICKNAME = gm.get_mod_global("AUTOBOOT_OPENBMC_NICKNAME")
43
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050044 # Set values for call to create_robot_cmd_string.
Michael Walsh410b1782019-10-22 15:56:18 -050045 # Environment variable TMP_ROBOT_DIR_PATH can be set by the user to indicate that robot-generated output
46 # should initially be written to the specified temporary directory and then moved to the normal output
Michael Walsha0ce75a2018-07-31 13:54:29 -050047 # location after completion.
Patrick Williams20f38712022-12-08 06:18:26 -060048 outputdir = os.environ.get(
49 "TMP_ROBOT_DIR_PATH",
50 os.environ.get(
51 "STATUS_DIR_PATH", os.environ.get("HOME", ".") + "/status"
52 ),
53 )
Michael Walsha0ce75a2018-07-31 13:54:29 -050054 outputdir = gm.add_trailing_slash(outputdir)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050055 seconds = time.time()
56 loc_time = time.localtime(seconds)
57 time_string = time.strftime("%y%m%d.%H%M%S", loc_time)
Patrick Williams20f38712022-12-08 06:18:26 -060058 file_prefix = (
59 AUTOBOOT_OPENBMC_NICKNAME + "." + extra_prefix + time_string + "."
60 )
Michael Walsh410b1782019-10-22 15:56:18 -050061 # Environment variable SAVE_STATUS_POLICY governs when robot-generated output files (e.g. the log.html)
62 # 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 -050063 SAVE_STATUS_POLICY = os.environ.get("SAVE_STATUS_POLICY", "ALWAYS")
64 if SAVE_STATUS_POLICY == "NEVER":
65 output = "NONE"
66 log = "NONE"
67 report = "NONE"
68 else:
69 output = file_prefix + "output.xml"
70 log = file_prefix + "log.html"
71 report = file_prefix + "report.html"
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050072 loglevel = "TRACE"
Patrick Williams20f38712022-12-08 06:18:26 -060073 consolecolors = "off"
74 consolemarkers = "off"
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050075
76 # Make create_robot_cmd_string values global.
77 gm.set_mod_global(outputdir)
78 gm.set_mod_global(output)
79 gm.set_mod_global(log)
80 gm.set_mod_global(report)
81 gm.set_mod_global(loglevel)
Michael Walsh1f961b72020-06-16 15:54:11 -050082 gm.set_mod_global(consolecolors)
83 gm.set_mod_global(consolemarkers)
84
Patrick Williams20f38712022-12-08 06:18:26 -060085 return (
86 outputdir,
87 output,
88 log,
89 report,
90 loglevel,
91 consolecolors,
92 consolemarkers,
93 )
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050094
95
96def init_robot_test_base_dir_path():
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050097 r"""
Michael Walsh410b1782019-10-22 15:56:18 -050098 Initialize and validate the environment variable, ROBOT_TEST_BASE_DIR_PATH and set corresponding global
99 variable ROBOT_TEST_RUNNING_FROM_SB.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500100
Michael Walsh410b1782019-10-22 15:56:18 -0500101 If ROBOT_TEST_BASE_DIR_PATH is already set, this function will merely validate it. This function will
102 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 -0500103 """
104
105 # ROBOT_TEST_BASE_DIR_PATH will be set as follows:
Michael Walsh410b1782019-10-22 15:56:18 -0500106 # This function will determine whether we are running in a user sandbox or from a standard apolloxxx
107 # environment.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500108 # - User sandbox:
Michael Walsh410b1782019-10-22 15:56:18 -0500109 # If there is a <developer's home dir>/git/openbmc-test-automation/, ROBOT_TEST_BASE_DIR_PATH will be
110 # set to that path. Otherwise, we set it to <program dir path>/git/openbmc-test-automation/
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500111 # - Not in user sandbox:
Michael Walsh410b1782019-10-22 15:56:18 -0500112 # ROBOT_TEST_BASE_DIR_PATH will be set to <program dir path>/git/openbmc-test-automation/
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500113
Patrick Williams20f38712022-12-08 06:18:26 -0600114 ROBOT_TEST_BASE_DIR_PATH = os.environ.get("ROBOT_TEST_BASE_DIR_PATH", "")
115 ROBOT_TEST_RUNNING_FROM_SB = int(
116 os.environ.get("ROBOT_TEST_RUNNING_FROM_SB", "0")
117 )
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500118 if ROBOT_TEST_BASE_DIR_PATH == "":
119 # ROBOT_TEST_BASE_DIR_PATH was not set by user/caller.
Patrick Williams20f38712022-12-08 06:18:26 -0600120 AUTOIPL_VERSION = os.environ.get("AUTOIPL_VERSION", "")
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500121 if AUTOIPL_VERSION == "":
122 ROBOT_TEST_BASE_DIR_PATH = base_path
123 else:
124 suffix = "git/openbmc-test-automation/"
125
Michael Walsh410b1782019-10-22 15:56:18 -0500126 # Determine whether we're running out of a developer sandbox or simply out of an apolloxxx/bin
127 # path.
Patrick Williams20f38712022-12-08 06:18:26 -0600128 shell_rc, out_buf = gc.shell_cmd(
129 "dirname $(which gen_print.py)",
130 quiet=(not debug),
131 print_output=0,
132 )
Michael Walshbffaa1d2018-06-08 15:09:27 -0500133 executable_base_dir_path = os.path.realpath(out_buf.rstrip()) + "/"
Patrick Williams20f38712022-12-08 06:18:26 -0600134 apollo_dir_path = (
135 os.environ["AUTO_BASE_PATH"] + AUTOIPL_VERSION + "/bin/"
136 )
137 developer_home_dir_path = re.sub(
138 "/sandbox.*", "", executable_base_dir_path
139 )
140 developer_home_dir_path = gm.add_trailing_slash(
141 developer_home_dir_path
142 )
143 gp.dprint_vars(
144 executable_base_dir_path,
145 developer_home_dir_path,
146 apollo_dir_path,
147 )
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500148
149 ROBOT_TEST_RUNNING_FROM_SB = 0
150 if executable_base_dir_path != apollo_dir_path:
151 ROBOT_TEST_RUNNING_FROM_SB = 1
152 gp.dprint_vars(ROBOT_TEST_RUNNING_FROM_SB)
153 ROBOT_TEST_BASE_DIR_PATH = developer_home_dir_path + suffix
154 if not os.path.isdir(ROBOT_TEST_BASE_DIR_PATH):
Patrick Williams20f38712022-12-08 06:18:26 -0600155 gp.dprint_timen(
156 "NOTE: Sandbox directory "
157 + ROBOT_TEST_BASE_DIR_PATH
158 + " does not"
159 + " exist."
160 )
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500161 # Fall back to the apollo dir path.
162 ROBOT_TEST_BASE_DIR_PATH = apollo_dir_path + suffix
163 else:
164 # Use to the apollo dir path.
165 ROBOT_TEST_BASE_DIR_PATH = apollo_dir_path + suffix
166
Michael Walsh2ea965c2019-08-01 16:14:25 -0500167 gv.valid_value(ROBOT_TEST_BASE_DIR_PATH)
Patrick Williams20f38712022-12-08 06:18:26 -0600168 gp.dprint_vars(
169 ROBOT_TEST_RUNNING_FROM_SB,
170 ROBOT_TEST_BASE_DIR_PATH,
Patrick Williams20f38712022-12-08 06:18:26 -0600171 )
Michael Walsh2ea965c2019-08-01 16:14:25 -0500172 gv.valid_dir_path(ROBOT_TEST_BASE_DIR_PATH)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500173
174 ROBOT_TEST_BASE_DIR_PATH = gm.add_trailing_slash(ROBOT_TEST_BASE_DIR_PATH)
175 gm.set_mod_global(ROBOT_TEST_BASE_DIR_PATH)
Patrick Williams20f38712022-12-08 06:18:26 -0600176 os.environ["ROBOT_TEST_BASE_DIR_PATH"] = ROBOT_TEST_BASE_DIR_PATH
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500177
178 gm.set_mod_global(ROBOT_TEST_RUNNING_FROM_SB)
Patrick Williams20f38712022-12-08 06:18:26 -0600179 os.environ["ROBOT_TEST_RUNNING_FROM_SB"] = str(ROBOT_TEST_RUNNING_FROM_SB)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500180
181
Patrick Williams20f38712022-12-08 06:18:26 -0600182raw_robot_file_search_path = (
183 "${ROBOT_TEST_BASE_DIR_PATH}:"
184 + "${ROBOT_TEST_BASE_DIR_PATH}tests:${ROBOT_TEST_BASE_DIR_PATH}extended:"
185 + "${ROBOT_TEST_BASE_DIR_PATH}scratch:${PATH}"
186)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500187
188
189def init_robot_file_path(robot_file_path):
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500190 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500191 Determine full path name for the file path passed in robot_file_path and return it.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500192
Michael Walsh410b1782019-10-22 15:56:18 -0500193 If robot_file_path contains a fully qualified path name, this function will verify that the file exists.
194 If robot_file_path contains a relative path, this function will search for the file and set
195 robot_file_path so that it contains the absolute path to the robot file. This function will search for
196 the robot file using the raw_robot_file_search_path (defined above). Note that if
197 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 -0500198
199 Description of arguments:
Michael Walsh410b1782019-10-22 15:56:18 -0500200 robot_file_path The absolute or relative path to a robot file.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500201 """
202
Michael Walsh2ea965c2019-08-01 16:14:25 -0500203 gv.valid_value(robot_file_path)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500204
205 try:
206 if ROBOT_TEST_BASE_DIR_PATH is NONE:
207 init_robot_test_base_dir_path()
208 except NameError:
209 init_robot_test_base_dir_path()
210
211 if not re.match(r".*\.(robot|py)$", robot_file_path):
212 # No suffix so we'll assign one of "\.robot".
213 robot_file_path = robot_file_path + ".robot"
214
215 abs_path = 0
216 if robot_file_path[0:1] == "/":
217 abs_path = 1
218
219 gp.dprint_vars(abs_path, robot_file_path)
220
221 if not abs_path:
Patrick Williams20f38712022-12-08 06:18:26 -0600222 cmd_buf = 'echo -n "' + raw_robot_file_search_path + '"'
223 shell_rc, out_buf = gc.shell_cmd(
224 cmd_buf, quiet=(not debug), print_output=0
225 )
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500226 robot_file_search_paths = out_buf
Michael Walsha0ce75a2018-07-31 13:54:29 -0500227 gp.dprint_var(robot_file_search_paths)
Patrick Williams20f38712022-12-08 06:18:26 -0600228 robot_file_search_paths_list = robot_file_search_paths.split(":")
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500229 for search_path in robot_file_search_paths_list:
230 search_path = gm.add_trailing_slash(search_path)
231 candidate_file_path = search_path + robot_file_path
232 gp.dprint_var(candidate_file_path)
233 if os.path.isfile(candidate_file_path):
234 gp.dprint_timen("Found full path to " + robot_file_path + ".")
235 robot_file_path = candidate_file_path
236 break
237
238 gp.dprint_var(robot_file_path)
Michael Walsh2ea965c2019-08-01 16:14:25 -0500239 gv.valid_file_path(robot_file_path)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500240
241 return robot_file_path
242
243
244def get_robot_parm_names():
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500245 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500246 Return a list containing all of the long parm names (e.g. --outputdir) supported by the robot program.
247 Double dashes are not included in the names returned.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500248 """
249
Patrick Williams20f38712022-12-08 06:18:26 -0600250 cmd_buf = (
251 "robot -h | egrep "
252 + "'^([ ]\\-[a-zA-Z0-9])?[ ]+--[a-zA-Z0-9]+[ ]+' | sed -re"
253 + " s'/.*\\-\\-//g' -e s'/ .*//g' | sort -u"
254 )
Michael Walshbffaa1d2018-06-08 15:09:27 -0500255 shell_rc, out_buf = gc.shell_cmd(cmd_buf, quiet=1, print_output=0)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500256
257 return out_buf.split("\n")
258
259
260def create_robot_cmd_string(robot_file_path, *parms):
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500261 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500262 Create a robot command string and return it. On failure, return an empty string.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500263
264 Description of arguments:
265 robot_file_path The path to the robot file to be run.
Michael Walsh410b1782019-10-22 15:56:18 -0500266 parms The list of parms to be included in the command string. The name of each
267 variable in this list must be the same as the name of the corresponding
268 parm. This function figures out that name. This function is also able
269 to distinguish robot parms (e.g. --outputdir) from robot program parms
270 (all other parms which will be passed as "-v PARM_NAME:parm_value")..
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500271
272 Example:
273
274 The following call to this function...
Michael Walsh410b1782019-10-22 15:56:18 -0500275 cmd_buf = create_robot_cmd_string("tools/start_sol_console.robot", OPENBMC_HOST, quiet, test_mode, debug,
276 outputdir, output, log, report)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500277
278 Would return a string something like this.
279 robot -v OPENBMC_HOST:beye6 -v quiet:0 -v test_mode:1 -v debug:1
Michael Walsh410b1782019-10-22 15:56:18 -0500280 --outputdir=/gsa/ausgsa/projects/a/status --output=beye6.OS_Console.output.xml
281 --log=beye6.OS_Console.log.html --report=beye6.OS_Console.report.html tools/start_sol_console.robot
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500282 """
283
284 robot_file_path = init_robot_file_path(robot_file_path)
285
286 robot_parm_names = get_robot_parm_names()
287
288 robot_parm_list = []
289
290 stack_frame = 2
291 ix = 2
292 for arg in parms:
293 parm = arg
294 parm = gm.quote_bash_parm(gm.escape_bash_quotes(str(parm)))
295 var_name = gp.get_arg_name(None, ix, stack_frame)
296 if var_name in robot_parm_names:
297 p_string = "--" + var_name + "=" + str(parm)
298 robot_parm_list.append(p_string)
299 else:
300 p_string = "-v " + var_name + ":" + str(parm)
301 robot_parm_list.append(p_string)
302 ix += 1
303
Patrick Williams20f38712022-12-08 06:18:26 -0600304 robot_cmd_buf = (
305 "robot " + " ".join(robot_parm_list) + " " + robot_file_path
306 )
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500307
308 return robot_cmd_buf
309
310
Michael Walsha0ce75a2018-07-31 13:54:29 -0500311# Global variables to aid in cleanup after running robot_cmd_fnc.
312gcr_last_robot_cmd_buf = ""
313gcr_last_robot_rc = 0
314
315
Patrick Williams20f38712022-12-08 06:18:26 -0600316def process_robot_output_files(robot_cmd_buf=None, robot_rc=None, gzip=None):
Michael Walsha0ce75a2018-07-31 13:54:29 -0500317 r"""
318 Process robot output files which can involve several operations:
Michael Walsh410b1782019-10-22 15:56:18 -0500319 - If the files are in a temporary location, using SAVE_STATUS_POLICY to decide whether to move them to a
320 permanent location or to delete them.
Michael Walsha0ce75a2018-07-31 13:54:29 -0500321 - Gzipping them.
322
323 Description of argument(s):
Michael Walsh410b1782019-10-22 15:56:18 -0500324 robot_cmd_buf The complete command string used to invoke robot.
325 robot_rc The return code from running the robot command string.
326 gzip Indicates whether robot-generated output should be gzipped.
Michael Walsha0ce75a2018-07-31 13:54:29 -0500327 """
328
329 robot_cmd_buf = gm.dft(robot_cmd_buf, gcr_last_robot_cmd_buf)
330 robot_rc = gm.dft(robot_rc, gcr_last_robot_rc)
Michael Walshf33140f2018-11-01 14:05:56 -0500331 gzip = gm.dft(gzip, int(os.environ.get("GZIP_ROBOT", "1")))
Michael Walsha0ce75a2018-07-31 13:54:29 -0500332
333 if robot_cmd_buf == "":
Michael Walsh410b1782019-10-22 15:56:18 -0500334 # This can legitimately occur if this function is called from an exit_function without the program
335 # having ever run robot_cmd_fnc.
Michael Walsha0ce75a2018-07-31 13:54:29 -0500336 return
337
338 SAVE_STATUS_POLICY = os.environ.get("SAVE_STATUS_POLICY", "ALWAYS")
339 gp.qprint_vars(SAVE_STATUS_POLICY)
340
Michael Walsh410b1782019-10-22 15:56:18 -0500341 # When SAVE_STATUS_POLICY is "NEVER" robot output files don't even get generated.
Michael Walsha0ce75a2018-07-31 13:54:29 -0500342 if SAVE_STATUS_POLICY == "NEVER":
343 return
344
345 # Compose file_list based on robot command buffer passed in.
346 robot_cmd_buf_dict = gc.parse_command_string(robot_cmd_buf)
Patrick Williams20f38712022-12-08 06:18:26 -0600347 outputdir = robot_cmd_buf_dict["outputdir"]
Michael Walsha0ce75a2018-07-31 13:54:29 -0500348 outputdir = gm.add_trailing_slash(outputdir)
Patrick Williams20f38712022-12-08 06:18:26 -0600349 file_list = (
350 outputdir
351 + robot_cmd_buf_dict["output"]
352 + " "
353 + outputdir
354 + robot_cmd_buf_dict["log"]
355 + " "
356 + outputdir
357 + robot_cmd_buf_dict["report"]
358 )
Michael Walsha0ce75a2018-07-31 13:54:29 -0500359
360 # Double checking that files are present.
Patrick Williams20f38712022-12-08 06:18:26 -0600361 shell_rc, out_buf = gc.shell_cmd(
362 "ls -1 " + file_list + " 2>/dev/null", show_err=0
363 )
Michael Walsha0ce75a2018-07-31 13:54:29 -0500364 file_list = re.sub("\n", " ", out_buf.rstrip("\n"))
365
366 if file_list == "":
Patrick Williams20f38712022-12-08 06:18:26 -0600367 gp.qprint_timen(
368 "No robot output files were found in " + outputdir + "."
369 )
Michael Walsha0ce75a2018-07-31 13:54:29 -0500370 return
Michael Walsh0d5f96a2019-05-20 10:09:57 -0500371 gp.qprint_var(robot_rc, gp.hexa())
Michael Walsha0ce75a2018-07-31 13:54:29 -0500372 if SAVE_STATUS_POLICY == "FAIL" and robot_rc == 0:
Patrick Williams20f38712022-12-08 06:18:26 -0600373 gp.qprint_timen(
374 "The call to robot produced no failures."
375 + " Deleting robot output files."
376 )
Michael Walsha0ce75a2018-07-31 13:54:29 -0500377 gc.shell_cmd("rm -rf " + file_list)
378 return
379
380 if gzip:
Michael Walshe53dfec2018-08-07 15:02:56 -0500381 gc.shell_cmd("gzip -f " + file_list)
Michael Walsha0ce75a2018-07-31 13:54:29 -0500382 # Update the values in file_list.
383 file_list = re.sub(" ", ".gz ", file_list) + ".gz"
384
Michael Walsh410b1782019-10-22 15:56:18 -0500385 # It TMP_ROBOT_DIR_PATH is set, it means the caller wanted the robot output initially directed to
386 # TMP_ROBOT_DIR_PATH but later moved to FFDC_DIR_PATH. Otherwise, we're done.
Michael Walsha0ce75a2018-07-31 13:54:29 -0500387
George Keishinge7e91712021-09-03 11:28:44 -0500388 if os.environ.get("TMP_ROBOT_DIR_PATH", "") == "":
Michael Walsha0ce75a2018-07-31 13:54:29 -0500389 return
390
Michael Walsh410b1782019-10-22 15:56:18 -0500391 # We're directing these to the FFDC dir path so that they'll be subjected to FFDC cleanup.
Patrick Williams20f38712022-12-08 06:18:26 -0600392 target_dir_path = os.environ.get(
393 "FFDC_DIR_PATH", os.environ.get("HOME", ".") + "/ffdc"
394 )
Michael Walsha0ce75a2018-07-31 13:54:29 -0500395 target_dir_path = gm.add_trailing_slash(target_dir_path)
396
Patrick Williams20f38712022-12-08 06:18:26 -0600397 targ_file_list = [
398 re.sub(".*/", target_dir_path, x) for x in file_list.split(" ")
399 ]
Michael Walsha0ce75a2018-07-31 13:54:29 -0500400
Patrick Williams20f38712022-12-08 06:18:26 -0600401 gc.shell_cmd(
402 "mv " + file_list + " " + target_dir_path + " >/dev/null", time_out=600
403 )
Michael Walsha0ce75a2018-07-31 13:54:29 -0500404
405 gp.qprint_timen("New robot log file locations:")
Patrick Williams20f38712022-12-08 06:18:26 -0600406 gp.qprintn("\n".join(targ_file_list))
Michael Walsha0ce75a2018-07-31 13:54:29 -0500407
408
Patrick Williams20f38712022-12-08 06:18:26 -0600409def robot_cmd_fnc(
410 robot_cmd_buf,
411 robot_jail=os.environ.get("ROBOT_JAIL", ""),
412 quiet=None,
413 test_mode=0,
414):
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500415 r"""
416 Run the robot command string.
417
Michael Walsh410b1782019-10-22 15:56:18 -0500418 This function will set the various PATH variables correctly so that you are running the proper version of
419 all imported files, etc.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500420
421 Description of argument(s):
422 robot_cmd_buf The complete robot command string.
Michael Walsh410b1782019-10-22 15:56:18 -0500423 robot_jail Indicates that this is to run in "robot jail" meaning without visibility
424 to any apolloxxx import files, programs, etc.
Michael Walsh8dc99a32020-04-02 13:45:13 -0500425 test_mode If test_mode is set, this function will not actually run the command.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500426 """
427
Patrick Williams20f38712022-12-08 06:18:26 -0600428 quiet = int(gm.dft(quiet, gp.get_stack_var("quiet", 0)))
Michael Walsh2ea965c2019-08-01 16:14:25 -0500429 gv.valid_value(robot_cmd_buf)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500430
Michael Walsha0ce75a2018-07-31 13:54:29 -0500431 # Set global variables to aid in cleanup with process_robot_output_files.
432 global gcr_last_robot_cmd_buf
433 global gcr_last_robot_rc
434 gcr_last_robot_cmd_buf = robot_cmd_buf
435
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500436 # Get globals set by init_robot_test_base_dir_path().
437 module = sys.modules["__main__"]
438 try:
439 ROBOT_TEST_BASE_DIR_PATH = getattr(module, "ROBOT_TEST_BASE_DIR_PATH")
440 except NameError:
441 init_robot_test_base_dir_path()
442 ROBOT_TEST_BASE_DIR_PATH = getattr(module, "ROBOT_TEST_BASE_DIR_PATH")
443
Patrick Williams20f38712022-12-08 06:18:26 -0600444 ROBOT_TEST_RUNNING_FROM_SB = gm.get_mod_global(
445 "ROBOT_TEST_RUNNING_FROM_SB"
446 )
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500447
448 if robot_jail == "":
449 if ROBOT_TEST_RUNNING_FROM_SB:
450 robot_jail = 0
451 else:
452 robot_jail = 1
453
454 robot_jail = int(robot_jail)
Patrick Williams20f38712022-12-08 06:18:26 -0600455 ROBOT_JAIL = os.environ.get("ROBOT_JAIL", "")
456 gp.dprint_vars(
457 ROBOT_TEST_BASE_DIR_PATH,
458 ROBOT_TEST_RUNNING_FROM_SB,
459 ROBOT_JAIL,
460 robot_jail,
461 )
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500462
463 # Save PATH and PYTHONPATH to be restored later.
464 os.environ["SAVED_PYTHONPATH"] = os.environ.get("PYTHONPATH", "")
465 os.environ["SAVED_PATH"] = os.environ.get("PATH", "")
466
467 if robot_jail:
Michael Walsh410b1782019-10-22 15:56:18 -0500468 # Make sure required programs like python and robot can be found in the new restricted PATH.
Michael Walsha5d29bc2019-06-05 15:29:18 -0500469 required_programs = "python robot"
Michael Walsh410b1782019-10-22 15:56:18 -0500470 # It is expected that there will be a "python" program in the tool base bin path which is really a
471 # link to select_version. Ditto for "robot". Call each with the --print_only option to get the
472 # paths to the "real" programs.
Patrick Williams20f38712022-12-08 06:18:26 -0600473 cmd_buf = (
474 "for program in "
475 + required_programs
Michael Walsha5d29bc2019-06-05 15:29:18 -0500476 + " ; do dirname $(${program} --print_only) ; done 2>/dev/null"
Patrick Williams20f38712022-12-08 06:18:26 -0600477 )
Michael Walsha5d29bc2019-06-05 15:29:18 -0500478 rc, out_buf = gc.shell_cmd(cmd_buf, quiet=1, print_output=0)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500479 PYTHONPATH = ROBOT_TEST_BASE_DIR_PATH + "lib"
480 NEW_PATH_LIST = [ROBOT_TEST_BASE_DIR_PATH + "bin"]
Michael Walsha5d29bc2019-06-05 15:29:18 -0500481 NEW_PATH_LIST.extend(list(set(out_buf.rstrip("\n").split("\n"))))
Patrick Williams20f38712022-12-08 06:18:26 -0600482 NEW_PATH_LIST.extend(
483 [
484 "/usr/local/sbin",
485 "/usr/local/bin",
486 "/usr/sbin",
487 "/usr/bin",
488 "/sbin",
489 "/bin",
Patrick Williams20f38712022-12-08 06:18:26 -0600490 ]
491 )
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500492 PATH = ":".join(NEW_PATH_LIST)
493 else:
Patrick Williams20f38712022-12-08 06:18:26 -0600494 PYTHONPATH = (
495 os.environ.get("PYTHONPATH", "")
496 + ":"
497 + ROBOT_TEST_BASE_DIR_PATH
498 + "lib"
499 )
500 PATH = (
George Keishing12de0942023-12-08 17:02:13 +0530501 os.environ.get("PATH", "") + ":" + ROBOT_TEST_BASE_DIR_PATH + "bin"
Patrick Williams20f38712022-12-08 06:18:26 -0600502 )
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500503
Patrick Williams20f38712022-12-08 06:18:26 -0600504 os.environ["PYTHONPATH"] = PYTHONPATH
505 os.environ["PATH"] = PATH
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500506 gp.dprint_vars(PATH, PYTHONPATH)
507
Patrick Williams20f38712022-12-08 06:18:26 -0600508 os.environ["FFDC_DIR_PATH_STYLE"] = os.environ.get(
509 "FFDC_DIR_PATH_STYLE", "1"
510 )
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500511 gp.qpissuing(robot_cmd_buf, test_mode)
512 if test_mode:
513 os.environ["PATH"] = os.environ.get("SAVED_PATH", "")
514 os.environ["PYTHONPATH"] = os.environ.get("SAVED_PYTHONPATH", "")
515 return True
516
517 if quiet:
Patrick Williams20f38712022-12-08 06:18:26 -0600518 DEVNULL = open(os.devnull, "wb")
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500519 stdout = DEVNULL
520 else:
521 stdout = None
522 sub_proc = subprocess.Popen(robot_cmd_buf, stdout=stdout, shell=True)
523 sub_proc.communicate()
524 shell_rc = sub_proc.returncode
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500525 os.environ["PATH"] = os.environ.get("SAVED_PATH", "")
526 os.environ["PYTHONPATH"] = os.environ.get("SAVED_PYTHONPATH", "")
Michael Walsha0ce75a2018-07-31 13:54:29 -0500527 gcr_last_robot_rc = shell_rc
528 process_robot_output_files()
529 if shell_rc != 0:
Michael Walsh0d5f96a2019-05-20 10:09:57 -0500530 gp.print_var(shell_rc, gp.hexa())
Michael Walsha0ce75a2018-07-31 13:54:29 -0500531 return False
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500532
533 return True