def main(): test = False #recipients = {} #recipientsFile = open('C:' + os.sep + 'PythonScripts' + os.sep + 'SelinaReports' + os.sep + 'recipients.txt', 'r') #pairs = recipientsFile.readlines() dateFolder = str(datetime.date.today()) dateFolder = dateFolder[:4] + dateFolder[5:7] + dateFolder[8:10] #create the name of the output directory EOD_ReportsFolder = 'C:\\EOD-Reports\\Montrica\\' todaysFolder = '%s%s' %(EOD_ReportsFolder, dateFolder) #find all the reports generated on a given day Reports = os.listdir(todaysFolder) #add the path to the beginning of every report name todaysReports = [] for report in Reports: todaysReports.append(todaysFolder + os.sep + report) #email the reports to everyone in the list sendTo if not test: sendTo = ['email',\ ] else: sendTo = ['email'] Email.notify(sendTo\ ,'EOD Reports Delivery for Montrica'\ ,'Please find a copy of today\'s reports attached.'\ ,files=todaysReports)
def main(users, \ start, \ end, \ whereToLook = None, \ pattern = r", RETR",\ email = False): if whereToLook == None: whereToLook = "\\\\fcpap01.fcproduction.local\\G6Log\\firstcoverage.com-transfers-%s.log" % end while end >= start: print end #print whereToLook for user in users: ##if os.path.isfile(downloads): ## f = open(downloads) find = r"%s%s" % (user, pattern) print user mainpattern = re.compile(find, re.I) try: QuantCharterCheck.processFile(whereToLook, mainpattern, datetime.date.today(), validateOnly=True) except IOError: pass print end -= datetime.timedelta(days=1) whereToLook = "\\\\fcpap01.fcproduction.local\\G6Log\\firstcoverage.com-transfers-%s.log" % end if email: #Still need to build the email body with downloaders Email.notify()
def checkConnection(): if os.path.isfile("\\\\fcpap01.fcproduction.local\\G6Log\\firstcoverage.com-transfers-%s.log" % start): f = open("\\\\fcpap01.fcproduction.local\\G6Log\\firstcoverage.com-transfers-%s.log" % start) f.close() else: print 'Error: Cannot reach remote file' Email.notify('email', 'Problem Running QuantChartercheck on FCPUTIL01', 'Error: Cannot reach remote file') exit(1)
def main(): live = False fcftp = '//fcpap01.fcproduction.local/ftproot/' locations = {'company':'15893_file/'} today = str(datetime.date.today()) today = today[:4] + today[5:7] + today[8:10] filename = today + '-eod.tab' errMsg = 'Today\'s file ' + ' was not published to: ' + fcftp errMsg2 = 'Please escalate this matter to Todd Brown, Shane Franklin, Fahad Ashfaq or Ramy Abdel-Azim. \nThe following query should assist in regenerating the file.' selectFrom = '\n\n\tselect * from\n\t--begin tran update\n\tscheduledEnvelope' nextSched = '\n\t--set\n\t--nextScheduledRun = \n\t--today\'s date @ 16:15:00.000 ,--for example \'2010-11-12 16:15:00.000\'' lastSched = '\n\t--lastScheduledRun = \n\t--yesterday\'s date @ 16:15:00.000--for example \'2010-11-11 16:15:00.000\'' where = '\n\twhere organizationid = %s and times = \'16:15\'\n\t--commit' query = selectFrom + nextSched + lastSched + where if live: successRecipients = ['email'\ ] failedRecipients = ['email'\ ] else: successRecipients = ['email'\ ] failedRecipients = ['email'\ ] for l in locations.keys(): sql = query % locations[l][:5] try: open(fcftp + locations[l]+filename) Email.notify(successRecipients,\ filename + ' file for ' + l + ' has successfully published!', '') except: Email.notify(failedRecipients,\ filename + ' not yet published for ' + l + '!',\ errMsg + locations[l]+ '.\n' + errMsg2 + sql) continue
def main(): live = True fcftp = '//fcpap01.fcproduction.local/ftproot/' locations = {'company':'15893_file/',\ 'company':'15892_file/',\ 'company':'15891_file/'} today = str(datetime.date.today()) yesterday = str(datetime.date.today()+ datetime.timedelta(days = -1)) selectFrom = '\tuse firstcoverage\n\tselect * from scheduledEnvelope\n\t--begin tran update scheduledEnvelope' nextSched = '\n\t--set nextScheduledRun = \'%s ' % (today) lastSched = ':00.000\',\n\t--lastScheduledRun = \'%s ' % (yesterday) where = ':00.000\' \n\twhere organizationid = %s and times = \'%s\'\n\t--commit' if live: successRecipients = ['email'\ ] failureRecipients = ['email' ] elif not live: successRecipients = ['email'] failureRecipients = ['email'] today = str(datetime.date.today()) today = today[:4] + today[5:7] + today[8:10] filename = today + '-eod_Europe.tab' err1 = 'Today\'s file ' + filename + ' was not published to: ' + fcftp err2 = 'Please escalate this matter to Todd Brown, Shane Franklin, Fahad Ashfaq or Ramy Abdel-Azim. \nThe following query should assist in regenerating the file.' err3 = '\nFirst run the query as is. Check to make sure the row returned (it should only be one row) is the row you expect to update.' err4 = '\nIf the row returned is accurate, comment OUT the first line of the query and comment back IN all the other lines of the query.' for l in locations.keys(): try: open(fcftp + locations[l]+filename) Email.notify(transmitter = 'email',\ receiver = successRecipients,\ subject = filename + ' file for ' + l + ' has successfully published!', body ='') except: if l == 'CR Intrinsics': tiempo = '16:10' company = '15892' elif l == 'SAC': tiempo = '16:15' company = '15891' elif l == 'Sigma': tiempo = '16:20' company = '15893' query = selectFrom + nextSched + tiempo + lastSched + tiempo + where sql = query % (company, tiempo) if not live: print sql print err1 + '.\n' + err2 + err3 + err4 + '\n\n' + sql continue Email.notify(transmitter = 'email',\ receiver = failureRecipients,\ subject = filename + ' not yet published for ' + l + '!',\ body = err1 + locations[l] + '.\n' + err2 + err3 + '\n\n' + sql) continue exit(0)
def main(): checkingYesterdaysUpload = False #True for testing. server = 'sever' username = '******' password = '******' if not checkingYesterdaysUpload: today = str(datetime.date.today()) today = today[:4] + today[5:7] + today[8:10] f = today + '-eod.tab' else: # For Testing yesterday = str(datetime.date.today() + datetime.timedelta(days = -1)) yesterday = yesterday[:4] + yesterday[5:7] + yesterday[8:10] f = yesterday + '-eod.tab' itList = [f] conn = ftplib.FTP(server) conn.login(username,password) fileList = [] ls = conn.retrlines('LIST', fileList.append) conn.quit() for fileToCheck in itList: found = False for f in fileList: if fileToCheck in f: print f found = True if found: if checkingYesterdaysUpload: Email.notify(['email' ],\ fileToCheck + ' uploaded successfully',\ fileToCheck + ' uploaded successfully to ' + server,\ [],\ 'email') else: #Email Everyone Email.notify(['email'] ,\ fileToCheck + ' uploaded successfully for CITADEL',\ fileToCheck + ' uploaded successfully to ' + server,\ [],\ 'email') pass if not found: print 'ERROR!!' if checkingYesterdaysUpload: Email.notify(['email' ],\ fileToCheck + ' WAS NOT UPLOADED',\ fileToCheck + ' was not uploaded to ' + server + '\n' + generateErrInstructions(),\ [],\ 'email') else: #Email Everyone Email.notify(['email' ],\ fileToCheck + ' WAS NOT UPLOADED',\ fileToCheck + ' was not uploaded to ' + server + '\n' + generateErrInstructions(),\ [],\ 'email')
def main(): verbose = False wantFileAttached = False print 'Entering main()' dbServer = 'server' dbDatabase = 'firstcoverage' dbUser = '******' dbPwd = 'pass' mssql=_mssql.connect(dbServer,dbUser,dbPwd)#,dbDatabase) #con = pymssql.connect(dbServer,dbUser,dbPwd, dbDatabase) mssql.select_db(dbDatabase) #cursor = con.cursor() queryArg = sys.argv[2] if verbose: print queryArg if os.path.isfile(queryArg): query = open(queryArg).read() else: query = queryArg if len(sys.argv) == 5: recips = sys.argv[4].split(',') else: recips = ['email'] if verbose: print "len of args", len(sys.argv) if len(sys.argv) > 3: wantFileAttached = True outputFile = sys.argv[3] if mssql.query(query): if verbose: print 'running query' ## get the data results=mssql.fetch_array() mssql.close() resultTuple = results[0] if resultTuple[1] == 0: emailBody = 'Zero Rows returned' #if verbose or resultTuple[1] != 0: else: i = 0 columnTuples = resultTuple[0] listOfRows = resultTuple[2] columns = [] emailBody = '' if verbose: print 'looping results' while i < len(columnTuples): columns.append(columnTuples[i][0]) i = i + 1 if verbose: print 'made columns list' emailBody = '\t'.join([title.strip() for title in columns]) emailBody = emailBody + '\n' print emailBody if verbose: print 'starting rows' emailBody = emailBody + '\n'.join(('\t'.join([field.strip() for \ field in [str(tup).center(20) for \ tup in rowTuple]])) for\ rowTuple in listOfRows) if verbose: print 'finished rows' #print emailBody if verbose: print wantFileAttached print outputFile h = open(outputFile, 'w') h.write(emailBody) h.close() if verbose: print 'writing query results to email body' emailBody = emailBody + '\n\nQUERY USED\n\n' emailBody = emailBody + query if verbose: print 'Sending Email' if wantFileAttached: Email.notify(['email'], sys.argv[1], query, [outputFile]) else: Email.notify(['email'], sys.argv[1], emailBody) else: if wantFileAttached: Email.notify(recips, sys.argv[1], query, [outputFile]) else: Email.notify(recips, sys.argv[1], emailBody)
thirtyDaysAgo = today + datetime.timedelta(days = -30) print today today = str(today)[:4] + str(today)[5:7] + str(today)[8:10] thirtyDaysAgo = str(thirtyDaysAgo)[:4] + str(thirtyDaysAgo)[5:7] + str(thirtyDaysAgo)[8:10] + '1630' pathPart = PATH + os.sep + today if test: toFix = PATH + os.sep + 'Copy of ' + today + '-eod.tab' else: toFix = pathPart + '-eod.tab' fixed = pathPart + '-REPLACED.tab' f = open(toFix, 'r') newFile = open(fixed, 'w') for line in f.readlines(): if 'DOW J' in line or 'Dow J' in line: newFile.write(line[:51] + line[51:].replace(' ', '\t')) elif 'EVERTZ' in line: newFile.write(line[:70] + line[70:].replace(' ', '\t')) else: newFile.write(line[:25] + line[25:].replace(' ', '\t')) f.close() newFile.close() shutil.copy(fixed,toFix) os.remove(fixed) Email.notify(['email'], 'Another Go: company SR-Buy Sell report', 'Please QC this file any way you see fit.',[toFix])
def main(): try: test = False compress = True recipientsDirectory = 'C:' + os.sep + 'PythonScripts' + os.sep + 'SelinaReports' + os.sep if not test: recipientsFile = open(recipientsDirectory + 'recipients.txt', 'r') else: recipientsFile = open(recipientsDirectory + 'recipients_old.txt', 'r') recips = readRecipientsIntoMemory(recipientsFile) if not test: dateFolder = str(datetime.date.today()) else: dateFolder = str(datetime.date.today()+ datetime.timedelta(days = -1)) dateFolder = dateFolder[:4] + dateFolder[5:7] + dateFolder[8:10] named_date = datetime.date.today().strftime("%b %d.%y") subject = 'First Coverage Daily Update-'+ named_date #create the name of the output directory EOD_ReportsFolder = 'C:\\EOD-Reports\\' todaysFolder = '%s%s' %(EOD_ReportsFolder, dateFolder) #find all the reports generated on a given day Reports = os.listdir(todaysFolder) #add the path to the beginning of every report name todaysReports = [] for report in Reports: todaysReports.append(todaysFolder + os.sep + report) #give a name to the zip file where the reports will be archived #and zip all the files. if compress: if not os.path.isdir(todaysFolder + '_sent'): os.mkdir(todaysFolder + '_sent') if not os.path.isdir(todaysFolder + os.sep + 'zips'): os.mkdir(todaysFolder + os.sep + 'zips') if not os.path.isdir(todaysFolder + '_sent' + os.sep + 'zips'): os.mkdir(todaysFolder + '_sent' + os.sep + 'zips') zipFolder = todaysFolder + os.sep + 'zips' print '!!COMPRESS LOOP!!' for report in todaysReports: print 'report', report #f.write('report' + report) f = report[:-4] + '.zip' if not os.path.isdir(report): print 'report is: ' + report + ' file is: ' + f Zipper.main([report],f, True) #[:-4] + "_" + str(x) + ".zip") #todaysZipFolder = zipFolder print 'move %s to %s' %(f, zipFolder) shutil.move(f, zipFolder) msg = '''Please find a copy of today's First Coverage report attached. It includes all open and closed ideas entered into First Coverage for you since inception along with total return, vs sector, stop, target and comments. Regards, First Coverage www.firstcoverage.com 888-473-7836''' sterlingFiles = [] sendDirectory = todaysFolder + os.sep + 'zips' #if test: # zipFolder = todaysFolder + os.sep + 'zips' #if compress or test: # sendDirectory = zipFolder print 'looping over', os.listdir(sendDirectory) for report in os.listdir(sendDirectory): #print 'Todays Folder: ', sendDirectory + os.sep + report #Find the client name in the published reports. If a report isn't published for the client, #the client will not receive anything. for name in clientList: if 'SterlingJohnston' in report: sterlingFiles.append(todaysFolder + os.sep + report) elif name in report: sendReportToRecipient(name,[sendDirectory + os.sep + report], subject, msg, test, recips) #if test: print 'current report is: ', report print 'moving : ' + sendDirectory + os.sep + report + ' to : ' + todaysFolder + '_sent' + os.sep + 'zips' shutil.move(sendDirectory + os.sep + report,\ todaysFolder + '_sent' + os.sep + 'zips') #if test : print 'moving : ' + todaysFolder + os.sep + report[:-4] + '.xls' + ' to : ' + todaysFolder + '_sent' shutil.move(todaysFolder + os.sep + report[:-4] + '.xls', todaysFolder + '_sent') if len(sterlingFiles) == 2: #!!untested - sending the sterling formatted reports is discouraged going forward. sendReportToRecipient('Sterling Johnston',sterlingFiles, subject, msg, test, recips) for f in sterlingFiles: shutil.move(sendDirectory + os.sep + f, todaysFolder + '_sent' + os.sep + 'zips') shutil.move(todaysFolder + os.sep + f[:-4] + '.xls', todaysFolder + '_sent') everythingLeft = os.listdir(todaysFolder) leftovers = [] for thing in everythingLeft: if not os.path.isdir(todaysFolder + os.sep + thing): leftovers.append(thing) if leftovers !=[]: Email.notify(['email'],\ 'Problem with Selina/EOD/Daily Update XLS report',\ 'The following reports could not be sent for some reason. Most likely, the name of the file\nand the name in recipients.txt do not match up.'\ + '\n\n' + '\n'.join(leftovers) ) else: shutil.rmtree(todaysFolder) except: traceback.print_exc(file=open(r'c:\temp\EOD-XL_log.txt', 'a')) raise exit(1)
def main(): test = False wantFileAttached = False print 'Entering main()' dbServer = 'server' dbDatabase = 'firstcoverage' dbUser = '******'' dbPwd = 'XXX' mssql=_mssql.connect(dbServer,dbUser,dbPwd) mssql.select_db(dbDatabase) queryArg = sys.argv[2] if test: print queryArg if os.path.isfile(queryArg): query = open(queryArg).read() else: query = queryArg print len(sys.argv) print query if mssql.query(query): if test: print 'running query' ## get the data results=mssql.fetch_array() mssql.close() resultTuple = results[0] if resultTuple[1] == 0: emailBody = 'Zero Rows returned' if test or resultTuple[1] != 0: i = 0 columnTuples = resultTuple[0] listOfRows = resultTuple[2] columns = [] emailBody = '' if test: print 'looping results' while i < len(columnTuples): columns.append(columnTuples[i][0]) i = i + 1 emailBody = emailBody + '\t'.join([title.center(20) for title in columns]) emailBody = emailBody + '\n' rows = [] for rowTuple in listOfRows: #row loop rows.append('\t'.join([str(tup).center(20) for tup in rowTuple])) #emailBody = emailBody + '\t'.join([str(tup).center(20) for tup in rowTuple]) emailBody = emailBody + '\n'.join(rows) #print emailBody if test: print 'writing query results to email body' emailBody = emailBody + '\n\nQUERY USED\n\n' emailBody = emailBody + query if test: print 'Sending Email' Email.notify(['email'], sys.argv[1], emailBody) else: if len(sys.argv) == 4: Email.notify(eval(sys.argv[3],{},{}), sys.argv[1], emailBody) else: Email.notify(['email'], sys.argv[1], emailBody)
def generateQuantEmail(test, sheet, downloaders, attachments = [], metaFile = 'C:/PythonScripts/Quant/NumberOfAccounts.log'): emails = {'Dave':'email',\ 'Chris':'email',\ 'Ramy': 'email'} attach = False bodyOfEmail = '' subj = 'Download Activity' newDownloaders = False for tup in downloaders: if (tup[0] + ' downloaded on ' + tup[1] + '\n') not in bodyOfEmail: print tup[0] + ' downloaded on ' + tup[1] bodyOfEmail = bodyOfEmail + tup[0] + ' downloaded on ' + tup[1] + '\n' if not newDownloaders: newDownloaders = True if newDownloaders: subj = subj + ' - New Downloads' if bodyOfEmail == '': bodyOfEmail = 'No New Downloads.' f = open(metaFile, 'r') oldNumberOfaccounts = int(f.readline()) print 'yesterday there were ' + str(oldNumberOfaccounts) f.close() currentNumberOfAccounts = sheet.nrows print 'today there are ' + str(currentNumberOfAccounts) print (currentNumberOfAccounts - oldNumberOfaccounts) if(currentNumberOfAccounts > oldNumberOfaccounts): attach = True f = open(metaFile, 'w') f.write(str(currentNumberOfAccounts)) update = (currentNumberOfAccounts - oldNumberOfaccounts) if update == 1: bodyOfEmail = bodyOfEmail + ' %s New Account' % (update) else: bodyOfEmail = bodyOfEmail + ' %s New Accounts' % (update) for x in range(0,update): if x == 0: bodyOfEmail = bodyOfEmail + '(%s' % sheet.cell_value(rowx = (oldNumberOfaccounts + x), colx = 3) elif (x < update): bodyOfEmail = bodyOfEmail + ', %s' % sheet.cell_value(rowx = (oldNumberOfaccounts + x), colx = 3) if (x == update - 1): bodyOfEmail = bodyOfEmail + ')' elif(currentNumberOfAccounts == oldNumberOfaccounts): bodyOfEmail = bodyOfEmail + ' No New Accounts.' bodyOfEmail = bodyOfEmail + '\n_Ramy' try: if(test and attach): Email.notify([emails['Ramy']], subj, bodyOfEmail, attachments) elif(test and not attach): Email.notify([emails['Ramy']], subj, bodyOfEmail) elif (attach and not test): Email.notify(emails.values(), subj, bodyOfEmail, attachments) elif (not attach and not test): Email.notify(emails.values(), subj, bodyOfEmail) except: Email.notify([emails['Ramy']], 'Emailing Error', 'Unable to send the download report from FCPUTIL01')