blob: 6c3f8d883ab9507e7361dd288620c68f5abff4a0 [file] [log] [blame]
Michael Walshf4c62a22017-11-13 15:40:57 -06001#!/usr/bin/env python
2
3r"""
4This module provides functions which are useful to plug-in call point programs.
5"""
6
7import sys
8import os
9import re
10import collections
11
12import gen_print as gp
Michael Walshe5599df2019-11-20 14:05:10 -060013import gen_valid as gv
Michael Walsh3ba8ecd2018-04-24 11:33:25 -050014import gen_misc as gm
Michael Walsha6b46ed2018-06-01 14:31:23 -050015import gen_cmd as gc
16
17PLUG_VAR_PREFIX = os.environ.get("PLUG_VAR_PREFIX", "AUTOBOOT")
Michael Walshf4c62a22017-11-13 15:40:57 -060018
19
20def get_plug_in_package_name(case=None):
Michael Walshf4c62a22017-11-13 15:40:57 -060021 r"""
22 Return the plug-in package name (e.g. "OS_Console", "DB_Logging").
23
24 Description of argument(s):
Michael Walsh0cb727d2019-10-16 17:16:45 -050025 case Indicates whether the value returned should be converted to upper or
26 lower case. Valid values are "upper", "lower" or None.
Michael Walshf4c62a22017-11-13 15:40:57 -060027 """
28
29 plug_in_package_name = os.path.basename(gp.pgm_dir_path[:-1])
30 if case == "upper":
31 return plug_in_package_name.upper()
32 elif case == "lower":
33 return plug_in_package_name.lower()
34 else:
35 return plug_in_package_name
36
37
Michael Walshfd7443d2018-10-30 13:12:18 -050038def return_plug_vars(general=True,
Michael Walshaeeb5272019-11-20 13:58:47 -060039 custom=True,
40 plug_in_package_name=None):
Michael Walshf4c62a22017-11-13 15:40:57 -060041 r"""
Michael Walsh0cb727d2019-10-16 17:16:45 -050042 Return an OrderedDict which is sorted by key and which contains all of the plug-in environment variables.
Michael Walshf4c62a22017-11-13 15:40:57 -060043
44 Example excerpt of resulting dictionary:
45
46 plug_var_dict:
Michael Walsh2ce1dba2019-02-05 19:29:28 +000047 [AUTOBOOT_BASE_TOOL_DIR_PATH]: /tmp/
Michael Walshf4c62a22017-11-13 15:40:57 -060048 [AUTOBOOT_BB_LEVEL]: <blank>
49 [AUTOBOOT_BOOT_FAIL]: 0
50 ...
51
52 This function also does the following:
Michael Walsh0cb727d2019-10-16 17:16:45 -050053 - Set a default value for environment variable AUTOBOOT_OPENBMC_NICKNAME/AUTOIPL_FSP1_NICKNAME if it is
54 not already set.
Michael Walshf4c62a22017-11-13 15:40:57 -060055 - Register PASSWORD variables to prevent their values from being printed.
Michael Walsha6b46ed2018-06-01 14:31:23 -050056
Michael Walsh0cb727d2019-10-16 17:16:45 -050057 Note: The programmer may set a default for any given environment variable by declaring a global variable
58 of the same name and setting its value. For example, let's say the calling program has this global
59 declaration:
Michael Walsha6b46ed2018-06-01 14:31:23 -050060
61 PERF_EXERCISERS_TOTAL_TIMEOUT = '180'
62
Michael Walsh0cb727d2019-10-16 17:16:45 -050063 If environment variable PERF_EXERCISERS_TOTAL_TIMEOUT is blank or not set, this function will set it to
64 '180'.
65
66 Furthermore, if such a default variable declaration is not a string, this function will preserve that
67 non-string type in setting global variables (with the exception of os.environ values which must be
68 string). Example:
69
70 NVDIMM_ENCRYPT = 0
Michael Walshfd7443d2018-10-30 13:12:18 -050071
72 Description of argument(s):
Michael Walsh0cb727d2019-10-16 17:16:45 -050073 general Return general plug-in parms (e.g. those beginning with "AUTOBOOT" or
74 "AUTOGUI").
75 custom Return custom plug-in parms (i.e. those beginning with the upper case
76 name of the plug-in package, for example "OBMC_SAMPLE_PARM1").
Michael Walshaeeb5272019-11-20 13:58:47 -060077 plug_in_package_name The name of the plug-in package for which custom parms are to be
78 returned. The default is the current plug in package name.
Michael Walshf4c62a22017-11-13 15:40:57 -060079 """
80
Michael Walshfd7443d2018-10-30 13:12:18 -050081 regex_list = []
82 if not (general or custom):
83 return collections.OrderedDict()
Michael Walshaeeb5272019-11-20 13:58:47 -060084 plug_in_package_name = gm.dft(plug_in_package_name, get_plug_in_package_name())
Michael Walshfd7443d2018-10-30 13:12:18 -050085 if general:
86 regex_list = [PLUG_VAR_PREFIX, "AUTOGUI"]
87 if custom:
Michael Walshaeeb5272019-11-20 13:58:47 -060088 regex_list.append(plug_in_package_name.upper())
Michael Walshfd7443d2018-10-30 13:12:18 -050089
90 regex = "^(" + "|".join(regex_list) + ")_"
Michael Walshf4c62a22017-11-13 15:40:57 -060091
92 # Set a default for nickname.
93 if os.environ.get("AUTOBOOT_OPENBMC_NICKNAME", "") == "":
94 os.environ['AUTOBOOT_OPENBMC_NICKNAME'] = \
95 os.environ.get("AUTOBOOT_OPENBMC_HOST", "")
96
Michael Walsha6b46ed2018-06-01 14:31:23 -050097 if os.environ.get("AUTOIPL_FSP1_NICKNAME", "") == "":
98 os.environ['AUTOIPL_FSP1_NICKNAME'] = \
99 os.environ.get("AUTOIPL_FSP1_NAME", "").split(".")[0]
100
Michael Walsh0cb727d2019-10-16 17:16:45 -0500101 # For all variables specified in the parm_def file, we want them to default to "" rather than being unset.
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500102 # Process the parm_def file if it exists.
Michael Walshaeeb5272019-11-20 13:58:47 -0600103 parm_def_file_path = os.path.dirname(gp.pgm_dir_path.rstrip("/")) + "/" + plug_in_package_name \
104 + "/parm_def"
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500105 if os.path.exists(parm_def_file_path):
106 parm_defs = gm.my_parm_file(parm_def_file_path)
107 else:
108 parm_defs = collections.OrderedDict()
109 # Example parm_defs:
110 # parm_defs:
111 # parm_defs[rest_fail]: boolean
112 # parm_defs[command]: string
113 # parm_defs[esel_stop_file_path]: string
114
Michael Walsh0cb727d2019-10-16 17:16:45 -0500115 # Create a list of plug-in environment variables by pre-pending <all caps plug-in package name>_<all
116 # caps var name>
Michael Walshaeeb5272019-11-20 13:58:47 -0600117 plug_in_parm_names = [plug_in_package_name.upper() + "_" + x for x in
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500118 map(str.upper, parm_defs.keys())]
119 # Example plug_in_parm_names:
120 # plug_in_parm_names:
121 # plug_in_parm_names[0]: STOP_REST_FAIL
122 # plug_in_parm_names[1]: STOP_COMMAND
123 # plug_in_parm_names[2]: STOP_ESEL_STOP_FILE_PATH
124
Michael Walsh0cb727d2019-10-16 17:16:45 -0500125 # os.environ only accepts string values. However, if the user defines default values of other types
126 # (e.g. int), we wish to preserve the type.
127 non_string_defaults = {}
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500128 # Initialize unset plug-in vars.
129 for var_name in plug_in_parm_names:
Michael Walsh0cb727d2019-10-16 17:16:45 -0500130 # If there is a global variable with the same name as the environment variable, use its value as a
131 # default.
Michael Walsha6b46ed2018-06-01 14:31:23 -0500132 default_value = gm.get_mod_global(var_name, "")
Michael Walsh0cb727d2019-10-16 17:16:45 -0500133 if type(default_value) is not str:
134 non_string_defaults[var_name] = type(default_value)
135 os.environ[var_name] = os.environ.get(var_name, str(default_value))
Michael Walsha6b46ed2018-06-01 14:31:23 -0500136 if os.environ[var_name] == "":
Michael Walshc1df1502019-11-04 14:38:13 -0600137 os.environ[var_name] = str(default_value)
Michael Walsh3ba8ecd2018-04-24 11:33:25 -0500138
Michael Walshf4c62a22017-11-13 15:40:57 -0600139 plug_var_dict = \
140 collections.OrderedDict(sorted({k: v for (k, v) in
Gunnar Mills096cd562018-03-26 10:19:12 -0500141 os.environ.items()
142 if re.match(regex, k)}.items()))
Michael Walsh0cb727d2019-10-16 17:16:45 -0500143 # Restore the types of any variables where the caller had defined default values.
144 for key, value in non_string_defaults.items():
145 cmd_buf = "plug_var_dict[key] = " + str(value).split("'")[1] + "(plug_var_dict[key]"
146 if value is int:
147 # Use int base argument of 0 to allow it to interpret hex strings.
148 cmd_buf += ", 0)"
149 else:
150 cmd_buf += ")"
Michael Walshc09b7cb2019-10-22 10:39:17 -0500151 exec(cmd_buf) in globals(), locals()
Michael Walsh0cb727d2019-10-16 17:16:45 -0500152 # Register password values to prevent printing them out. Any plug var whose name ends in PASSWORD will
153 # be registered.
Michael Walshf4c62a22017-11-13 15:40:57 -0600154 password_vals = {k: v for (k, v) in plug_var_dict.items()
155 if re.match(r".*_PASSWORD$", k)}.values()
156 map(gp.register_passwords, password_vals)
157
158 return plug_var_dict
159
160
Michael Walshfd7443d2018-10-30 13:12:18 -0500161def sprint_plug_vars(headers=1, **kwargs):
Michael Walshf4c62a22017-11-13 15:40:57 -0600162 r"""
Michael Walsh0cb727d2019-10-16 17:16:45 -0500163 Sprint the plug-in environment variables (i.e. those that begin with the global PLUG_VAR_PREFIX value or
164 those that begin with <plug-in package_name>_ in upper case letters.).
Michael Walshf4c62a22017-11-13 15:40:57 -0600165
166 Example excerpt of output:
Michael Walsh2ce1dba2019-02-05 19:29:28 +0000167 AUTOBOOT_BASE_TOOL_DIR_PATH=/tmp/
Michael Walshf4c62a22017-11-13 15:40:57 -0600168 AUTOBOOT_BB_LEVEL=
169 AUTOBOOT_BOOT_FAIL=0
170 AUTOBOOT_BOOT_FAIL_THRESHOLD=1000000
171
172 Description of argument(s):
173 headers Print a header and a footer.
Michael Walsh0cb727d2019-10-16 17:16:45 -0500174 kwargs These are passed directly to return_plug_vars. See return_plug_vars doc
175 string for details.
Michael Walshf4c62a22017-11-13 15:40:57 -0600176 """
Michael Walshfd7443d2018-10-30 13:12:18 -0500177 plug_var_dict = return_plug_vars(**kwargs)
Michael Walshf4c62a22017-11-13 15:40:57 -0600178 buffer = ""
179 if headers:
180 buffer += "\n" + gp.sprint_dashes()
181 for key, value in plug_var_dict.items():
Michael Walshfd7443d2018-10-30 13:12:18 -0500182 buffer += gp.sprint_varx(key, value)
Michael Walshf4c62a22017-11-13 15:40:57 -0600183 if headers:
184 buffer += gp.sprint_dashes() + "\n"
185
186 return buffer
187
188
Michael Walshfd7443d2018-10-30 13:12:18 -0500189def print_plug_in_header():
190 r"""
191 Print plug-in header.
192
Michael Walsh0cb727d2019-10-16 17:16:45 -0500193 When debug is set, print all plug_prefix variables (e.g. AUTOBOOT_OPENBMC_HOST, etc.) and all plug-in
194 environment variables (e.g. OBMC_SAMPLE_PARM1) with surrounding dashed lines. When debug is not set,
195 print only the plug-in environment variables (e.g. OBMC_SAMPLE_PARM1) with no surrounding dashed lines.
Michael Walshfd7443d2018-10-30 13:12:18 -0500196
Michael Walsh0cb727d2019-10-16 17:16:45 -0500197 NOTE: plug-in environment variables means any variable defined in the <plug-in dir>/parm_def file plus
198 any environment variables whose names begin with the upper-case plug-in package name.
Michael Walshfd7443d2018-10-30 13:12:18 -0500199 """
200
201 dprint_plug_vars()
202 if not debug:
203 qprint_plug_vars(headers=0, general=False, custom=True)
204
205
Michael Walsh47f8a602019-11-20 14:02:11 -0600206def get_plug_vars(mod_name="__main__", **kwargs):
Michael Walshf4c62a22017-11-13 15:40:57 -0600207 r"""
208 Get all plug-in variables and put them in corresponding global variables.
209
Michael Walsh0cb727d2019-10-16 17:16:45 -0500210 This would include all environment variables beginning with either the global PLUG_VAR_PREFIX value or
211 with the upper case version of the plug-in package name + underscore (e.g. OP_SAMPLE_VAR1 for plug-in
212 OP_Sample).
Michael Walshf4c62a22017-11-13 15:40:57 -0600213
Michael Walsh0cb727d2019-10-16 17:16:45 -0500214 The global variables to be set will be both with and without the global PLUG_VAR_PREFIX value prefix.
215 For example, if the environment variable in question is AUTOBOOT_OPENBMC_HOST, this function will set
216 global variable AUTOBOOT_OPENBMC_HOST and global variable OPENBMC_HOST.
Michael Walsh8b79b052019-05-02 17:07:08 -0500217
218 Description of argument(s):
Michael Walsh0cb727d2019-10-16 17:16:45 -0500219 mod_name The name of the module whose global plug-in variables should be retrieved.
Michael Walsh47f8a602019-11-20 14:02:11 -0600220 kwargs These are passed directly to return_plug_vars. See return_plug_vars's
221 prolog for details.
Michael Walshf4c62a22017-11-13 15:40:57 -0600222 """
223
Michael Walsh8b79b052019-05-02 17:07:08 -0500224 module = sys.modules[mod_name]
Michael Walsh47f8a602019-11-20 14:02:11 -0600225 plug_var_dict = return_plug_vars(**kwargs)
Michael Walshf4c62a22017-11-13 15:40:57 -0600226
Michael Walsha6b46ed2018-06-01 14:31:23 -0500227 # Get all PLUG_VAR_PREFIX environment variables and put them into globals.
Michael Walshf4c62a22017-11-13 15:40:57 -0600228 for key, value in plug_var_dict.items():
229 setattr(module, key, value)
Michael Walsha6b46ed2018-06-01 14:31:23 -0500230 setattr(module, re.sub("^" + PLUG_VAR_PREFIX + "_", "", key), value)
Michael Walshf4c62a22017-11-13 15:40:57 -0600231
232
233def get_plug_default(var_name,
234 default=None):
Michael Walshf4c62a22017-11-13 15:40:57 -0600235 r"""
236 Derive and return a default value for the given parm variable.
237
Michael Walsha6b46ed2018-06-01 14:31:23 -0500238 Dependencies:
239 Global variable PLUG_VAR_PREFIX must be set.
240
Michael Walsh0cb727d2019-10-16 17:16:45 -0500241 This function will assign a default by checking the following environment variables in the order shown.
242 The first one that has a value will be used.
Michael Walshf4c62a22017-11-13 15:40:57 -0600243 - <upper case package_name>_<var_name>
Michael Walsha6b46ed2018-06-01 14:31:23 -0500244 - <PLUG_VAR_PREFIX>_OVERRIDE_<var_name>
245 - <PLUG_VAR_PREFIX>_<var_name>
Michael Walshf4c62a22017-11-13 15:40:57 -0600246
Michael Walsh0cb727d2019-10-16 17:16:45 -0500247 If none of these are found, this function will return the value passed by the caller in the "default"
248 parm.
Michael Walshf4c62a22017-11-13 15:40:57 -0600249
250 Example:
251
Michael Walsh0cb727d2019-10-16 17:16:45 -0500252 Let's say your plug-in is named "OS_Console" and you call this function as follows:
Michael Walshf4c62a22017-11-13 15:40:57 -0600253
254 get_plug_default("quiet", 0)
255
Michael Walsh0cb727d2019-10-16 17:16:45 -0500256 The first of these environment variables that is found to be set will be used to provide the default
257 value.
Michael Walshf4c62a22017-11-13 15:40:57 -0600258 - OS_CONSOLE_QUIET
259 - AUTOBOOT_OVERRIDE_QUIET
260 - AUTOBOOT_QUIET
261
Michael Walsh0cb727d2019-10-16 17:16:45 -0500262 If none of those has a value, 0 (as specified by the caller in this example) is returned.
Michael Walshf4c62a22017-11-13 15:40:57 -0600263
Michael Walsh0cb727d2019-10-16 17:16:45 -0500264 Let's say the master driver program is named obmc_boot. obmc_boot program is responsible for calling
265 plug-ins. Let's further suppose that the user wishes to run the master program with --debug=0 but wishes
266 to have all plug-ins run with --debug=1. This could be accomplished with the following call:
267 export AUTOBOOT_OVERRIDE_DEBUG=1 ; obmc_boot --debug=0 --plug_in_dir_paths=<list of plug ins>
268
269 As another example, let's suppose that the user wishes to have just the OS_Console plug-in run with debug
270 and everything else to default to debug=0. This could be accomplished as follows:
271 export OS_CONSOLE_DEBUG=1 ; obmc_boot --debug=0 --plug_in_dir_paths=<list of plug ins>
272
273 And as one more example, let's say the user wishes to have obmc_boot and OS_Console run without debug but
274 have all other plug-ins run with debug:
275 export AUTOBOOT_OVERRIDE_DEBUG=1 ; export OS_CONSOLE_DEBUG=0 ; obmc_boot --debug=0
Michael Walshf4c62a22017-11-13 15:40:57 -0600276 --plug_in_dir_paths=<list of plug ins>
277
Michael Walshf4c62a22017-11-13 15:40:57 -0600278 Description of argument(s):
Michael Walsh0cb727d2019-10-16 17:16:45 -0500279 var_name The name of the variable for which a default value is to be calculated.
280 default The default value if one cannot be determined.
Michael Walshf4c62a22017-11-13 15:40:57 -0600281 """
282
283 var_name = var_name.upper()
284 plug_in_package_name = get_plug_in_package_name(case="upper")
285
286 package_var_name = plug_in_package_name + "_" + var_name
287 default_value = os.environ.get(package_var_name, None)
288 if default_value is not None:
289 # A package-name version of the variable was found so return its value.
290 return(default_value)
291
Michael Walsha6b46ed2018-06-01 14:31:23 -0500292 plug_var_name = PLUG_VAR_PREFIX + "_OVERRIDE_" + var_name
293 default_value = os.environ.get(plug_var_name, None)
Michael Walshf4c62a22017-11-13 15:40:57 -0600294 if default_value is not None:
Michael Walsh0cb727d2019-10-16 17:16:45 -0500295 # A PLUG_VAR_PREFIX version of the variable was found so return its value.
Michael Walshf4c62a22017-11-13 15:40:57 -0600296 return default_value
297
Michael Walsha6b46ed2018-06-01 14:31:23 -0500298 plug_var_name = PLUG_VAR_PREFIX + "_" + var_name
299 default_value = os.environ.get(plug_var_name, None)
Michael Walshf4c62a22017-11-13 15:40:57 -0600300 if default_value is not None:
Michael Walsh0cb727d2019-10-16 17:16:45 -0500301 # A PLUG_VAR_PREFIX version of the variable was found so return its value.
Michael Walshf4c62a22017-11-13 15:40:57 -0600302 return default_value
303
304 return default
305
306
Michael Walshe5599df2019-11-20 14:05:10 -0600307def required_plug_in(required_plug_in_names,
308 plug_in_dir_paths=None):
Michael Walshf4c62a22017-11-13 15:40:57 -0600309 r"""
Michael Walshe5599df2019-11-20 14:05:10 -0600310 Determine whether the required_plug_in_names are in plug_in_dir_paths, construct an error_message and
311 call gv.process_error_message(error_message).
312
313 In addition, for each plug-in in required_plug_in_names, set the global plug-in variables. This is
314 useful for callers who then want to validate certain values from other plug-ins.
Michael Walshf4c62a22017-11-13 15:40:57 -0600315
316 Example call:
Michael Walshe5599df2019-11-20 14:05:10 -0600317 required_plug_in(required_plug_in_names)
Michael Walshf4c62a22017-11-13 15:40:57 -0600318
319 Description of argument(s):
Michael Walshe5599df2019-11-20 14:05:10 -0600320 required_plug_in_names A list of plug_in names that the caller requires (e.g. ['OS_Console']).
Michael Walsh0cb727d2019-10-16 17:16:45 -0500321 plug_in_dir_paths A string which is a colon-delimited list of plug-ins specified by the
322 user (e.g. DB_Logging:FFDC:OS_Console:Perf). Path values (e.g.
323 "/home/robot/dir1") will be stripped from this list to do the analysis.
Michael Walshe5599df2019-11-20 14:05:10 -0600324 Default value is the AUTOGUI_PLUG_IN_DIR_PATHS or
325 <PLUG_VAR_PREFIX>_PLUG_IN_DIR_PATHS environment variable.
Michael Walshf4c62a22017-11-13 15:40:57 -0600326 """
327
328 # Calculate default value for plug_in_dir_paths.
Michael Walshe5599df2019-11-20 14:05:10 -0600329 plug_in_dir_paths = gm.dft(plug_in_dir_paths,
330 os.environ.get('AUTOGUI_PLUG_IN_DIR_PATHS',
331 os.environ.get(PLUG_VAR_PREFIX + "_PLUG_IN_DIR_PATHS", "")))
Michael Walshf4c62a22017-11-13 15:40:57 -0600332
333 # Convert plug_in_dir_paths to a list of base names.
334 plug_in_dir_paths = \
Michael Walsh6aa5a9e2018-08-07 15:04:56 -0500335 list(filter(None, map(os.path.basename, plug_in_dir_paths.split(":"))))
Michael Walshf4c62a22017-11-13 15:40:57 -0600336
Michael Walshe5599df2019-11-20 14:05:10 -0600337 error_message = gv.valid_list(plug_in_dir_paths, required_values=required_plug_in_names)
338 if error_message:
339 return gv.process_error_message(error_message)
Michael Walshf4c62a22017-11-13 15:40:57 -0600340
Michael Walshe5599df2019-11-20 14:05:10 -0600341 for plug_in_package_name in required_plug_in_names:
342 get_plug_vars(general=False, plug_in_package_name=plug_in_package_name)
Michael Walshf4c62a22017-11-13 15:40:57 -0600343
344
Michael Walsh96ffeb72018-08-23 11:37:22 -0500345def compose_plug_in_save_dir_path(plug_in_package_name=None):
Michael Walsha6b46ed2018-06-01 14:31:23 -0500346 r"""
Michael Walsh0cb727d2019-10-16 17:16:45 -0500347 Create and return a directory path name that is suitable for saving plug-in data.
Michael Walsha6b46ed2018-06-01 14:31:23 -0500348
Michael Walsh0cb727d2019-10-16 17:16:45 -0500349 The name will be comprised of things such as plug_in package name, pid, etc. in order to guarantee that
350 it is unique for a given test run.
Michael Walsh96ffeb72018-08-23 11:37:22 -0500351
352 Description of argument(s):
Michael Walsh0cb727d2019-10-16 17:16:45 -0500353 plug_in_package_name The plug-in package name. This defaults to the name of the caller's
354 plug-in package. However, the caller can specify another value in order
355 to retrieve data saved by another plug-in package.
Michael Walsha6b46ed2018-06-01 14:31:23 -0500356 """
357
Michael Walsh96ffeb72018-08-23 11:37:22 -0500358 plug_in_package_name = gm.dft(plug_in_package_name,
359 get_plug_in_package_name())
360
Michael Walsha6b46ed2018-06-01 14:31:23 -0500361 BASE_TOOL_DIR_PATH = \
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500362 gm.add_trailing_slash(os.environ.get(PLUG_VAR_PREFIX
Michael Walsh2ce1dba2019-02-05 19:29:28 +0000363 + "_BASE_TOOL_DIR_PATH",
364 "/tmp/"))
Michael Walsha6b46ed2018-06-01 14:31:23 -0500365 NICKNAME = os.environ.get("AUTOBOOT_OPENBMC_NICKNAME", "")
366 if NICKNAME == "":
367 NICKNAME = os.environ["AUTOIPL_FSP1_NICKNAME"]
368 MASTER_PID = os.environ[PLUG_VAR_PREFIX + "_MASTER_PID"]
Michael Walsh053d0182019-09-06 16:27:33 -0500369 gp.qprint_vars(BASE_TOOL_DIR_PATH, NICKNAME, plug_in_package_name,
370 MASTER_PID)
Michael Walsh2ce1dba2019-02-05 19:29:28 +0000371 return BASE_TOOL_DIR_PATH + gm.username() + "/" + NICKNAME + "/" +\
372 plug_in_package_name + "/" + str(MASTER_PID) + "/"
Michael Walsha6b46ed2018-06-01 14:31:23 -0500373
374
Michael Walsh96ffeb72018-08-23 11:37:22 -0500375def create_plug_in_save_dir(plug_in_package_name=None):
Michael Walsha6b46ed2018-06-01 14:31:23 -0500376 r"""
Michael Walsh0cb727d2019-10-16 17:16:45 -0500377 Create a directory suitable for saving plug-in processing data and return its path name.
Michael Walsh024a2f32019-04-18 10:55:34 -0500378
379 See compose_plug_in_save_dir_path for details.
Michael Walsh96ffeb72018-08-23 11:37:22 -0500380
381 Description of argument(s):
Michael Walsh0cb727d2019-10-16 17:16:45 -0500382 plug_in_package_name See compose_plug_in_save_dir_path for details.
Michael Walsha6b46ed2018-06-01 14:31:23 -0500383 """
384
Michael Walsh96ffeb72018-08-23 11:37:22 -0500385 plug_in_save_dir_path = compose_plug_in_save_dir_path(plug_in_package_name)
Michael Walsha6b46ed2018-06-01 14:31:23 -0500386 if os.path.isdir(plug_in_save_dir_path):
387 return plug_in_save_dir_path
388 gc.shell_cmd("mkdir -p " + plug_in_save_dir_path)
389 return plug_in_save_dir_path
390
391
Michael Walsh96ffeb72018-08-23 11:37:22 -0500392def delete_plug_in_save_dir(plug_in_package_name=None):
Michael Walsha6b46ed2018-06-01 14:31:23 -0500393 r"""
Michael Walsh0cb727d2019-10-16 17:16:45 -0500394 Delete the plug_in save directory. See compose_plug_in_save_dir_path for details.
Michael Walsh96ffeb72018-08-23 11:37:22 -0500395
396 Description of argument(s):
Michael Walsh0cb727d2019-10-16 17:16:45 -0500397 plug_in_package_name See compose_plug_in_save_dir_path for details.
Michael Walsha6b46ed2018-06-01 14:31:23 -0500398 """
399
Michael Walsh96ffeb72018-08-23 11:37:22 -0500400 gc.shell_cmd("rm -rf "
401 + compose_plug_in_save_dir_path(plug_in_package_name))
Michael Walsha6b46ed2018-06-01 14:31:23 -0500402
403
Michael Walsh96ffeb72018-08-23 11:37:22 -0500404def save_plug_in_value(value, plug_in_package_name=None):
Michael Walsha6b46ed2018-06-01 14:31:23 -0500405 r"""
Michael Walsh0cb727d2019-10-16 17:16:45 -0500406 Save a value in a plug-in save file. The value may be retrieved later via a call to the
407 restore_plug_in_value function.
Michael Walsha6b46ed2018-06-01 14:31:23 -0500408
Michael Walsh0cb727d2019-10-16 17:16:45 -0500409 This function will figure out the variable name of the value passed and use that name in creating the
410 plug-in save file.
Michael Walsha6b46ed2018-06-01 14:31:23 -0500411
412 Example call:
413
414 my_var1 = 5
415 save_plug_in_value(my_var1)
416
Michael Walsh0cb727d2019-10-16 17:16:45 -0500417 In this example, the value "5" would be saved to the "my_var1" file in the plug-in save directory.
Michael Walsha6b46ed2018-06-01 14:31:23 -0500418
419 Description of argument(s):
420 value The value to be saved.
Michael Walsh0cb727d2019-10-16 17:16:45 -0500421 plug_in_package_name See compose_plug_in_save_dir_path for details.
Michael Walsha6b46ed2018-06-01 14:31:23 -0500422 """
423
424 # Get the name of the variable used as argument one to this function.
425 var_name = gp.get_arg_name(0, 1, stack_frame_ix=2)
Michael Walsh96ffeb72018-08-23 11:37:22 -0500426 plug_in_save_dir_path = create_plug_in_save_dir(plug_in_package_name)
Michael Walsha6b46ed2018-06-01 14:31:23 -0500427 save_file_path = plug_in_save_dir_path + var_name
428 gp.qprint_timen("Saving \"" + var_name + "\" value.")
429 gc.shell_cmd("echo '" + str(value) + "' > " + save_file_path)
430
431
Michael Walsh96ffeb72018-08-23 11:37:22 -0500432def restore_plug_in_value(default="", plug_in_package_name=None):
Michael Walsha6b46ed2018-06-01 14:31:23 -0500433 r"""
434 Return a value from a plug-in save file.
435
Michael Walsh0cb727d2019-10-16 17:16:45 -0500436 The name of the value to be restored will be determined by this function based on the lvalue being
437 assigned. Consider the following example:
Michael Walsha6b46ed2018-06-01 14:31:23 -0500438
439 my_var1 = restore_plug_in_value(2)
440
Michael Walsh0cb727d2019-10-16 17:16:45 -0500441 In this example, this function would look for the "my_var1" file in the plug-in save directory, read its
442 value and return it. If no such file exists, the default value of 2 would be returned.
Michael Walsha6b46ed2018-06-01 14:31:23 -0500443
444 Description of argument(s):
Michael Walsh0cb727d2019-10-16 17:16:45 -0500445 default The default value to be returned if there is no plug-in save file for the
446 value in question.
447 plug_in_package_name See compose_plug_in_save_dir_path for details.
Michael Walsha6b46ed2018-06-01 14:31:23 -0500448 """
449
450 # Get the lvalue from the caller's invocation of this function.
451 lvalue = gp.get_arg_name(0, -1, stack_frame_ix=2)
Michael Walsh96ffeb72018-08-23 11:37:22 -0500452 plug_in_save_dir_path = create_plug_in_save_dir(plug_in_package_name)
Michael Walsha6b46ed2018-06-01 14:31:23 -0500453 save_file_path = plug_in_save_dir_path + lvalue
454 if os.path.isfile(save_file_path):
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500455 gp.qprint_timen("Restoring " + lvalue + " value from "
456 + save_file_path + ".")
Michael Walsh25f0f162018-09-10 13:57:11 -0500457 value = gm.file_to_list(save_file_path, newlines=0, comments=0,
458 trim=1)[0]
459 if type(default) is bool:
460 # Convert from string to bool.
461 value = (value == 'True')
462 if type(default) is int:
463 # Convert from string to int.
464 value = int(value)
465 gp.qprint_varx(lvalue, value)
466 return value
Michael Walsha6b46ed2018-06-01 14:31:23 -0500467 else:
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500468 gp.qprint_timen("Save file " + save_file_path
469 + " does not exist so returning default value.")
Michael Walsh25f0f162018-09-10 13:57:11 -0500470 gp.qprint_var(default)
Michael Walsha6b46ed2018-06-01 14:31:23 -0500471 return default
472
473
Michael Walsh99beb652019-02-25 10:45:23 -0600474def exit_not_master():
475 r"""
Michael Walsh0cb727d2019-10-16 17:16:45 -0500476 Exit the program with return code zero if this program was NOT called by the master program.
Michael Walsh99beb652019-02-25 10:45:23 -0600477
478 There are cases where plug-ins are called by a multi-layered stack:
479
480 master_wrapper
481 obmc_boot_test.py
482 Example_plug_in/cp_setup
483
Michael Walsh0cb727d2019-10-16 17:16:45 -0500484 In a scenario like this, Example_plug_in/cp_setup may be called once directly by master_wrapper (the
485 master) and and then called again directly by obmc_boot_test.py (the child). Some plug-in programs may
486 wish to avoid doing any processing on the second such call. This function will achieve that purpose.
Michael Walsh99beb652019-02-25 10:45:23 -0600487
488 This function will print a standard message to stdout prior to exiting.
489 """
490
491 AUTOBOOT_MASTER_PID = gm.get_mod_global("AUTOBOOT_MASTER_PID")
492 AUTOBOOT_PROGRAM_PID = gm.get_mod_global("AUTOBOOT_PROGRAM_PID")
493
494 if AUTOBOOT_MASTER_PID != AUTOBOOT_PROGRAM_PID:
495 message = get_plug_in_package_name() + "/" + gp.pgm_name + " is not" \
496 + " being called by the master program in the stack so no action" \
497 + " will be taken."
498 gp.qprint_timen(message)
499 gp.qprint_vars(AUTOBOOT_MASTER_PID, AUTOBOOT_PROGRAM_PID)
500 exit(0)
501
502
Michael Walsh0cb727d2019-10-16 17:16:45 -0500503def stop_test_rc():
504 r"""
505 Return the constant stop test return code value.
506
507 When a plug-in call point program returns this value, it indicates that master program should stop
508 running.
509 """
510
511 return 0x00000002
512
513
Michael Walshf4c62a22017-11-13 15:40:57 -0600514# Create print wrapper functions for all sprint functions defined above.
Michael Walsh0cb727d2019-10-16 17:16:45 -0500515# func_names contains a list of all print functions which should be created from their sprint counterparts.
Michael Walshf4c62a22017-11-13 15:40:57 -0600516func_names = ['print_plug_vars']
517
Michael Walsh0cb727d2019-10-16 17:16:45 -0500518# stderr_func_names is a list of functions whose output should go to stderr rather than stdout.
Michael Walshf4c62a22017-11-13 15:40:57 -0600519stderr_func_names = []
520
521replace_dict = dict(gp.replace_dict)
Michael Walsh81c02342018-01-05 15:43:28 -0600522replace_dict['mod_qualifier'] = 'gp.'
Michael Walshf4c62a22017-11-13 15:40:57 -0600523func_defs = gp.create_print_wrapper_funcs(func_names, stderr_func_names,
524 replace_dict)
525gp.gp_debug_print(func_defs)
526exec(func_defs)