diff --git a/tsshbatch.py b/tsshbatch.py index f6fcdbe..12cbace 100755 --- a/tsshbatch.py +++ b/tsshbatch.py @@ -17,9 +17,11 @@ PROGNAME = "tsshbatch.py" BASENAME = PROGNAME.split(".py")[0] PROGENV = BASENAME.upper() -CVSID = "$Id: tsshbatch.py,v 1.163 2013/10/28 13:19:16 tundra Exp $" -VERSION = CVSID.split()[2] +CMDINCL = PROGENV + "CMDS" +HOSTINCL = PROGENV + "HOSTS" +CVSID = "$Id: tsshbatch.py,v 1.164 2013/10/28 22:44:46 tundra Exp $" +VERSION = CVSID.split()[2] CPRT = "(c)" DATE = "2011-2013" OWNER = "TundraWare Inc." @@ -68,8 +70,9 @@ HOSTNOISE = '[%s]' HOSTLIST = 'Hosts' INDENTWIDTH = 8 -OPTIONSLIST = 'G:H:NP:Sef:hkn:p:tyv' +OPTIONSLIST = 'G:H:NP:Sef:hkn:p:tvxy' PADWIDTH = 12 +PATHDELIM = ':' PATHSEP = os.sep PUTFILES = 'Files To PUT' SEPARATOR = ' ---> ' @@ -81,6 +84,7 @@ SYMTABLE = 'Symbol Table' TESTRUN = 'Test Run For' TRAILER = ': ' +USERVAR = 'USER' USAGE = \ PROGVER + "\n" +\ @@ -101,8 +105,10 @@ " -p pw Specify login password\n" +\ " -t Run in test mode, don't actually execute commands\n" +\ " -v Display extended program version information\n" +\ + " -x Turn off test mode (if on) and execute requests\n" +\ " -y Turn on 'noisy' reporting for additional detail\n" + ##### # Directives & Related Support ##### @@ -399,11 +405,21 @@ # Read File Handling Comments And Directives ##### -def ReadFile(filename, listcontainer, containingfile=""): +def ReadFile(fname, envvar, listcontainer, containingfile=""): + + # Check to see if we can find the file, searching the + # the relevant include environment variable path, if any + + matches = tsearchpath(fname, envvar) + if not matches: + ErrorExit(eBADFILE % fname) + + # We always attempt to open the first match found in the path + else: + filename = matches[0] # Make sure we don't have a cyclic include reference - filename = os.path.realpath(filename) if filename in FileIncludeStack: ErrorExit(eINCLUDECYCLE % containingfile + SEPARATOR + filename) @@ -411,6 +427,7 @@ FileIncludeStack.append(filename) # Push it on to the stack history try: + f = open(filename) for line in f.readlines(): @@ -441,7 +458,7 @@ elif line: if line.startswith(INCLUDE): fname = ConditionLine(line.split(INCLUDE)[1]) - ReadFile(fname, listcontainer, containingfile=filename) + ReadFile(fname, envvar, listcontainer, containingfile=filename) else: @@ -459,6 +476,39 @@ # End of 'ReadFile()' +##### +# Search A Path For A File, Returning All Possible Matches +##### + +def tsearchpath(filename, pathlist, delimiter=PATHDELIM): + + # What we'll return if we find nothing + + retval = [] + + # Handle fully qualified filenames + + if os.path.exists(filename): + retval.append(os.path.realpath(filename)) + + # Find all instances of filename in specified paths + + paths = pathlist.split(delimiter) + for path in paths: + + if path and path[-1] != PATHSEP: + path += PATHSEP + + path += filename + + if os.path.exists(path): + retval.append(os.path.realpath(path)) + + return retval + +# End of 'tsearchpath()' + + # ---------------------- Program Entry Point ---------------------- # ##### @@ -503,7 +553,7 @@ REPORTERR = False if opt == "-f": - Commands = ReadFile(val, Commands) + Commands = ReadFile(val, os.getenv(CMDINCL), Commands) if opt == "-h": PrintStdout(USAGE) @@ -525,6 +575,9 @@ PrintStdout(CVSID) sys.exit() + if opt == "-x": + TESTMODE = False + if opt == "-y": NOISY = True @@ -547,7 +600,7 @@ if not args: ErrorExit(eNOHOSTS) - Hosts = ReadFile(args[0], Hosts) + Hosts = ReadFile(args[0], os.getenv(HOSTINCL), Hosts) command = " ".join(args[1:]) # If hosts were passed on the command line, all the arguments @@ -581,7 +634,7 @@ # Preset commandline and/or program option variable username takes precedence if not UNAME: - UNAME = os.getenv("USER") + UNAME = os.getenv(USERVAR) # By default, use the above as the login name and don't prompt for it # unless overriden on the command line with -N @@ -669,7 +722,7 @@ for dest in xfers[source]: unrolled.append(source + (PADWIDTH*3 - len(source)) * " "+ SEPARATOR + dest) - for prompt, description, items in ((TESTRUN, " ".join(sys.argv), ["\n"]), + for prompt, description, items in ((TESTRUN, " ".join(OPTIONS), ["\n"]), (SYMTABLE, "", symtbl + ["\n"]), (HOSTLIST, "", Hosts + ["\n"]), (GETFILES, "", gets + ["\n"]),