From a36ddc28bded0507d90d0ffc9d0a04831d13f47f Mon Sep 17 00:00:00 2001 From: Shwetha-H-Panduranga Date: Tue, 20 Dec 2011 18:04:44 +0530 Subject: Changes made to '_substitute_value_for_variables' function. The function now handles substitution of variable values. (Ex:- replaces volumename of volume1. The variable value is the value defined in testenv.cfg for that testunit). The function returns commands wiith values substituted for variables. The function 'execute_command' now returns 'output'(dictionary output from ssh.executecommand) --- libs/utils/hostutils.py | 93 +++++++++++++++++++++++++++++-------------------- 1 file changed, 56 insertions(+), 37 deletions(-) diff --git a/libs/utils/hostutils.py b/libs/utils/hostutils.py index 9992bb6..32741ea 100644 --- a/libs/utils/hostutils.py +++ b/libs/utils/hostutils.py @@ -10,6 +10,7 @@ Supported Wrappers: """ import re +from collections import namedtuple import atfutils from atfglobals import GlobalObj import pdb @@ -147,63 +148,81 @@ def execute_command(hostkey, command, commandInput=None): logger.error("SSH Connection Not established to host '%s' " % hostkey) return 1 - new_command = _substitute_value_for_variables(hostkey, command) + new_command = _substitute_value_for_variables(command) logger.debug('%s: Executing Command: %s' % (hostkey, command)) output = host_connection.executecommand(new_command, commandInput) - return_status = atfutils.assert_success(**output) atfutils.print_stdout(output['stdoutdata']) atfutils.print_stderr(output['stderrdata']) - return return_status + return output -def _substitute_value_for_variables(hostkey, command): +def _substitute_value_for_variables(command): """ """ logger = GlobalObj.getLoggerObj() - pattern_for_variables = re.compile("<[a-z]+\d*>") - pattern_for_hosts = re.compile('(server|client|master)*') - variables_to_replace = [] - replace_values = {} + env = GlobalObj.getTestenvObj() new_command = command - Functions_Map = { - "server" : "getserver", - "client" : "getclient", - "master" : "getmaster" - } + pattern_for_variables = re.compile("<[a-z]+\d*\.[a-z]*>") + name_to_functions_mapping = { + "export" : "getExportdir", + "server" : "getServer", + "brick" : "getBrick", + "volume" : "getVolume", + "client" : "getClient", + "mountdevice" : "getMountDevice", + "mount" : "getMount"} + name_pattern = re.compile('(export|server|brick|volume|client|mountdevice|mount)*') + + variable_to_replace_named_tuple = namedtuple("Vars", ["actual_var", + "name", + "option"]) + variables_to_replace = [] + variables = pattern_for_variables.findall(command) - host = None - if not variables: return new_command else: - result = pattern_for_hosts.match(hostkey.lower()).group(0) - if result: - funcname = Functions_Map[result] - function = getattr(env, funcname) - if re.match("master", result): - host = function() - else: - host = function(hostkey) - - if not host: - logger.error("No Host to execute the command\n") - return 1 + active_volume = env.getActiveVolume() for variable in variables: if variable not in variables_to_replace: - variables_to_replace.append(variable.strip("<>")) - + name, option = (variable.strip("<>")).split(".") + variables_to_replace.append(variable_to_replace_named_tuple(variable, + name, + option)) for variable in variables_to_replace: - value = host.__getattribute__(variable) - replace_values[variable] = value - - for key in replace_values.keys(): - value = replace_values[key] - key = "<" + key + ">" - pattern = re.compile(key) - new_command = pattern.sub(value, new_command) + actual_var = variable.actual_var + name = variable.name + option = variable.option + matched_obj = name_pattern.match(name.lower()) + result = matched_obj.group(0) + if result is '': + continue + else: + funcname = name_to_functions_mapping[result] + function = getattr(env, funcname) + obj_value = function(name) + if obj_value is None: + continue + else: + try: + option_value = obj_value.__getattribute__(option) + + except AttributeError: + logger.error("Attribute Error: %s object has no attribute '%s'" % + (name, option)) + continue + else: + actual_var_pattern = re.compile(actual_var) + new_command = actual_var_pattern.sub(option_value, + new_command) + + variables = pattern_for_variables.findall(command) + if not variables: + new_command = "" + return new_command return new_command -- cgit