#!python3 ''' Created on Sep 21, 20-16 author: tpweis file name: test_start.py Input arguments feeddir - Directory that supplies the package files to feed the testing workorder - specifies the workorder name that is placed in the manifest. interval - the time spacing between each copy action copylimit - the number of times the contents of feeddir are copied to targetdir. A value of 0 means no limit to the repeated copies. Output arguments targetdir - Location to place package and manifest files A manifest file is created for each package that is copied to the target directory. ''' import os, time, datetime import subprocess import sys import shutil import json def helpmessage(): "function helpmessage" print("test_start.py ") sys.exit(1) def writemanifest(fullname, workorder, targetdir): "function writemanifest" filename, file_ext = os.path.splitext(fullname) manifest = dict() originator = dict() packages = dict() originator["ipAddress"] = "127.0.0.1" originator["jobId"] = filename packages["package_name"] = filename packages["files"] = [fullname] manifest["data"] = {} createtime = datetime.datetime.now().isoformat() manifest["dateCreated"] = createtime manifest["jobType"] = "loan" manifest["originator"] = originator manifest["packages"] = [packages] manifest["type"] = "SNT_JOB" manifest["workOrderName"] = workorder # Save the manifest and return its name newname = os.path.normpath(targetdir + "/" +filename + ".json") print(createtime + " manifest: " + newname) f = open(newname, 'w') json.dump(manifest, f, sort_keys=True, indent=4) f.close() # We added the source_dir to the manifest file open, to ensure it is written in # the correct location. However, subsequent processing of the manifest file will # control the directories. Thuse, return only the basename. return filename + ".json" def main(): "function main" arglist = sys.argv[1:] if(len(arglist)< 1): print("Missing Feed Directory argument. Try again") helpmessage() else: feeddir = arglist[0] if(len(arglist)< 2): print("Missing Workorder name argument. Try again") helpmessage() else: workorder = arglist[1] if(len(arglist) < 3): print("Missing copy interval parameter (seconds between copies). Try again") helpmessage() else: interval =int(arglist[2]) if interval < 5 : print("Minimum interval is 5 seconds") interval = 5 elif interval > 1800: print("Maximum interval is 1800 seconds (30 minutes)") interval = 1800 if(len(arglist)< 4): print("Missing copy count argument. Try again") helpmessage() else: copylimit = int(arglist[3]) if(len(arglist) < 5): print("Missing Target directory argument. Try again") helpmessage() else: targetdir =arglist[4] # Now ensure that both directories exist if not os.path.isdir(feeddir): print("First arg (Feed directory) " + feeddir + " is not a valid directory") helpmessage() if not os.path.isdir(targetdir): print("Target directory " + targetdir + " is not a valid directory") helpmessage() print("Copy Package files from " + feeddir + " to " + targetdir + ", one every " + str(interval) + " seconds") feedlist = dict ([(f, None) for f in os.listdir (feeddir)]) # Only proceed if the feedlist is none empty. If it is non-empty, but has no .pdf files in it, # we will find that out later if not feedlist: print("Feed directory is empty. Try again") helpmessage() copyctr = 0 while copylimit == 0 or copyctr < copylimit: copyctr += 1 print("Make copy # " + str(copyctr) +", limit = " + str(copylimit)) # No umlimited copies and we havent exceeded the copy limit yet for sourcefile in feedlist: try: print("examine name contents: " + sourcefile) file_name, sep, file_ext = sourcefile.rpartition('.') print("File extension is " + file_ext) except: print("Could not get a valid filename from " + sourcefile) helpmessage() # We are only copying .pdf files. Check now if file_ext == "pdf": # begin the copying # fiddle with the file name so that we can produce numbered copies oldfile = os.path.normpath(feeddir + "/" + sourcefile) newname = file_name + str(copyctr) + sep + file_ext newfile = os.path.normpath(targetdir + "/" + newname) print(oldfile + " to " + newfile) shutil.copy(oldfile,newfile) # Now generate the manifest file manifest = writemanifest(newname, workorder, targetdir) time.sleep(interval) sys.exit(0) if __name__ == '__main__': # execute only if run as a script main()