import os import sys import xml.etree.ElementTree as ET import os.path import time import argparse import subprocess import email import smtplib import shutil import re import subprocess import distutils.core import threading import zipfile import datetime argParser = argparse.ArgumentParser(description="Set the filename for dumping the stats.") argParser.add_argument( '--gearStudioPath', type=str, default='', help="Set the path for gear studio.") argParser.add_argument( '--dumpStatsFileName', type=str, default='stats.txt', help="Set the filename for dumping the stats.") argParser.add_argument( '--outputPath', type=str, default='', help="Set the path of the outputPath.") args = argParser.parse_args() # files management STATISTICS_FILE = args.dumpStatsFileName GEAR_STUDIO_PATH = args.gearStudioPath GEAR_STUDIO_OUTPUT_PATH= args.outputPath NETWORK_PATH_SHP = "\\\\ubisoft.org\\projects\\TechGroup\\Gear\\GearStudio\\Test SHPs\\" NETWORK_PATH_SHP2 = "\\\\ubisoft.org\\projects\\TechGroup\\Gear\\GearStudio\\Test SHPs\\Pilot\\" NETWORK_PATH_SHP3 = "\\\\ubisoft.org\\projects\\TechGroup\\Gear\\GearStudio\\Test SHPs\\R6\\" NETWORK_PATH_SHP4 = "\\\\ubisoft.org\\projects\\TechGroup\\Gear\\GearStudio\\Test SHPs\\R6\\JPMorel\\" NETWORK_PATH_PEL = "\\\\ubisoft.org\\projects\\TechGroup\\Gear\\GearStudio\\Test PELs\\" NETWORK_PATH_FAP = "\\\\ubisoft.org\\projects\\TechGroup\\Gear\\GearStudio\\Test FAPs\\" TESTFILE=[] EXCEPTION_FILE=[ "2010.04.09-21.28.34_gear_tests.shp", "freeze.shp", "freeze2.shp", "nick2.shp", "paul.shp", "Profile_Moving_v547_release_Freeze - Copy.shp", "RemoteProfile.shp", "withDeps.shp", "ACVI_GEN_NORMAL.pel2", "2014.05.27-09.23.59_Scimitar_238_EngineProfile_MTL-WKS-BA914.shp", "Bloomberg.log", "773_village_idle_look_front.shp" ] def UnzipGearStudio(): #distutils.dir_util.copy_tree(GEAR_STUDIO_OUTPUT_PATH,os.getcwd()) with zipfile.ZipFile(GEAR_STUDIO_OUTPUT_PATH+'gear_studio.zip', "r") as z: z.extractall(args.gearStudioPath+'studio/bin/x64/Release/') def LaunchTest(path): print("Beginning of the dump_stats script") result="" indexFileArray = 0 index = 0 TESTFILE=os.listdir(path) print("test length of array"+str(len(TESTFILE))) while (indexFileArray != len(TESTFILE)): print("time"+str(datetime.datetime.now())) print("testing current file = "+TESTFILE[indexFileArray]) if (str(TESTFILE[indexFileArray]).find(".")!=-1): if (not str(TESTFILE[indexFileArray]) in EXCEPTION_FILE): LaunchGearStudio(path+TESTFILE[indexFileArray]) result=result+str(ReadAndValidateStats(path+TESTFILE[indexFileArray])) indexFileArray = indexFileArray+1 else: #we skeep these files because those aren't loading in gear studio because there's an pop-up error that avoid #us to load them properly. So to avoid to block this unit test, we just skip them. print(str(TESTFILE[indexFileArray])+" has been skipped.") indexFileArray = indexFileArray+1 else: print(str(TESTFILE[indexFileArray])+" has been skipped since it's a folder.") indexFileArray = indexFileArray+1 print("End of the dump_stats script") return result def LaunchGearStudio(file): print("Copying and executing Gear Studio") if (os.path.isfile(args.gearStudioPath+'studio/bin/x64/Release/GearStudio.exe')): print("GearStudio.exe found") else: print("GearStudio.exe not found") if (os.path.exists(args.gearStudioPath+'studio/bin/x64/Release/'+STATISTICS_FILE)): os.remove(args.gearStudioPath+'studio/bin/x64/Release/'+STATISTICS_FILE) subprocess.call([args.gearStudioPath+'studio/bin/x64/Release/GearStudio.exe',['"'+ file+'"'],'--bloomless','--report_on_load','--select_all',['--dump_stats='+args.gearStudioPath+'studio/bin/x64/Release/'+STATISTICS_FILE]]) def SendEmail(emailReport): # Import the email modules we'll need from email.mime.text import MIMEText # Create a text/plain message message = emailReport msg = MIMEText(message) msg['Subject'] = 'Gear Studio : Negative values or over percent values have been found' msg['From'] = "GearStudioStatsBot" # Send the message via our own SMTP server, but don't include the # envelope header. s = smtplib.SMTP('relay.ubisoft.org', 25) s.sendmail("MTL-TGGearStudioTeam@ubisoft.com", ['Jean-Francois.Lemay@ubisoft.com','cyrille.gauclin@ubisoft.com','jean-francois.richard@ubisoft.com','jean-francois.tremblay@ubisoft.com','maxime.godin@ubisoft.com','yannick.cholette@ubisoft.com'], msg.as_string()) #s.sendmail("MTL-TGGearStudioTeam@ubisoft.com", 'Jean-Francois.Lemay@ubisoft.com', msg.as_string()) s.quit() def ValidatePercentage(stat): if (stat.find("%")!=-1): stat = stat.replace("%","") if (float(stat) > 100): print("over percentage found="+str(stat)) return True else: return False def ValidateNotNegative(stat): if (stat != "-"): stat.replace("us","") stat.replace("s","") stat.replace("ms","") stat.replace("m","") if (stat.find("-") !=-1 and stat.find("-") <2): print("negative found="+str(stat)) return True else: return False def ReadAndValidateStats(file): print("Start reading and validating the statistics.") path = str(args.gearStudioPath) path = path.replace("gear_studio","") path = path+"private\\tools\\Python27\\" emailMessage="" fileAlreadyRead=False print(path) with open(args.gearStudioPath+'studio/bin/x64/Release/'+STATISTICS_FILE,'r') as statsFile: loop = True while loop: line =statsFile.readline() if (line == "Start of TaskBox\n"): line = statsFile.readline() line = statsFile.readline() currentBox="TaskBox" if (line == "Start of CollectorBox\n"): line = statsFile.readline() currentBox="CollectorBox" if (line == "Start of ThreadBox\n"): line = statsFile.readline() currentBox="ThreadBox" if (line == "End of ThreadBox\n"): loop=False line = line.replace(" ","") index = 0 stat="" originalLine="" originalLine = line while (index != -1): index = line.find(";") stat = line[0:index] if(ValidatePercentage(stat)): if (fileAlreadyRead == False): emailMessage = emailMessage+"\n"+"File="+str(file) emailMessage = emailMessage+"\n Stat="+stat+"\n Original line="+originalLine+"\n" fileAlreadyRead=True if(ValidateNotNegative(stat)): if (fileAlreadyRead == False): emailMessage = emailMessage+"\n"+"File="+str(file) emailMessage = emailMessage+"\n Stat="+stat+"\n Original line="+originalLine+"\n" fileAlreadyRead=True line = line[index+1:] return emailMessage # Main def main(): report = "" UnzipGearStudio() report = report + "Branch=" + str(os.getcwd())+"\n" report = report + str(LaunchTest(NETWORK_PATH_SHP4)) report = report + str(LaunchTest(NETWORK_PATH_PEL)) report = report + str(LaunchTest(NETWORK_PATH_SHP)) report = report + str(LaunchTest(NETWORK_PATH_SHP2)) report = report + str(LaunchTest(NETWORK_PATH_SHP3)) #report = report + str(LaunchTest(NETWORK_PATH_FAP)) if (report != ""): print("stats negative or percentage over 100% has been found. Email sent !") SendEmail(str(report)) if __name__ == '__main__': sys.exit( main() )