#!python3 ''' Created on Sep 21, 20-16 author: tpweis file name: run_ts.py Run a test suite. Read a spreadsheet that defines the test suite Loop through the rows to initiate each test Input arguments testsuite - Xcel file that contains the definition of the test suite Output arguments 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 import logging import openpyxl def helpmessage(): "function helpmessage" print("Command string: run_ts.py ") logging.warning('Test Suite initiation or run-time error, msg sent to console') logging.warning('Halt') 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") logging.info("Generate 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" logfile = os.path.normpath("/sl/bin/test.log") logging.basicConfig(format='%(asctime)s: %(module)s: %(levelname)s: %(message)s', filename=logfile, level=logging.DEBUG) arglist = sys.argv[1:] if(len(arglist)< 1): print("Missing Test Suite file. Try again") helpmessage() else: testsuite = arglist[0] # Now ensure that both directories exist if not os.path.isfile(testsuite): print("Bad first argument (testsuite spreadsheet) " + testsuite + " is not a valid file") helpmessage() basename = os.path.basename(testsuite) if not basename: # should never get here, because we passed the isfile() test. Let's be careful anyway print("Bad file name for testsuite: " + testsuite) # Let's check the extension filename, file_ext = os.path.splitext(basename) if file_ext != ".xlsx": print("Bad testsuite file type, must be .xlsx: " + file_ext) helpmessage() # open and read the testsuite spreadsheet/worksheet try: wb = openpyxl.load_workbook(testsuite, read_only=True) except: print("Could not open spreadsheet " + testsuite) helpmessage() sheet = wb.active rows = sheet.rows header = [cell.value for cell in next(rows)] logstr = "| " + header[0] + " | " + header[1] + " | " + header[2] + " | " + header[3] \ + " | " + header[4] + " | " + header[5] + " |" logging.info("Spreadsheet header row:") logging.info(logstr) test_table = [] for row in rows: record = {} for key, cell in zip(header, row): if cell.data_type == 's': record[key] = cell.value.strip() else: record[key] = cell.value test_table.append(record) # Proceed if the testsuite is not empty. if len(test_table) <= 0: print("Testsuite is empty. Try again") helpmessage() # Iterate through the test_table, one row at a time. for thistest in test_table: # validate source and target directories testname = thistest["Test Name"] sourcedir = thistest["Source Directory"] targetdir = thistest["Target Directory"] copylimit = thistest["Number of Copies"] workorder = thistest["Workorder Name"] interval = thistest["Copy Interval"] execute = thistest["Execute"] if execute.upper() != "YES": logging.info("Skip test *************** " + testname + "***************") continue logging.info("Execute test *************** " + testname + "***************") logging.info(workorder + " | " + sourcedir + " | " + str(copylimit) + " | " \ + targetdir + " | " + str(interval) + " | " + execute) if not os.path.isdir(sourcedir): print("First arg (Feed directory) " + sourcedir + " is not a valid directory") helpmessage() if not os.path.isdir(targetdir): print("Target directory " + targetdir + " is not a valid directory") helpmessage() copyctr = 0 feedlist = dict ([(f, None) for f in os.listdir (sourcedir)]) while copyctr < copylimit: copyctr += 1 logging.info("Make copy # " + str(copyctr) +", limit = " + str(copylimit)) for sourcefile in feedlist: try: file_name, sep, file_ext = sourcefile.rpartition('.') except: logging.warning("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(sourcedir + "/" + sourcefile) newname = testname + "_" + file_name + str(copyctr) + sep + file_ext newfile = os.path.normpath(targetdir + "/" + newname) logging.info("Copy " + 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()