blob: 4a8e2b0386495058553c08c3e4e0aac2c0d49e88 [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
Brian Ma139f1da2024-10-18 13:34:14 +08008import importlib.util
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 = (
Brian Ma139f1da2024-10-18 13:34:14 +080021 os.path.dirname(
22 os.path.dirname(importlib.util.find_spec("gen_robot_print").origin)
23 )
Patrick Williams20f38712022-12-08 06:18:26 -060024 + os.sep
25)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050026
27
28def init_robot_out_parms(extra_prefix=""):
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050029 r"""
30 Initialize robot output parms such as outputdir, output, etc.
31
32 This function will set global values for the following robot output parms.
33
Michael Walsh1f961b72020-06-16 15:54:11 -050034 outputdir, output, log, report, loglevel, consolecolors, consolemarkers
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050035
Michael Walsh410b1782019-10-22 15:56:18 -050036 This function would typically be called prior to calling create_robot_cmd_string.
Michael Walshf33140f2018-11-01 14:05:56 -050037
38 Description of argument(s):
Michael Walsh410b1782019-10-22 15:56:18 -050039 extra_prefix An extra prefix to be appended to the default prefix for output file
40 names.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050041 """
42
Michael Walsha0ce75a2018-07-31 13:54:29 -050043 gp.dprint_executing()
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050044 AUTOBOOT_OPENBMC_NICKNAME = gm.get_mod_global("AUTOBOOT_OPENBMC_NICKNAME")
45
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050046 # Set values for call to create_robot_cmd_string.
Michael Walsh410b1782019-10-22 15:56:18 -050047 # Environment variable TMP_ROBOT_DIR_PATH can be set by the user to indicate that robot-generated output
48 # should initially be written to the specified temporary directory and then moved to the normal output
Michael Walsha0ce75a2018-07-31 13:54:29 -050049 # location after completion.
Patrick Williams20f38712022-12-08 06:18:26 -060050 outputdir = os.environ.get(
51 "TMP_ROBOT_DIR_PATH",
52 os.environ.get(
53 "STATUS_DIR_PATH", os.environ.get("HOME", ".") + "/status"
54 ),
55 )
Michael Walsha0ce75a2018-07-31 13:54:29 -050056 outputdir = gm.add_trailing_slash(outputdir)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050057 seconds = time.time()
58 loc_time = time.localtime(seconds)
59 time_string = time.strftime("%y%m%d.%H%M%S", loc_time)
Patrick Williams20f38712022-12-08 06:18:26 -060060 file_prefix = (
61 AUTOBOOT_OPENBMC_NICKNAME + "." + extra_prefix + time_string + "."
62 )
Michael Walsh410b1782019-10-22 15:56:18 -050063 # Environment variable SAVE_STATUS_POLICY governs when robot-generated output files (e.g. the log.html)
64 # 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 -050065 SAVE_STATUS_POLICY = os.environ.get("SAVE_STATUS_POLICY", "ALWAYS")
66 if SAVE_STATUS_POLICY == "NEVER":
67 output = "NONE"
68 log = "NONE"
69 report = "NONE"
70 else:
71 output = file_prefix + "output.xml"
72 log = file_prefix + "log.html"
73 report = file_prefix + "report.html"
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050074 loglevel = "TRACE"
Patrick Williams20f38712022-12-08 06:18:26 -060075 consolecolors = "off"
76 consolemarkers = "off"
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050077
78 # Make create_robot_cmd_string values global.
79 gm.set_mod_global(outputdir)
80 gm.set_mod_global(output)
81 gm.set_mod_global(log)
82 gm.set_mod_global(report)
83 gm.set_mod_global(loglevel)
Michael Walsh1f961b72020-06-16 15:54:11 -050084 gm.set_mod_global(consolecolors)
85 gm.set_mod_global(consolemarkers)
86
Patrick Williams20f38712022-12-08 06:18:26 -060087 return (
88 outputdir,
89 output,
90 log,
91 report,
92 loglevel,
93 consolecolors,
94 consolemarkers,
95 )
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050096
97
98def init_robot_test_base_dir_path():
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050099 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500100 Initialize and validate the environment variable, ROBOT_TEST_BASE_DIR_PATH and set corresponding global
101 variable ROBOT_TEST_RUNNING_FROM_SB.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500102
Michael Walsh410b1782019-10-22 15:56:18 -0500103 If ROBOT_TEST_BASE_DIR_PATH is already set, this function will merely validate it. This function will
104 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 -0500105 """
106
107 # ROBOT_TEST_BASE_DIR_PATH will be set as follows:
Michael Walsh410b1782019-10-22 15:56:18 -0500108 # This function will determine whether we are running in a user sandbox or from a standard apolloxxx
109 # environment.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500110 # - User sandbox:
Michael Walsh410b1782019-10-22 15:56:18 -0500111 # If there is a <developer's home dir>/git/openbmc-test-automation/, ROBOT_TEST_BASE_DIR_PATH will be
112 # set to that path. Otherwise, we set it to <program dir path>/git/openbmc-test-automation/
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500113 # - Not in user sandbox:
Michael Walsh410b1782019-10-22 15:56:18 -0500114 # ROBOT_TEST_BASE_DIR_PATH will be set to <program dir path>/git/openbmc-test-automation/
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500115
Patrick Williams20f38712022-12-08 06:18:26 -0600116 ROBOT_TEST_BASE_DIR_PATH = os.environ.get("ROBOT_TEST_BASE_DIR_PATH", "")
117 ROBOT_TEST_RUNNING_FROM_SB = int(
118 os.environ.get("ROBOT_TEST_RUNNING_FROM_SB", "0")
119 )
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500120 if ROBOT_TEST_BASE_DIR_PATH == "":
121 # ROBOT_TEST_BASE_DIR_PATH was not set by user/caller.
Patrick Williams20f38712022-12-08 06:18:26 -0600122 AUTOIPL_VERSION = os.environ.get("AUTOIPL_VERSION", "")
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500123 if AUTOIPL_VERSION == "":
124 ROBOT_TEST_BASE_DIR_PATH = base_path
125 else:
126 suffix = "git/openbmc-test-automation/"
127
Michael Walsh410b1782019-10-22 15:56:18 -0500128 # Determine whether we're running out of a developer sandbox or simply out of an apolloxxx/bin
129 # path.
Patrick Williams20f38712022-12-08 06:18:26 -0600130 shell_rc, out_buf = gc.shell_cmd(
131 "dirname $(which gen_print.py)",
132 quiet=(not debug),
133 print_output=0,
134 )
Michael Walshbffaa1d2018-06-08 15:09:27 -0500135 executable_base_dir_path = os.path.realpath(out_buf.rstrip()) + "/"
Patrick Williams20f38712022-12-08 06:18:26 -0600136 apollo_dir_path = (
137 os.environ["AUTO_BASE_PATH"] + AUTOIPL_VERSION + "/bin/"
138 )
139 developer_home_dir_path = re.sub(
140 "/sandbox.*", "", executable_base_dir_path
141 )
142 developer_home_dir_path = gm.add_trailing_slash(
143 developer_home_dir_path
144 )
145 gp.dprint_vars(
146 executable_base_dir_path,
147 developer_home_dir_path,
148 apollo_dir_path,
149 )
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500150
151 ROBOT_TEST_RUNNING_FROM_SB = 0
152 if executable_base_dir_path != apollo_dir_path:
153 ROBOT_TEST_RUNNING_FROM_SB = 1
154 gp.dprint_vars(ROBOT_TEST_RUNNING_FROM_SB)
155 ROBOT_TEST_BASE_DIR_PATH = developer_home_dir_path + suffix
156 if not os.path.isdir(ROBOT_TEST_BASE_DIR_PATH):
Patrick Williams20f38712022-12-08 06:18:26 -0600157 gp.dprint_timen(
158 "NOTE: Sandbox directory "
159 + ROBOT_TEST_BASE_DIR_PATH
160 + " does not"
161 + " exist."
162 )
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500163 # Fall back to the apollo dir path.
164 ROBOT_TEST_BASE_DIR_PATH = apollo_dir_path + suffix
165 else:
166 # Use to the apollo dir path.
167 ROBOT_TEST_BASE_DIR_PATH = apollo_dir_path + suffix
168
Michael Walsh2ea965c2019-08-01 16:14:25 -0500169 gv.valid_value(ROBOT_TEST_BASE_DIR_PATH)
Patrick Williams20f38712022-12-08 06:18:26 -0600170 gp.dprint_vars(
171 ROBOT_TEST_RUNNING_FROM_SB,
172 ROBOT_TEST_BASE_DIR_PATH,
Patrick Williams20f38712022-12-08 06:18:26 -0600173 )
Michael Walsh2ea965c2019-08-01 16:14:25 -0500174 gv.valid_dir_path(ROBOT_TEST_BASE_DIR_PATH)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500175
176 ROBOT_TEST_BASE_DIR_PATH = gm.add_trailing_slash(ROBOT_TEST_BASE_DIR_PATH)
177 gm.set_mod_global(ROBOT_TEST_BASE_DIR_PATH)
Patrick Williams20f38712022-12-08 06:18:26 -0600178 os.environ["ROBOT_TEST_BASE_DIR_PATH"] = ROBOT_TEST_BASE_DIR_PATH
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500179
180 gm.set_mod_global(ROBOT_TEST_RUNNING_FROM_SB)
Patrick Williams20f38712022-12-08 06:18:26 -0600181 os.environ["ROBOT_TEST_RUNNING_FROM_SB"] = str(ROBOT_TEST_RUNNING_FROM_SB)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500182
183
Patrick Williams20f38712022-12-08 06:18:26 -0600184raw_robot_file_search_path = (
185 "${ROBOT_TEST_BASE_DIR_PATH}:"
186 + "${ROBOT_TEST_BASE_DIR_PATH}tests:${ROBOT_TEST_BASE_DIR_PATH}extended:"
187 + "${ROBOT_TEST_BASE_DIR_PATH}scratch:${PATH}"
188)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500189
190
191def init_robot_file_path(robot_file_path):
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500192 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500193 Determine full path name for the file path passed in robot_file_path and return it.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500194
Michael Walsh410b1782019-10-22 15:56:18 -0500195 If robot_file_path contains a fully qualified path name, this function will verify that the file exists.
196 If robot_file_path contains a relative path, this function will search for the file and set
197 robot_file_path so that it contains the absolute path to the robot file. This function will search for
198 the robot file using the raw_robot_file_search_path (defined above). Note that if
199 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 -0500200
201 Description of arguments:
Michael Walsh410b1782019-10-22 15:56:18 -0500202 robot_file_path The absolute or relative path to a robot file.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500203 """
204
Michael Walsh2ea965c2019-08-01 16:14:25 -0500205 gv.valid_value(robot_file_path)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500206
207 try:
208 if ROBOT_TEST_BASE_DIR_PATH is NONE:
209 init_robot_test_base_dir_path()
210 except NameError:
211 init_robot_test_base_dir_path()
212
213 if not re.match(r".*\.(robot|py)$", robot_file_path):
214 # No suffix so we'll assign one of "\.robot".
215 robot_file_path = robot_file_path + ".robot"
216
217 abs_path = 0
218 if robot_file_path[0:1] == "/":
219 abs_path = 1
220
221 gp.dprint_vars(abs_path, robot_file_path)
222
223 if not abs_path:
Patrick Williams20f38712022-12-08 06:18:26 -0600224 cmd_buf = 'echo -n "' + raw_robot_file_search_path + '"'
225 shell_rc, out_buf = gc.shell_cmd(
226 cmd_buf, quiet=(not debug), print_output=0
227 )
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500228 robot_file_search_paths = out_buf
Michael Walsha0ce75a2018-07-31 13:54:29 -0500229 gp.dprint_var(robot_file_search_paths)
Patrick Williams20f38712022-12-08 06:18:26 -0600230 robot_file_search_paths_list = robot_file_search_paths.split(":")
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500231 for search_path in robot_file_search_paths_list:
232 search_path = gm.add_trailing_slash(search_path)
233 candidate_file_path = search_path + robot_file_path
234 gp.dprint_var(candidate_file_path)
235 if os.path.isfile(candidate_file_path):
236 gp.dprint_timen("Found full path to " + robot_file_path + ".")
237 robot_file_path = candidate_file_path
238 break
239
240 gp.dprint_var(robot_file_path)
Michael Walsh2ea965c2019-08-01 16:14:25 -0500241 gv.valid_file_path(robot_file_path)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500242
243 return robot_file_path
244
245
246def get_robot_parm_names():
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500247 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500248 Return a list containing all of the long parm names (e.g. --outputdir) supported by the robot program.
249 Double dashes are not included in the names returned.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500250 """
251
Patrick Williams20f38712022-12-08 06:18:26 -0600252 cmd_buf = (
253 "robot -h | egrep "
254 + "'^([ ]\\-[a-zA-Z0-9])?[ ]+--[a-zA-Z0-9]+[ ]+' | sed -re"
255 + " s'/.*\\-\\-//g' -e s'/ .*//g' | sort -u"
256 )
Michael Walshbffaa1d2018-06-08 15:09:27 -0500257 shell_rc, out_buf = gc.shell_cmd(cmd_buf, quiet=1, print_output=0)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500258
259 return out_buf.split("\n")
260
261
262def create_robot_cmd_string(robot_file_path, *parms):
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500263 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500264 Create a robot command string and return it. On failure, return an empty string.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500265
266 Description of arguments:
267 robot_file_path The path to the robot file to be run.
Michael Walsh410b1782019-10-22 15:56:18 -0500268 parms The list of parms to be included in the command string. The name of each
269 variable in this list must be the same as the name of the corresponding
270 parm. This function figures out that name. This function is also able
271 to distinguish robot parms (e.g. --outputdir) from robot program parms
272 (all other parms which will be passed as "-v PARM_NAME:parm_value")..
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500273
274 Example:
275
276 The following call to this function...
Michael Walsh410b1782019-10-22 15:56:18 -0500277 cmd_buf = create_robot_cmd_string("tools/start_sol_console.robot", OPENBMC_HOST, quiet, test_mode, debug,
278 outputdir, output, log, report)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500279
280 Would return a string something like this.
281 robot -v OPENBMC_HOST:beye6 -v quiet:0 -v test_mode:1 -v debug:1
Michael Walsh410b1782019-10-22 15:56:18 -0500282 --outputdir=/gsa/ausgsa/projects/a/status --output=beye6.OS_Console.output.xml
283 --log=beye6.OS_Console.log.html --report=beye6.OS_Console.report.html tools/start_sol_console.robot
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500284 """
285
286 robot_file_path = init_robot_file_path(robot_file_path)
287
288 robot_parm_names = get_robot_parm_names()
289
290 robot_parm_list = []
291
292 stack_frame = 2
293 ix = 2
294 for arg in parms:
295 parm = arg
296 parm = gm.quote_bash_parm(gm.escape_bash_quotes(str(parm)))
297 var_name = gp.get_arg_name(None, ix, stack_frame)
298 if var_name in robot_parm_names:
299 p_string = "--" + var_name + "=" + str(parm)
300 robot_parm_list.append(p_string)
301 else:
302 p_string = "-v " + var_name + ":" + str(parm)
303 robot_parm_list.append(p_string)
304 ix += 1
305
Patrick Williams20f38712022-12-08 06:18:26 -0600306 robot_cmd_buf = (
307 "robot " + " ".join(robot_parm_list) + " " + robot_file_path
308 )
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500309
310 return robot_cmd_buf
311
312
Michael Walsha0ce75a2018-07-31 13:54:29 -0500313# Global variables to aid in cleanup after running robot_cmd_fnc.
314gcr_last_robot_cmd_buf = ""
315gcr_last_robot_rc = 0
316
317
Patrick Williams20f38712022-12-08 06:18:26 -0600318def process_robot_output_files(robot_cmd_buf=None, robot_rc=None, gzip=None):
Michael Walsha0ce75a2018-07-31 13:54:29 -0500319 r"""
320 Process robot output files which can involve several operations:
Michael Walsh410b1782019-10-22 15:56:18 -0500321 - If the files are in a temporary location, using SAVE_STATUS_POLICY to decide whether to move them to a
322 permanent location or to delete them.
Michael Walsha0ce75a2018-07-31 13:54:29 -0500323 - Gzipping them.
324
325 Description of argument(s):
Michael Walsh410b1782019-10-22 15:56:18 -0500326 robot_cmd_buf The complete command string used to invoke robot.
327 robot_rc The return code from running the robot command string.
328 gzip Indicates whether robot-generated output should be gzipped.
Michael Walsha0ce75a2018-07-31 13:54:29 -0500329 """
330
331 robot_cmd_buf = gm.dft(robot_cmd_buf, gcr_last_robot_cmd_buf)
332 robot_rc = gm.dft(robot_rc, gcr_last_robot_rc)
Michael Walshf33140f2018-11-01 14:05:56 -0500333 gzip = gm.dft(gzip, int(os.environ.get("GZIP_ROBOT", "1")))
Michael Walsha0ce75a2018-07-31 13:54:29 -0500334
335 if robot_cmd_buf == "":
Michael Walsh410b1782019-10-22 15:56:18 -0500336 # This can legitimately occur if this function is called from an exit_function without the program
337 # having ever run robot_cmd_fnc.
Michael Walsha0ce75a2018-07-31 13:54:29 -0500338 return
339
340 SAVE_STATUS_POLICY = os.environ.get("SAVE_STATUS_POLICY", "ALWAYS")
341 gp.qprint_vars(SAVE_STATUS_POLICY)
342
Michael Walsh410b1782019-10-22 15:56:18 -0500343 # When SAVE_STATUS_POLICY is "NEVER" robot output files don't even get generated.
Michael Walsha0ce75a2018-07-31 13:54:29 -0500344 if SAVE_STATUS_POLICY == "NEVER":
345 return
346
347 # Compose file_list based on robot command buffer passed in.
348 robot_cmd_buf_dict = gc.parse_command_string(robot_cmd_buf)
Patrick Williams20f38712022-12-08 06:18:26 -0600349 outputdir = robot_cmd_buf_dict["outputdir"]
Michael Walsha0ce75a2018-07-31 13:54:29 -0500350 outputdir = gm.add_trailing_slash(outputdir)
Patrick Williams20f38712022-12-08 06:18:26 -0600351 file_list = (
352 outputdir
353 + robot_cmd_buf_dict["output"]
354 + " "
355 + outputdir
356 + robot_cmd_buf_dict["log"]
357 + " "
358 + outputdir
359 + robot_cmd_buf_dict["report"]
360 )
Michael Walsha0ce75a2018-07-31 13:54:29 -0500361
362 # Double checking that files are present.
Patrick Williams20f38712022-12-08 06:18:26 -0600363 shell_rc, out_buf = gc.shell_cmd(
364 "ls -1 " + file_list + " 2>/dev/null", show_err=0
365 )
Michael Walsha0ce75a2018-07-31 13:54:29 -0500366 file_list = re.sub("\n", " ", out_buf.rstrip("\n"))
367
368 if file_list == "":
Patrick Williams20f38712022-12-08 06:18:26 -0600369 gp.qprint_timen(
370 "No robot output files were found in " + outputdir + "."
371 )
Michael Walsha0ce75a2018-07-31 13:54:29 -0500372 return
Michael Walsh0d5f96a2019-05-20 10:09:57 -0500373 gp.qprint_var(robot_rc, gp.hexa())
Michael Walsha0ce75a2018-07-31 13:54:29 -0500374 if SAVE_STATUS_POLICY == "FAIL" and robot_rc == 0:
Patrick Williams20f38712022-12-08 06:18:26 -0600375 gp.qprint_timen(
376 "The call to robot produced no failures."
377 + " Deleting robot output files."
378 )
Michael Walsha0ce75a2018-07-31 13:54:29 -0500379 gc.shell_cmd("rm -rf " + file_list)
380 return
381
382 if gzip:
Michael Walshe53dfec2018-08-07 15:02:56 -0500383 gc.shell_cmd("gzip -f " + file_list)
Michael Walsha0ce75a2018-07-31 13:54:29 -0500384 # Update the values in file_list.
385 file_list = re.sub(" ", ".gz ", file_list) + ".gz"
386
Michael Walsh410b1782019-10-22 15:56:18 -0500387 # It TMP_ROBOT_DIR_PATH is set, it means the caller wanted the robot output initially directed to
388 # TMP_ROBOT_DIR_PATH but later moved to FFDC_DIR_PATH. Otherwise, we're done.
Michael Walsha0ce75a2018-07-31 13:54:29 -0500389
George Keishinge7e91712021-09-03 11:28:44 -0500390 if os.environ.get("TMP_ROBOT_DIR_PATH", "") == "":
Michael Walsha0ce75a2018-07-31 13:54:29 -0500391 return
392
Michael Walsh410b1782019-10-22 15:56:18 -0500393 # 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 -0600394 target_dir_path = os.environ.get(
395 "FFDC_DIR_PATH", os.environ.get("HOME", ".") + "/ffdc"
396 )
Michael Walsha0ce75a2018-07-31 13:54:29 -0500397 target_dir_path = gm.add_trailing_slash(target_dir_path)
398
Patrick Williams20f38712022-12-08 06:18:26 -0600399 targ_file_list = [
400 re.sub(".*/", target_dir_path, x) for x in file_list.split(" ")
401 ]
Michael Walsha0ce75a2018-07-31 13:54:29 -0500402
Patrick Williams20f38712022-12-08 06:18:26 -0600403 gc.shell_cmd(
404 "mv " + file_list + " " + target_dir_path + " >/dev/null", time_out=600
405 )
Michael Walsha0ce75a2018-07-31 13:54:29 -0500406
407 gp.qprint_timen("New robot log file locations:")
Patrick Williams20f38712022-12-08 06:18:26 -0600408 gp.qprintn("\n".join(targ_file_list))
Michael Walsha0ce75a2018-07-31 13:54:29 -0500409
410
Patrick Williams20f38712022-12-08 06:18:26 -0600411def robot_cmd_fnc(
412 robot_cmd_buf,
413 robot_jail=os.environ.get("ROBOT_JAIL", ""),
414 quiet=None,
415 test_mode=0,
416):
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500417 r"""
418 Run the robot command string.
419
Michael Walsh410b1782019-10-22 15:56:18 -0500420 This function will set the various PATH variables correctly so that you are running the proper version of
421 all imported files, etc.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500422
423 Description of argument(s):
424 robot_cmd_buf The complete robot command string.
Michael Walsh410b1782019-10-22 15:56:18 -0500425 robot_jail Indicates that this is to run in "robot jail" meaning without visibility
426 to any apolloxxx import files, programs, etc.
Michael Walsh8dc99a32020-04-02 13:45:13 -0500427 test_mode If test_mode is set, this function will not actually run the command.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500428 """
429
Patrick Williams20f38712022-12-08 06:18:26 -0600430 quiet = int(gm.dft(quiet, gp.get_stack_var("quiet", 0)))
Michael Walsh2ea965c2019-08-01 16:14:25 -0500431 gv.valid_value(robot_cmd_buf)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500432
Michael Walsha0ce75a2018-07-31 13:54:29 -0500433 # Set global variables to aid in cleanup with process_robot_output_files.
434 global gcr_last_robot_cmd_buf
435 global gcr_last_robot_rc
436 gcr_last_robot_cmd_buf = robot_cmd_buf
437
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500438 # Get globals set by init_robot_test_base_dir_path().
439 module = sys.modules["__main__"]
440 try:
441 ROBOT_TEST_BASE_DIR_PATH = getattr(module, "ROBOT_TEST_BASE_DIR_PATH")
442 except NameError:
443 init_robot_test_base_dir_path()
444 ROBOT_TEST_BASE_DIR_PATH = getattr(module, "ROBOT_TEST_BASE_DIR_PATH")
445
Patrick Williams20f38712022-12-08 06:18:26 -0600446 ROBOT_TEST_RUNNING_FROM_SB = gm.get_mod_global(
447 "ROBOT_TEST_RUNNING_FROM_SB"
448 )
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500449
450 if robot_jail == "":
451 if ROBOT_TEST_RUNNING_FROM_SB:
452 robot_jail = 0
453 else:
454 robot_jail = 1
455
456 robot_jail = int(robot_jail)
Patrick Williams20f38712022-12-08 06:18:26 -0600457 ROBOT_JAIL = os.environ.get("ROBOT_JAIL", "")
458 gp.dprint_vars(
459 ROBOT_TEST_BASE_DIR_PATH,
460 ROBOT_TEST_RUNNING_FROM_SB,
461 ROBOT_JAIL,
462 robot_jail,
463 )
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500464
465 # Save PATH and PYTHONPATH to be restored later.
466 os.environ["SAVED_PYTHONPATH"] = os.environ.get("PYTHONPATH", "")
467 os.environ["SAVED_PATH"] = os.environ.get("PATH", "")
468
469 if robot_jail:
Michael Walsh410b1782019-10-22 15:56:18 -0500470 # Make sure required programs like python and robot can be found in the new restricted PATH.
Michael Walsha5d29bc2019-06-05 15:29:18 -0500471 required_programs = "python robot"
Michael Walsh410b1782019-10-22 15:56:18 -0500472 # It is expected that there will be a "python" program in the tool base bin path which is really a
473 # link to select_version. Ditto for "robot". Call each with the --print_only option to get the
474 # paths to the "real" programs.
Patrick Williams20f38712022-12-08 06:18:26 -0600475 cmd_buf = (
476 "for program in "
477 + required_programs
Michael Walsha5d29bc2019-06-05 15:29:18 -0500478 + " ; do dirname $(${program} --print_only) ; done 2>/dev/null"
Patrick Williams20f38712022-12-08 06:18:26 -0600479 )
Michael Walsha5d29bc2019-06-05 15:29:18 -0500480 rc, out_buf = gc.shell_cmd(cmd_buf, quiet=1, print_output=0)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500481 PYTHONPATH = ROBOT_TEST_BASE_DIR_PATH + "lib"
482 NEW_PATH_LIST = [ROBOT_TEST_BASE_DIR_PATH + "bin"]
Michael Walsha5d29bc2019-06-05 15:29:18 -0500483 NEW_PATH_LIST.extend(list(set(out_buf.rstrip("\n").split("\n"))))
Patrick Williams20f38712022-12-08 06:18:26 -0600484 NEW_PATH_LIST.extend(
485 [
486 "/usr/local/sbin",
487 "/usr/local/bin",
488 "/usr/sbin",
489 "/usr/bin",
490 "/sbin",
491 "/bin",
Patrick Williams20f38712022-12-08 06:18:26 -0600492 ]
493 )
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500494 PATH = ":".join(NEW_PATH_LIST)
495 else:
Patrick Williams20f38712022-12-08 06:18:26 -0600496 PYTHONPATH = (
497 os.environ.get("PYTHONPATH", "")
498 + ":"
499 + ROBOT_TEST_BASE_DIR_PATH
500 + "lib"
501 )
502 PATH = (
George Keishing12de0942023-12-08 17:02:13 +0530503 os.environ.get("PATH", "") + ":" + ROBOT_TEST_BASE_DIR_PATH + "bin"
Patrick Williams20f38712022-12-08 06:18:26 -0600504 )
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500505
Patrick Williams20f38712022-12-08 06:18:26 -0600506 os.environ["PYTHONPATH"] = PYTHONPATH
507 os.environ["PATH"] = PATH
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500508 gp.dprint_vars(PATH, PYTHONPATH)
509
Patrick Williams20f38712022-12-08 06:18:26 -0600510 os.environ["FFDC_DIR_PATH_STYLE"] = os.environ.get(
511 "FFDC_DIR_PATH_STYLE", "1"
512 )
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500513 gp.qpissuing(robot_cmd_buf, test_mode)
514 if test_mode:
515 os.environ["PATH"] = os.environ.get("SAVED_PATH", "")
516 os.environ["PYTHONPATH"] = os.environ.get("SAVED_PYTHONPATH", "")
517 return True
518
519 if quiet:
Patrick Williams20f38712022-12-08 06:18:26 -0600520 DEVNULL = open(os.devnull, "wb")
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500521 stdout = DEVNULL
522 else:
523 stdout = None
524 sub_proc = subprocess.Popen(robot_cmd_buf, stdout=stdout, shell=True)
525 sub_proc.communicate()
526 shell_rc = sub_proc.returncode
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500527 os.environ["PATH"] = os.environ.get("SAVED_PATH", "")
528 os.environ["PYTHONPATH"] = os.environ.get("SAVED_PYTHONPATH", "")
Michael Walsha0ce75a2018-07-31 13:54:29 -0500529 gcr_last_robot_rc = shell_rc
530 process_robot_output_files()
531 if shell_rc != 0:
Michael Walsh0d5f96a2019-05-20 10:09:57 -0500532 gp.print_var(shell_rc, gp.hexa())
Michael Walsha0ce75a2018-07-31 13:54:29 -0500533 return False
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500534
535 return True