def valid_integer(var_value): r""" Return True if var_value is a valid integer. Otherwise, return False and print an error message to stderr. Description of arguments: var_value The value being validated. """ # This currently allows floats which is not good. try: if type(int(var_value)) is int: return True except ValueError: pass # If we get to this point, the validation has failed. var_name = gp.get_arg_name(0, 1, 2) gp.print_varx("var_name", var_name) gp.print_error_report("Invalid integer value:\n" + gp.sprint_varx(var_name, var_value)) return False
def get_host_name_ip(host=None, short_name=0): r""" Get the host name and the IP address for the given host and return them as a tuple. Description of argument(s): host The host name or IP address to be obtained. short_name Include the short host name in the returned tuple, i.e. return host, ip and short_host. """ host = dft(host, socket.gethostname()) host_name = socket.getfqdn(host) try: host_ip = socket.gethostbyname(host) except socket.gaierror as my_gaierror: message = "Unable to obtain the host name for the following host:" +\ "\n" + gp.sprint_var(host) gp.print_error_report(message) raise my_gaierror if short_name: host_short_name = host_name.split(".")[0] return host_name, host_ip, host_short_name else: return host_name, host_ip
def set_mod_global(var_value, mod_name="__main__", var_name=None): r""" Set a global variable for a given module. Description of arguments: var_value The value to set in the variable. mod_name The name of the module whose variable is to be set. var_name The name of the variable to set. This defaults to the name of the variable used for var_value when calling this function. """ try: module = sys.modules[mod_name] except KeyError: gp.print_error_report("Programmer error - The mod_name passed to" + " this function is invalid:\n" + gp.sprint_var(mod_name)) raise ValueError('Programmer error.') if var_name is None: var_name = gp.get_arg_name(None, 1, 2) setattr(module, var_name, var_value)
def get_mod_global(var_name, default=None, mod_name="__main__"): r""" Get module global variable value and return it. If we are running in a robot environment, the behavior will default to calling get_variable_value. Description of arguments: var_name The name of the variable whose value is sought. default The value to return if the global does not exist. mod_name The name of the module containing the global variable. """ if robot_env: return BuiltIn().get_variable_value("${" + var_name + "}", default) try: module = sys.modules[mod_name] except KeyError: gp.print_error_report("Programmer error - The mod_name passed to" + " this function is invalid:\n" + gp.sprint_var(mod_name)) raise ValueError('Programmer error.') if default is None: return getattr(module, var_name) else: return getattr(module, var_name, default)
def which(file_path): r""" Find the full path of an executable file and return it. The PATH environment variable dictates the results of this function. Description of arguments: file_path The relative file path (e.g. "my_file" or "lib/my_file"). """ shell_rc, out_buf = gc.cmd_fnc_u("which " + file_path, quiet=1, print_output=0, show_err=0) if shell_rc != 0: error_message = "Failed to find complete path for file \"" +\ file_path + "\".\n" error_message += gp.sprint_var(shell_rc, 1) error_message += out_buf if robot_env: BuiltIn().fail(gp.sprint_error(error_message)) else: gp.print_error_report(error_message) return False file_path = out_buf.rstrip("\n") return file_path
def get_mod_global(var_name, default=None, mod_name="__main__"): r""" Get module global variable value and return it. If we are running in a robot environment, the behavior will default to calling get_variable_value. Description of arguments: var_name The name of the variable whose value is sought. default The value to return if the global does not exist. mod_name The name of the module containing the global variable. """ if robot_env: return BuiltIn().get_variable_value("${" + var_name + "}", default) try: module = sys.modules[mod_name] except KeyError: gp.print_error_report("Programmer error - The mod_name passed to" + " this function is invalid:\n" + gp.sprint_var(mod_name)) raise ValueError('Programmer error.') if default is None: return getattr(module, var_name) else: return getattr(module, var_name, default)
def set_mod_global(var_value, mod_name="__main__", var_name=None): r""" Set a global variable for a given module. Description of arguments: var_value The value to set in the variable. mod_name The name of the module whose variable is to be set. var_name The name of the variable to set. This defaults to the name of the variable used for var_value when calling this function. """ try: module = sys.modules[mod_name] except KeyError: gp.print_error_report("Programmer error - The mod_name passed to" + " this function is invalid:\n" + gp.sprint_var(mod_name)) raise ValueError('Programmer error.') if var_name is None: var_name = gp.get_arg_name(None, 1, 2) setattr(module, var_name, var_value)
def get_host_name_ip(host, short_name=0): r""" Get the host name and the IP address for the given host and return them as a tuple. Description of argument(s): host The host name or IP address to be obtained. short_name Include the short host name in the returned tuple, i.e. return host, ip and short_host. """ host_name = socket.getfqdn(host) try: host_ip = socket.gethostbyname(host) except socket.gaierror as my_gaierror: message = "Unable to obtain the host name for the following host:" +\ "\n" + gp.sprint_var(host) gp.print_error_report(message) raise my_gaierror if short_name: host_short_name = host_name.split(".")[0] return host_name, host_ip, host_short_name else: return host_name, host_ip
def which(file_path): r""" Find the full path of an executable file and return it. The PATH environment variable dictates the results of this function. Description of arguments: file_path The relative file path (e.g. "my_file" or "lib/my_file"). """ shell_rc, out_buf = gc.cmd_fnc_u("which " + file_path, quiet=1, print_output=0, show_err=0) if shell_rc != 0: error_message = "Failed to find complete path for file \"" +\ file_path + "\".\n" error_message += gp.sprint_var(shell_rc, 1) error_message += out_buf if robot_env: BuiltIn().fail(gp.sprint_error(error_message)) else: gp.print_error_report(error_message) return False file_path = out_buf.rstrip("\n") return file_path
def process_error_message(error_message): r""" Process the error_message in the manner described below. This function is designed solely for use by other functions in this file. NOTE: A blank error_message means that there is no error. For the following explanations, assume the caller of this function is a function with the following definition: valid_value(var_value, valid_values=[], invalid_values=[], *args, **kwargs): If the user of valid_value() is assigning the valid_value() return value to a variable, process_error_message() will simply return the error_message. This mode of usage is illustrated by the following example: error_message = valid_value(var1) This mode is useful for callers who wish to validate a variable and then decide for themselves what to do with the error_message (e.g. raise(error_message), BuiltIn().fail(error_message), etc.). If the user of valid_value() is NOT assigning the valid_value() return value to a variable, process_error_message() will behave as follows. First, if error_message is non-blank, it will be printed to stderr via a call to gp.print_error_report(error_message). If exit_on_error is set: - If the error_message is blank, simply return. - If the error_message is non-blank, exit the program with a return code of 1. If exit_on_error is NOT set: - If the error_message is blank, return True. - If the error_message is non-blank, return False. Description of argument(s): error_message An error message. """ # Determine whether the caller's caller is assigning the result to a # variable. l_value = gp.get_arg_name(None, -1, stack_frame_ix=3) if l_value: return error_message if error_message == "": if exit_on_error: return return True gp.print_error_report(error_message, stack_frame_ix=4) if exit_on_error: exit(1) return False
def valid_integer(var_value, var_name=""): r""" Return True if var_value is a valid integer. Otherwise, return False and print an error message to stderr. Description of arguments: (See description of arguments for svalid_value (above)). """ error_message = svalid_integer(var_value, var_name) if not error_message == "": gp.print_error_report(error_message) return False return True
def valid_integer(var_value, var_name=""): r""" Return True if var_value is a valid integer. Otherwise, return False and print an error message to stderr. Description of arguments: (See description of arguments for svalid_value (above)). """ error_message = svalid_integer(var_value, var_name) if not error_message == "": gp.print_error_report(error_message) return False return True
def t_shell_cmd(command_string, **kwargs): r""" Search upward in the the call stack to obtain the test_mode argument, add it to kwargs and then call shell_cmd and return the result. See shell_cmd prolog for details on all arguments. """ if 'test_mode' in kwargs: error_message = "Programmer error - test_mode is not a valid" +\ " argument to this function." gp.print_error_report(error_message) exit(1) test_mode = int(gp.get_stack_var('test_mode', 0)) kwargs['test_mode'] = test_mode return shell_cmd(command_string, **kwargs)
def required_plug_in(req_plug_in_names, plug_in_dir_paths=None): r""" Return True if each of the plug-ins in req_plug_in_names can be found in plug_in_dir_paths Otherwise, return False and print an error message to stderr. Example call: if not required_plug_in(['OS_Console'], AUTOBOOT_PLUG_IN_DIR_PATHS): return False Description of argument(s): (See Description of arguments for srequired_plug_in (above)). """ error_message = srequired_plug_in(req_plug_in_names, plug_in_dir_paths) if not error_message == "": gp.print_error_report(error_message) return False return True
def valid_value(var_value, invalid_values=[""], valid_values=[]): r""" Return True if var_value is a valid value. Otherwise, return False and print an error message to stderr. Description of arguments: var_value The value being validated. invalid_values A list of invalid values. If var_value is equal to any of these, it is invalid. Note that if you specify anything for invalid_values (below), the valid_values list is not even processed. valid_values A list of invalid values. var_value must be equal to one of these values to be considered valid. """ len_valid_values = len(valid_values) len_invalid_values = len(invalid_values) if len_valid_values > 0 and len_invalid_values > 0: gp.print_error_report("Programmer error - You must provide either an" + " invalid_values list or a valid_values" + " list but NOT both.") return False if len_valid_values > 0: # Processing the valid_values list. if var_value in valid_values: return True var_name = gp.get_arg_name(0, 1, 2) gp.print_error_report("The following variable has an invalid" + " value:\n" + gp.sprint_varx(var_name, var_value) + "\nIt must be one of the following values:\n" + gp.sprint_varx("valid_values", valid_values)) return False if len_invalid_values == 0: gp.print_error_report("Programmer error - You must provide either an" + " invalid_values list or a valid_values" + " list. Both are empty.") return False # Assertion: We have an invalid_values list. Processing it now. if var_value not in invalid_values: return True var_name = gp.get_arg_name(0, 1, 2) gp.print_error_report("The following variable has an invalid value:\n" + gp.sprint_varx(var_name, var_value) + "\nIt must" + " NOT be one of the following values:\n" + gp.sprint_varx("invalid_values", invalid_values)) return False
def get_host_name_ip(host): r""" Get the host name and the IP address for the given host and return them as a tuple. Description of argument(s): host The host name or IP address to be obtained. """ host_host_name = socket.getfqdn(host) try: host_ip = socket.gethostbyname(host) except socket.gaierror as my_gaierror: message = "Unable to obtain the host name for the following host:" +\ "\n" + gp.sprint_var(host) gp.print_error_report(message) raise my_gaierror return host_host_name, host_ip
def get_host_name_ip(host): r""" Get the host name and the IP address for the given host and return them as a tuple. Description of argument(s): host The host name or IP address to be obtained. """ host_host_name = socket.getfqdn(host) try: host_ip = socket.gethostbyname(host) except socket.gaierror as my_gaierror: message = "Unable to obtain the host name for the following host:" +\ "\n" + gp.sprint_var(host) gp.print_error_report(message) raise my_gaierror return host_host_name, host_ip
def required_plug_in(req_plug_in_names, plug_in_dir_paths=None): r""" Return True if each of the plug-ins in req_plug_in_names can be found in plug_in_dir_paths Otherwise, return False and print an error message to stderr. Example call: if not required_plug_in(['OS_Console'], AUTOBOOT_PLUG_IN_DIR_PATHS): return False Description of argument(s): (See Description of arguments for srequired_plug_in (above)). """ error_message = srequired_plug_in(req_plug_in_names, plug_in_dir_paths) if not error_message == "": gp.print_error_report(error_message) return False return True
def process_error_message(error_message): r""" Process the error_message as follows: - If the error_message is blank, return True. - If the error_message contains a value: - Print the error_message as part of a full error report. - If global exit_on_error is set, then exit the program with a return code of 1. - If exit_on_error is not set, return False. This function is designed solely for use by wrapper functions in this file (e.g. "valid_value"). Description of argument(s): error_message An error message. """ if error_message == "": return True gp.print_error_report(error_message) if exit_on_error: exit(0) return False
def validate_plug_in_package(plug_in_dir_path, mch_class="obmc"): r""" Validate the plug in package and return the normalized plug-in directory path. Description of arguments: plug_in_dir_path The "relative" or absolute path to a plug in package directory. mch_class The class of machine that we are testing (e.g. "op" = "open power", "obmc" = "open bmc", etc). """ gp.dprint_executing() if os.path.isabs(plug_in_dir_path): # plug_in_dir_path begins with a slash so it is an absolute path. candidate_plug_in_dir_path = os.path.normpath(plug_in_dir_path) +\ os.sep if not os.path.isdir(candidate_plug_in_dir_path): gp.print_error_report("Plug-in directory path \"" + plug_in_dir_path + "\" does not exist.\n") exit(1) else: # The plug_in_dir_path is actually a simple name (e.g. # "OBMC_Sample")... candidate_plug_in_dir_path = find_plug_in_package(plug_in_dir_path) if candidate_plug_in_dir_path == "": global PATH_LIST gp.print_error_report("Plug-in directory path \"" + plug_in_dir_path + "\" could not be found" + " in any of the following directories:\n" + gp.sprint_var(PATH_LIST)) exit(1) # Make sure that this plug-in supports us... supports_file_path = candidate_plug_in_dir_path + "supports_" + mch_class if not os.path.exists(supports_file_path): gp.print_error_report("The following file path could not be" + " found:\n" + gp.sprint_varx("supports_file_path", supports_file_path) + "\nThis file is necessary to indicate that" + " the given plug-in supports the class of" + " machine we are testing, namely \"" + mch_class + "\".\n") exit(1) return candidate_plug_in_dir_path
def validate_plug_in_package(plug_in_dir_path, mch_class="obmc"): r""" Validate the plug in package and return the normalized plug-in directory path. Description of arguments: plug_in_dir_path The "relative" or absolute path to a plug in package directory. mch_class The class of machine that we are testing (e.g. "op" = "open power", "obmc" = "open bmc", etc). """ gp.dprint_executing() if os.path.isabs(plug_in_dir_path): # plug_in_dir_path begins with a slash so it is an absolute path. candidate_plug_in_dir_path = os.path.normpath(plug_in_dir_path) +\ os.sep if not os.path.isdir(candidate_plug_in_dir_path): gp.print_error_report("Plug-in directory path \"" + plug_in_dir_path + "\" does not exist.\n") exit(1) else: # The plug_in_dir_path is actually a simple name (e.g. # "OBMC_Sample")... candidate_plug_in_dir_path = find_plug_in_package(plug_in_dir_path) if candidate_plug_in_dir_path == "": global PATH_LIST gp.print_error_report("Plug-in directory path \"" + plug_in_dir_path + "\" could not be found" + " in any of the following directories:\n" + gp.sprint_var(PATH_LIST)) exit(1) # Make sure that this plug-in supports us... supports_file_path = candidate_plug_in_dir_path + "supports_" + mch_class if not os.path.exists(supports_file_path): gp.print_error_report("The following file path could not be" + " found:\n" + gp.sprint_varx("supports_file_path", supports_file_path) + "\nThis file is necessary to indicate that" + " the given plug-in supports the class of" + " machine we are testing, namely \"" + mch_class + "\".\n") exit(1) return candidate_plug_in_dir_path
def cmd_fnc(cmd_buf, quiet=None, test_mode=None, debug=0, print_output=1, show_err=1): r""" Run the given command in a shell and return the shell return code. Description of arguments: cmd_buf The command string to be run in a shell. quiet Indicates whether this function should run the pissuing() function prints an "Issuing: <cmd string>" to stdout. test_mode If test_mode is set, this function will not actually run the command. debug If debug is set, this function will print extra debug info. print_output If this is set, this function will print the stdout/stderr generated by the shell command. show_err If show_err is set, this function will print a standardized error report if the shell command returns non-zero. """ quiet = int(gm.global_default(quiet, 0)) test_mode = int(gm.global_default(test_mode, 0)) if debug: gp.print_vars(cmd_buf, quiet, test_mode, debug) err_msg = gv.svalid_value(cmd_buf) if err_msg != "": raise ValueError(err_msg) if not quiet: gp.pissuing(cmd_buf, test_mode) if test_mode: return 0, "" sub_proc = subprocess.Popen(cmd_buf, bufsize=1, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) out_buf = "" for line in sub_proc.stdout: out_buf += line if not print_output: continue if robot_env: grp.rprint(line) else: sys.stdout.write(line) if print_output and not robot_env: sys.stdout.flush() sub_proc.communicate() shell_rc = sub_proc.returncode if shell_rc != 0 and show_err: if robot_env: grp.rprint_error_report("The prior command failed.\n" + gp.sprint_var(shell_rc, 1)) else: gp.print_error_report("The prior command failed.\n" + gp.sprint_var(shell_rc, 1)) return shell_rc, out_buf
def cmd_fnc(cmd_buf, quiet=None, test_mode=None, debug=0, print_output=1, show_err=1, return_stderr=0, ignore_err=1): r""" Run the given command in a shell and return the shell return code and the output. Description of arguments: cmd_buf The command string to be run in a shell. quiet Indicates whether this function should run the print_issuing() function which prints "Issuing: <cmd string>" to stdout. test_mode If test_mode is set, this function will not actually run the command. If print_output is set, it will print "(test_mode) Issuing: <cmd string>" to stdout. debug If debug is set, this function will print extra debug info. print_output If this is set, this function will print the stdout/stderr generated by the shell command. show_err If show_err is set, this function will print a standardized error report if the shell command returns non-zero. return_stderr If return_stderr is set, this function will process the stdout and stderr streams from the shell command separately. It will also return stderr in addition to the return code and the stdout. """ # Determine default values. quiet = int(gm.global_default(quiet, 0)) test_mode = int(gm.global_default(test_mode, 0)) if debug: gp.print_vars(cmd_buf, quiet, test_mode, debug) err_msg = gv.valid_value(cmd_buf) if err_msg != "": raise ValueError(err_msg) if not quiet: gp.pissuing(cmd_buf, test_mode) if test_mode: if return_stderr: return 0, "", "" else: return 0, "" if return_stderr: err_buf = "" stderr = subprocess.PIPE else: stderr = subprocess.STDOUT sub_proc = subprocess.Popen(cmd_buf, bufsize=1, shell=True, executable='/bin/bash', stdout=subprocess.PIPE, stderr=stderr) out_buf = "" if return_stderr: for line in sub_proc.stderr: try: err_buf += line except TypeError: line = line.decode("utf-8") err_buf += line if not print_output: continue gp.gp_print(line) for line in sub_proc.stdout: try: out_buf += line except TypeError: line = line.decode("utf-8") out_buf += line if not print_output: continue gp.gp_print(line) if print_output and not robot_env: sys.stdout.flush() sub_proc.communicate() shell_rc = sub_proc.returncode if shell_rc != 0: err_msg = "The prior shell command failed.\n" err_msg += gp.sprint_var(shell_rc, gp.hexa()) if not print_output: err_msg += "out_buf:\n" + out_buf if show_err: gp.print_error_report(err_msg) if not ignore_err: if robot_env: BuiltIn().fail(err_msg) else: raise ValueError(err_msg) if return_stderr: return shell_rc, out_buf, err_buf else: return shell_rc, out_buf
def cmd_fnc(cmd_buf, quiet=None, test_mode=None, debug=0, print_output=1, show_err=1): r""" Run the given command in a shell and return the shell return code. Description of arguments: cmd_buf The command string to be run in a shell. quiet Indicates whether this function should run the pissuing() function prints an "Issuing: <cmd string>" to stdout. test_mode If test_mode is set, this function will not actually run the command. debug If debug is set, this function will print extra debug info. print_output If this is set, this function will print the stdout/stderr generated by the shell command. show_err If show_err is set, this function will print a standardized error report if the shell command returns non-zero. """ quiet = int(gm.global_default(quiet, 0)) test_mode = int(gm.global_default(test_mode, 0)) if debug: gp.print_vars(cmd_buf, quiet, test_mode, debug) err_msg = gv.svalid_value(cmd_buf) if err_msg != "": raise ValueError(err_msg) if not quiet: gp.pissuing(cmd_buf, test_mode) if test_mode: return 0, "" sub_proc = subprocess.Popen(cmd_buf, bufsize=1, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) out_buf = "" for line in sub_proc.stdout: out_buf += line if not print_output: continue if robot_env: grp.rprint(line) else: sys.stdout.write(line) if print_output and not robot_env: sys.stdout.flush() sub_proc.communicate() shell_rc = sub_proc.returncode if shell_rc != 0 and show_err: if robot_env: grp.rprint_error_report("The prior command failed.\n" + gp.sprint_var(shell_rc, 1)) else: gp.print_error_report("The prior command failed.\n" + gp.sprint_var(shell_rc, 1)) return shell_rc, out_buf
def gen_get_options(parser, stock_list=[]): r""" Parse the command line arguments using the parser object passed and return True/False (i.e. pass/fail). Also set the following built in values: __builtin__.quiet This value is used by the qprint functions. __builtin__.test_mode This value is used by command processing functions. __builtin__.debug This value is used by the dprint functions. __builtin__.arg_obj This value is used by print_program_header, etc. __builtin__.parser This value is used by print_program_header, etc. Description of arguments: parser A parser object. See argparse module documentation for details. stock_list The caller can use this parameter to request certain stock parameters offered by this function. For example, this function will define a "quiet" option upon request. This includes stop help text and parm checking. The stock_list is a list of tuples each of which consists of an arg_name and a default value. Example: stock_list = [("test_mode", 0), ("quiet", 1), ("debug", 0)] """ # This is a list of stock parms that we support. master_stock_list = ["quiet", "test_mode", "debug", "loglevel"] # Process stock_list. for ix in range(0, len(stock_list)): if len(stock_list[ix]) < 1: gp.print_error_report("Programmer error - stock_list[" + str(ix) + "] is supposed to be a tuple containing at" + " least one element which is the name of" + " the desired stock parameter:\n" + gp.sprint_var(stock_list)) return False if type(stock_list[ix]) is tuple: arg_name = stock_list[ix][0] default = stock_list[ix][1] else: arg_name = stock_list[ix] default = None if arg_name not in master_stock_list: gp.pvar(arg_name) gp.print_error_report("Programmer error - \"" + arg_name + "\" not found found in stock list:\n" + gp.sprint_var(master_stock_list)) return False if arg_name == "quiet": if default is None: default = 0 parser.add_argument( '--quiet', default=default, type=int, choices=[1, 0], help='If this parameter is set to "1", %(prog)s' + ' will print only essential information, i.e. it will' + ' not echo parameters, echo commands, print the total' + ' run time, etc.' + default_string) elif arg_name == "test_mode": if default is None: default = 0 parser.add_argument( '--test_mode', default=default, type=int, choices=[1, 0], help='This means that %(prog)s should go through all the' + ' motions but not actually do anything substantial.' + ' This is mainly to be used by the developer of' + ' %(prog)s.' + default_string) elif arg_name == "debug": if default is None: default = 0 parser.add_argument( '--debug', default=default, type=int, choices=[1, 0], help='If this parameter is set to "1", %(prog)s will print' + ' additional debug information. This is mainly to be' + ' used by the developer of %(prog)s.' + default_string) elif arg_name == "loglevel": if default is None: default = "info" parser.add_argument( '--loglevel', default=default, type=str, choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL', 'debug', 'info', 'warning', 'error', 'critical'], help='If this parameter is set to "1", %(prog)s will print' + ' additional debug information. This is mainly to be' + ' used by the developer of %(prog)s.' + default_string) arg_obj = parser.parse_args() __builtin__.quiet = 0 __builtin__.test_mode = 0 __builtin__.debug = 0 __builtin__.loglevel = 'WARNING' for ix in range(0, len(stock_list)): if type(stock_list[ix]) is tuple: arg_name = stock_list[ix][0] default = stock_list[ix][1] else: arg_name = stock_list[ix] default = None if arg_name == "quiet": __builtin__.quiet = arg_obj.quiet elif arg_name == "test_mode": __builtin__.test_mode = arg_obj.test_mode elif arg_name == "debug": __builtin__.debug = arg_obj.debug elif arg_name == "loglevel": __builtin__.loglevel = arg_obj.loglevel __builtin__.arg_obj = arg_obj __builtin__.parser = parser # For each command line parameter, create a corresponding global variable # and assign it the appropriate value. For example, if the command line # contained "--last_name='Smith', we'll create a global variable named # "last_name" with the value "Smith". module = sys.modules['__main__'] for key in arg_obj.__dict__: setattr(module, key, getattr(__builtin__.arg_obj, key)) return True
def gen_get_options(parser, stock_list=[]): r""" Parse the command line arguments using the parser object passed and return True/False (i.e. pass/fail). Also set the following built in values: __builtin__.quiet This value is used by the qprint functions. __builtin__.test_mode This value is used by command processing functions. __builtin__.debug This value is used by the dprint functions. __builtin__.arg_obj This value is used by print_program_header, etc. __builtin__.parser This value is used by print_program_header, etc. Description of arguments: parser A parser object. See argparse module documentation for details. stock_list The caller can use this parameter to request certain stock parameters offered by this function. For example, this function will define a "quiet" option upon request. This includes stop help text and parm checking. The stock_list is a list of tuples each of which consists of an arg_name and a default value. Example: stock_list = [("test_mode", 0), ("quiet", 1), ("debug", 0)] """ # This is a list of stock parms that we support. master_stock_list = ["quiet", "test_mode", "debug", "loglevel"] # Process stock_list. for ix in range(0, len(stock_list)): if len(stock_list[ix]) < 1: gp.print_error_report("Programmer error - stock_list[" + str(ix) + "] is supposed to be a tuple containing at" + " least one element which is the name of" + " the desired stock parameter:\n" + gp.sprint_var(stock_list)) return False if type(stock_list[ix]) is tuple: arg_name = stock_list[ix][0] default = stock_list[ix][1] else: arg_name = stock_list[ix] default = None if arg_name not in master_stock_list: gp.pvar(arg_name) gp.print_error_report("Programmer error - \"" + arg_name + "\" not found found in stock list:\n" + gp.sprint_var(master_stock_list)) return False if arg_name == "quiet": if default is None: default = 0 parser.add_argument( '--quiet', default=default, type=int, choices=[1, 0], help='If this parameter is set to "1", %(prog)s' + ' will print only essential information, i.e. it will' + ' not echo parameters, echo commands, print the total' + ' run time, etc.' + default_string) elif arg_name == "test_mode": if default is None: default = 0 parser.add_argument( '--test_mode', default=default, type=int, choices=[1, 0], help='This means that %(prog)s should go through all the' + ' motions but not actually do anything substantial.' + ' This is mainly to be used by the developer of' + ' %(prog)s.' + default_string) elif arg_name == "debug": if default is None: default = 0 parser.add_argument( '--debug', default=default, type=int, choices=[1, 0], help='If this parameter is set to "1", %(prog)s will print' + ' additional debug information. This is mainly to be' + ' used by the developer of %(prog)s.' + default_string) elif arg_name == "loglevel": if default is None: default = "info" parser.add_argument( '--loglevel', default=default, type=str, choices=[ 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL', 'debug', 'info', 'warning', 'error', 'critical' ], help='If this parameter is set to "1", %(prog)s will print' + ' additional debug information. This is mainly to be' + ' used by the developer of %(prog)s.' + default_string) arg_obj = parser.parse_args() __builtin__.quiet = 0 __builtin__.test_mode = 0 __builtin__.debug = 0 __builtin__.loglevel = 'WARNING' for ix in range(0, len(stock_list)): if type(stock_list[ix]) is tuple: arg_name = stock_list[ix][0] default = stock_list[ix][1] else: arg_name = stock_list[ix] default = None if arg_name == "quiet": __builtin__.quiet = arg_obj.quiet elif arg_name == "test_mode": __builtin__.test_mode = arg_obj.test_mode elif arg_name == "debug": __builtin__.debug = arg_obj.debug elif arg_name == "loglevel": __builtin__.loglevel = arg_obj.loglevel __builtin__.arg_obj = arg_obj __builtin__.parser = parser # For each command line parameter, create a corresponding global variable # and assign it the appropriate value. For example, if the command line # contained "--last_name='Smith', we'll create a global variable named # "last_name" with the value "Smith". module = sys.modules['__main__'] for key in arg_obj.__dict__: setattr(module, key, getattr(__builtin__.arg_obj, key)) return True
def setup(): r""" Do general program setup tasks. """ global cp_setup_called global transitional_boot_selected gp.qprintn() set_default_siguser1() transitional_boot_selected = False robot_pgm_dir_path = os.path.dirname(__file__) + os.sep repo_bin_path = robot_pgm_dir_path.replace("/lib/", "/bin/") # If we can't find process_plug_in_packages.py, ssh_pw or # validate_plug_ins.py, then we don't have our repo bin in PATH. shell_rc, out_buf = gc.cmd_fnc_u("which process_plug_in_packages.py" + " ssh_pw validate_plug_ins.py", quiet=1, print_output=0, show_err=0) if shell_rc != 0: os.environ['PATH'] = repo_bin_path + ":" + os.environ.get('PATH', "") # Likewise, our repo lib subdir needs to be in sys.path and PYTHONPATH. if robot_pgm_dir_path not in sys.path: sys.path.append(robot_pgm_dir_path) PYTHONPATH = os.environ.get("PYTHONPATH", "") if PYTHONPATH == "": os.environ['PYTHONPATH'] = robot_pgm_dir_path else: os.environ['PYTHONPATH'] = robot_pgm_dir_path + ":" + PYTHONPATH validate_parms() gp.qprint_pgm_header() grk.run_key("Set BMC Power Policy ALWAYS_POWER_OFF") initial_plug_in_setup() plug_in_setup() rc, shell_rc, failed_plug_in_name = grpi.rprocess_plug_in_packages( call_point='setup') if rc != 0: error_message = "Plug-in setup failed.\n" gp.print_error_report(error_message) BuiltIn().fail(error_message) # Setting cp_setup_called lets our Teardown know that it needs to call # the cleanup plug-in call point. cp_setup_called = 1 # Keyword "FFDC" will fail if TEST_MESSAGE is not set. BuiltIn().set_global_variable("${TEST_MESSAGE}", "${EMPTY}") # FFDC_LOG_PATH is used by "FFDC" keyword. BuiltIn().set_global_variable("${FFDC_LOG_PATH}", ffdc_dir_path) # Also printed by FFDC. global host_name global host_ip host = socket.gethostname() host_name, host_ip = gm.get_host_name_ip(host) gp.dprint_var(boot_table) gp.dprint_var(boot_lists)