diff --git a/tsshbatch.py b/tsshbatch.py index 0bb906d..d5f4371 100755 --- a/tsshbatch.py +++ b/tsshbatch.py @@ -100,7 +100,7 @@ SUDOPROMPT = 'READINGSUDOPW' SUDOARGS = '-S -p %s' % SUDOPROMPT SUDOPWHINT = '(Default: login password): ' -SYMTABLE = 'Symbol Table' +SYMTABLE = 'Global Symbol Table' TESTRUN = 'Test Run For' TRAILER = ': ' USERVAR = 'USER' @@ -151,6 +151,7 @@ ASSIGN = '=' DEFINE = '.define' INCLUDE = '.include' +LOCAL = '.local' NOTIFY = '.notify' @@ -252,7 +253,7 @@ Get_Transfer_List = collections.OrderedDict() Put_Transfer_List = collections.OrderedDict() SSH_Configuration = {} -SymbolTable = {} +GlobalSymbolTable = {} ##### @@ -295,6 +296,33 @@ # End of 'FindFilesOnPath()' +#### +# Get An Execution Variable Value +### + +def GetExecutionVariable(cmd): + + + try: + origcmd = cmd + status, cmd = commands.getstatusoutput(cmd.strip()) + + # Blow out if the command failed + + if status: + raise + + except: + + PrintReport([PROGNAME, eBADEXEC % origcmd], HANDLER=PrintStderr) + ErrorExit("") + + # Return successfully + return cmd + +# End of 'GetExecutionVariable()' + + ##### # Check To See If A Key Exists In A String, Excluding Quoted Substrings ##### @@ -639,6 +667,8 @@ def ReadFile(fname, envvar, listcontainer, containingfile=""): + LocalSymbolTable = {} + # Check to see if we can find the file, searching the # the relevant include environment variable path, if any @@ -674,7 +704,32 @@ val = line.split(FILEPUT)[1].strip() ProcessTXRQ(val, Put_Transfer_List) - # Process variable definitions + # Process local variable definitions + + elif line.startswith(LOCAL): + + line = line.split(LOCAL)[1] + if line.count(ASSIGN) == 0: + ErrorExit(eBADDEFINE % line) + + else: + + name = line.split(ASSIGN)[0].strip() + val = "=".join(line.split(ASSIGN)[1:]).strip() + + if name: + + # Process references to execution variables + + if val.startswith(EXECUTE): + val = GetExecutionVariable(val[1:]) + + LocalSymbolTable[name] = val + + else: + ErrorExit(eBADDEFINE % line) + + # Process global variable definitions elif line.startswith(DEFINE): @@ -687,27 +742,15 @@ name = line.split(ASSIGN)[0].strip() val = "=".join(line.split(ASSIGN)[1:]).strip() - # Process references to execution variables - - if val.startswith(EXECUTE): - - try: - - origval = val - status, val = commands.getstatusoutput(val[1:].strip()) - - # Blow out if the command failed - - if status: - raise - - except: - - PrintReport([PROGNAME, eBADEXEC % origval], HANDLER=PrintStderr) - ErrorExit("") - if name: - SymbolTable[name] = val + + # Process references to execution variables + + if val.startswith(EXECUTE): + val = GetExecutionVariable(val[1:]) + + + GlobalSymbolTable[name] = val else: ErrorExit(eBADDEFINE % line) @@ -718,9 +761,9 @@ fname = VarSub(ConditionLine(line.split(INCLUDE)[1])) ReadFile(fname, envvar, listcontainer, containingfile=filename) - # It's a normal line - do variable substitution and save + # It's a normal line - do local variable substitution and save else: - listcontainer.append(line) + listcontainer.append(VarSub(line, Table=LocalSymbolTable)) f.close() FileIncludeStack.pop() # Remove this invocation from the stack @@ -838,10 +881,10 @@ # Do Variable Substitution In A String ##### -def VarSub(line): +def VarSub(line, Table=GlobalSymbolTable): - for symbol in SymbolTable: - line = line.replace(symbol, SymbolTable[symbol]) + for symbol in Table: + line = line.replace(symbol, Table[symbol]) return line @@ -1150,10 +1193,10 @@ # Unroll and format dictionary structures - symbols = SymbolTable.keys() + symbols = GlobalSymbolTable.keys() symbols.sort() for symbol in symbols: - symtbl.append(symbol + (PADWIDTH - len(symbol)) * " "+ SEPARATOR + SymbolTable[symbol]) + symtbl.append(symbol + (PADWIDTH - len(symbol)) * " "+ SEPARATOR + GlobalSymbolTable[symbol]) for xfers, unrolled in ((Get_Transfer_List, gets), (Put_Transfer_List, puts)): @@ -1215,7 +1258,7 @@ protected = [] for builtin in BuiltIns: - if builtin in SymbolTable: + if builtin in GlobalSymbolTable: protected.append(builtin) # Now iterate over requested hosts @@ -1252,7 +1295,7 @@ for symbol, value in internals: if symbol not in protected: - SymbolTable[symbol] = value + GlobalSymbolTable[symbol] = value # Apply any relevant variable dereferencing