def trigger(self, k, result): """ 每个报警模版必有的主触发函数,通过此函数来触发进行报警。 """ mail = Mail() content = self._init_content(k, result) mail.sendmail(MAIL_SENDEE, k, content)
def init_app(app): app.teardown_appcontext(app_teardown) app.before_request(before_request) for error in range(400, 420) + range(500, 506): app.error_handler_spec[None][error] = http_error_handler app.register_error_handler(ResponseMeta, response_meta_handler) app.register_error_handler(Exception, generic_error_handler) from utils.mail import Mail Mail.init_app(app) # 注册接口 from website.api import api from website.web import web from website.wx import wx from website.application import app as application from website.message import root as message from website.account import account from website.question import question from website import customer app.register_blueprint(web) app.register_blueprint(api) app.register_blueprint(wx) app.register_blueprint(application) app.register_blueprint(question) app.register_blueprint(message) app.register_blueprint(account) app.register_blueprint(customer.app)
def worker(loop, seconds, url): i = 1 total_erros = 0 dao = Dao() while(i <= loop): print('check => ', url, 'i => ', i) try: start = time.time() request = requests.get(url, timeout=CONFIG.get("REQUEST_TIMEOUT")) status_code = request.status_code except Exception as e: if CONFIG.get("SENTRY_URL") != "": sentry_sdk.init(CONFIG.get("SENTRY_URL")) sentry_sdk.capture_exception(e) status_code = CONFIG.get("REQUEST_DEFAULT_ERROR") total_erros += 1 if total_erros > CONFIG.get("NUMBER_ERRORS_SEND_MAIL"): if CONFIG.get("SENDMAIL"): mail = Mail(url, status_code) mail.send_mail() total_erros = 0 try: dao.insert(log_entity(url, status_code, (time.time() - start))) except Exception as e: if CONFIG.get("SENTRY_URL") != "": sentry_sdk.init(CONFIG.get("SENTRY_URL")) sentry_sdk.capture_exception(e) sentry_sdk.capture_exception(e) i += 1 time.sleep(seconds)
def run(self): _message = render_to_string('mail/gk3_announce.html') _mail = Mail(u"果库 3.0 隆重上线!", _message) count = 0 for _obj in Email_List.objects.filter(done=False): try: _mail.send(address=_obj.email) _obj.done = True _obj.save() logger.info("[edm][info] %s sent success.", _obj.email) except Exception, e: logger.error("[edm][error] %s", e) count += 1 if count >= 1000: break
def create(self, data): try: with self.con: title = data.get('title') text = data.get('text') img_url = data.get('img_url') img_width = data.get('img_width') img_height = data.get('img_height') type = data.get('type') cur = self.con.cursor(Model=News) sql = "INSERT INTO news (title, text, img_url, img_width, img_height, date, type) VALUES (%s, %s, %s, %s, %s, %s, %s)" now = datetime.now() now = now.strftime('%Y-%m-%d %H:%M:%S') cur.execute( sql, (title, text, img_url, img_width, img_height, now, type)) self.con.commit() sql = "SELECT * FROM news WHERE id = (SELECT MAX(id) FROM news)" cur.execute(sql) last = cur.fetchone() tokens = UserView().list_tokens() message = NotificationMessage(data) NotificationsView(message, tokens).send_push_message() if type == 'email': list_mail = UserView().list_mail() Mail().massive_mail_sender(list_mail, title, text) return last.to_json() except Exception as e: print(e) self.con.rollback() return Error('Problem happened in news creation', 400).get_error()
def reset_password(self): """ generate a new password for the login and send it by mail """ letters = string.ascii_lowercase new_pwd = ''.join(random.choice(letters) for i in range(10)) try: with self.con: cur = self.con.cursor(Model = User) sql = "UPDATE users_app SET password=aes_encrypt(%s, %s) WHERE login=%s;" cur.execute(sql, (new_pwd, SALT, self.login)) self.con.commit() email = self.get(self.login).to_json().get('email') msg = """ Salut, participant.e à l'édition 2020 de SKI'UTC. Comme tu as pu le constater, cette année, <br> De la nouveauté cette année : une application SKI'UTC rien que pour toi, rien que pour vous ! <br> <br> Cours l'installer sur le store de ton téléphone (recherches SKI'UTC) <br> <br> Pour te connecter c'est simple : tu utilises ton login (ou email si tu es tremplin), et tu utilises ces codes : <br><br> <B>IDENTIFIANT : {}</B><br> <B>PASSWORD : {}</B><br> """.format(self.login, new_pwd) Mail().mail_sender(email, "Ton mot de passe pour l'application SKI'UTC", msg) return new_pwd except Exception as e: print(e) self.con.rollback() response.status = 400 return Error('Error happened in password reset process').get_error()
def retrieve_password(self): self.__ensure_user_obj() self.__ensure_user_profile_obj() _token = self.__create_one_time_token('reset_password') _url = 'http://www.guoku.com/reset_password/?token=' + _token _message = render_to_string( 'mail/forget_password.html', { 'url' : _url, 'nickname' : self.user_profile_obj.nickname } ) _mail = Mail(u"重设果库帐号密码", _message) print self.user_obj.email _mail.send( address = self.user_obj.email )
def save(self, *args, **kwargs): if not self.name_cn: try: youdao = requests.get(''.join( [settings.YOUDAO_BASEURL, self.name_en])).json() name_cn = youdao.get('translation', [])[0] if any(u'\u4e00' <= char <= u'\u9fff' for char in name_cn): self.name_cn = name_cn else: raise RuntimeError('Cannot translate %s to chinese!' % self.name_en) except: Mail.send(['*****@*****.**', '*****@*****.**'], 'Failure Overseas City Translation', 'Youdao cannot translation %s.' % self.name_en) # need add method for country map # if not self.country_cn: super(City, self).save(*args, **kwargs)
def init_app(app): app.teardown_appcontext(app_teardown) app.before_request(before_request) for error in range(400, 420) + range(500, 506): app.error_handler_spec[None][error] = http_error_handler app.register_error_handler(ResponseMeta, response_meta_handler) app.register_error_handler(Exception, generic_error_handler) from utils.mail import Mail from utils.sentry import Sentry Mail.init_app(app) Sentry.init_app(app) # 注册接口 from api import api from web import web app.register_blueprint(web) app.register_blueprint(api)
def compute_flow(self): if is_time_early(): first_range = { 'from': get_upper_first(), 'to': get_yesterday_time() } last_range = {'from': get_upper_first(), 'to': get_today_time()} else: first_range = { 'from': get_early_time(), 'to': get_yesterday_time() } last_range = {'from': get_early_time(), 'to': get_today_time()} try: first_response = self.get_bw(first_range) last_response = self.get_bw(last_range) customer = self.get_customer() libs = self.get_libs() dispose = LumDispose(customer, libs) first_bw = dispose.dispose_flow(first_response, first_range) last_bw = dispose.dispose_flow(last_response, last_range) self.make_flow_report(first_bw) compute_bw = {} for item in first_bw['zones']: compute_bw[item] = \ self.conversion(self.get_value(last_bw['zones'][item]) - self.get_value(first_bw['zones'][item])) self.submit(compute_bw) except: if IS_FAIL: mail = Mail() mail.send_content_mail(FAIL_MSG2 % 'compute_flow', FAIL_TITLE_MSG, MAIL_FLOW_SEND) # mail.send_content_mail(FAIL_MSG % 'compute_flow', FAIL_TITLE_MSG, MAIL_TEST) mail.close() return schedule.CancelJob
def mails(self): self.mail_content = StringIO() self.mail_content.write("*This is an automatically generated email. Do not reply.*\n\n\n") self.is_need_mail = False if not self.is_all_7zfiles_exist: self.is_need_mail = True self.mail_content.write("**************************Missing 7z files**************************\n") self.mail_content.writelines(self.mail_content_7zfiles.getvalue()) self.mail_content.write("\n\n") if not self.is_all_scs_configed: self.is_need_mail = True self.mail_content.write("**************************Scs not configed**************************\n") self.mail_content.write(self.mail_content_scs) self.mail_content.write("\n\n") if not self.is_recovered_bitbake: self.is_need_mail = True self.mail_content.write("**************************Recover bitbake**************************\n") self.mail_content.writelines(self.mail_content_recover.getvalue()) self.mail_content.write("\n\n") elif not self.is_bitbake_content_fine: self.is_need_mail = True self.mail_content.write("**************************Recover bitbake**************************\n") self.mail_content.writelines(self.mail_content_recover.getvalue()) self.mail_content.write("\n\n") if self.is_need_mail: mail = Mail(self.config.get("mail_from"), self.config.get("mail_to")) mail.create(self.config.get("mail_subject"), self.mail_content.getvalue()) mail.send()
def init_app(app): app.teardown_appcontext(app_teardown) app.before_request(before_request) for error in range(400, 420) + range(500, 506): app.error_handler_spec[None][error] = http_error_handler app.register_error_handler(ResponseMeta, response_meta_handler) app.register_error_handler(Exception, generic_error_handler) from utils.mail import Mail Mail.init_app(app) # 注册接口 from api import api from web import web from store import store from account import account app.register_blueprint(web) app.register_blueprint(api) app.register_blueprint(store) app.register_blueprint(account)
def run(self): fp = open(os.path.join(REPORT_PATH, self.report_name), 'wb') runner = HTMLTestRunner(stream=fp, title=u'测试报告', description=u'测试用例执行情况') runner.run(self.suites) logger.info('run suits success') fp.close() logger.info(self.report_name + ' is closed.') result = BeautifulReport(self.suites) result.report(filename=self.report_name, description='测试deafult报告', log_path='report') # 发邮件 Mail(self.report_name).send()
def make_flow_report(self, first_bw): if not is_time_early(): return None csv = FlowCsv() for item in first_bw['zones']: csv.writer_csv([ item, self.conversion(self.get_value(first_bw['zones'][item])) ]) csv.close_csv() mail = Mail() mail.send_file_mail(REPROT_MSG, REPROT_TITLE_MSG, csv.get_path(), MAIL_REPORT_SEND) mail.close()
def unlock_level(self, key, user): try: users_info = self.get_user_level() with self.con: cur = self.con.cursor(Model=AnimationKey) sql = "SELECT `level`, `next_indice` from `anim_key` WHERE `key`=%s" cur.execute(sql, key) response = cur.fetchone() if response is None: return {} target_anim = response.to_json() if users_info.get('level') + 1 == target_anim.get('level'): user_mail = user.to_json().get('email') Mail().mail_sender(user_mail, "Ton nouvel indice", target_anim.get('next_indice')) return self.update_user_level(target_anim.get('level')) else: return Error('Unauthorized animation level target', 403).get_error() except Exception as e: print(e) return Error('Problem happened in query get', 400).get_error()
def submit(self, data): bw, bw_success, bw_fail, num_success, num_fail = '%s : %s/g, ', '', '', 0, 0 for item in data: API_PARAM['zone'] = item API_PARAM['use_flow'] = data[item] # self.request.post_back_data(GEEK_API, API_PARAM) try: response = self.request.post_back_data(QUICKBUY_API, API_PARAM) if response.status_code == 200: bw_success += bw % (item, data[item]) num_success += 1 else: bw_fail += bw % (item, data[item]) num_fail += 1 except: bw_fail += bw % (item, data[item]) num_fail += 1 continue mail = Mail() mail.send_content_mail( SUCCESS_MSG % (bw_success, num_success, bw_fail, num_fail), SUCCESS_TITLE_MSG, MAIL_FLOW_SEND) mail.close()
def sendMail(ciIndex_thisWeek, ciIndex_lastWeek=0): thisWeekCommit = ciIndex_thisWeek['commitCount'] lastWeekCommit = ciIndex_lastWeek[ 'commitCount'] if ciIndex_lastWeek != 0 else 0 COMMIT_AND_TIME = "<table border='1' align=center> <caption><font size='3'><b>用户感知指标</b></font></caption>" if ciIndex_lastWeek == 0: DIFF_RATE = "%.2f" % float((thisWeekCommit - 0.00001) / 0.00001 * 100) else: DIFF_RATE = "%.2f" % float( abs(thisWeekCommit - lastWeekCommit) / lastWeekCommit * 100) if thisWeekCommit - lastWeekCommit > 0: COMMIT_AND_TIME += "<tr align=center><td bgcolor='#d0d0d0'>commit数</td><td bgcolor='#ff6eb4'>%s(↑ %s" % ( thisWeekCommit, DIFF_RATE) COMMIT_AND_TIME += "%)</td></tr>" else: COMMIT_AND_TIME += "<tr align=center><td bgcolor='#d0d0d0'>commit数</td><td bgcolor='#b5c4b1'>%s(↓ %s" % ( thisWeekCommit, DIFF_RATE) COMMIT_AND_TIME += "%)</td></tr>" MAX_WAITTIME = max( float(ciIndex_thisWeek['PR-CI-Coverage_average_wait_time']), float(ciIndex_thisWeek['PR-CI-Py35_average_wait_time']), float(ciIndex_thisWeek['PR-CI-Inference_average_wait_time']), float(ciIndex_thisWeek['PR-CI-CPU-Py2_average_wait_time'])) MAX_EXECTIME = max( float(ciIndex_thisWeek['PR-CI-Coverage_average_exec_time']), float(ciIndex_thisWeek['PR-CI-Py35_average_exec_time']), float(ciIndex_thisWeek['PR-CI-Inference_average_exec_time']), float(ciIndex_thisWeek['PR-CI-CPU-Py2_average_exec_time'])) COMMIT_AND_TIME += "<tr align=center><td bgcolor='#d0d0d0'>commit从提交到返回结果耗时</td><td>%.2f min</td></tr>" % ( MAX_WAITTIME + MAX_EXECTIME) COMMIT_AND_TIME += "</table>" HTML_CONTENT = "<html><body><p>Hi, ALL:</p> <p>本周CI评价指标详细信息可参考如下表格:</p> %s" % COMMIT_AND_TIME HTML_TABLE_CONTENT = write_excel_xls(ciIndex_thisWeek, ciIndex_lastWeek) HTML_CONTENT = HTML_CONTENT + "<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td height='10'></td</tr></table>" + HTML_TABLE_CONTENT + "<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td height='10'></td</tr></table>" + "<p>如有问题,随时联系</p> <p>张春乐</p></body></html>" mail = Mail() mail.set_sender('*****@*****.**') mail.set_receivers(['*****@*****.**']) mail.set_title( '效率云%s~%s CI评价指标统计' % (ciIndex_thisWeek['startTime'], ciIndex_thisWeek['endTime'])) mail.set_message(HTML_CONTENT, messageType='html', encoding='gb2312') mail.send()
import random import time from config.pathes import PROJECTINFO from utils.FileReader import YamlReader from utils.mail import Mail from utils.report import runner Y = [] time.sleep(random.randint(1, 90)) test = 'test_oa_clockin_Y*' runner(test) receusers = YamlReader(PROJECTINFO).get('OA').get('mail')['receusers'][0] Y.append(receusers) eml = Mail('OA', Y) eml.send_mail()
def sendCloseMail(content): mail = Mail() mail.set_sender('*****@*****.**') mail.set_receivers(['*****@*****.**']) mail.set_title('关闭超过1年未更新的issue/pr通知~') mail.set_message(content, messageType='html', encoding='gb2312') mail.send()
def sendMail(self, mailContent): HTML_CONTENT = "<html><body><p>Hi, ALL:</p>" HTML_CONTENT += mailContent HTML_CONTENT += "<p>如有问题,请联系张春乐.</p> <p>张春乐</p></body></html>" mail = Mail() mail.set_sender('*****@*****.**') mail.set_receivers(['*****@*****.**']) mail.set_title('[告警]自动取消超时任务') mail.set_message(HTML_CONTENT, messageType='html', encoding='gb2312') mail.send()
def mail(HTML_CONTENT): mail = Mail() mail.set_sender('*****@*****.**') mail.set_receivers(['*****@*****.**']) mail.set_title("【告警】效率云过去4小时/过去1天CI指标") mail.set_message(HTML_CONTENT, messageType='html', encoding='gb2312') mail.send()
if start_time is None: exit(1) sql = f"SELECT * FROM RPA.CRM_DV_ERASE_VOUCHER where {condition}" df = pd.read_sql(sql, con=conn) except cx_Oracle.DatabaseError as e: print(e) # log.log_critical("There is a problem with Oracle DETAILS : EXCEPTION - {e}") location = './erased_voucher_report.xlsx' try: os.remove(location) except FileNotFoundError as e: # log.log_warn(f"No File Found to be deleted LOCATION - {location}") pass if len(df) > 0: df.to_excel(location) else: location = None mail = Mail(channel=Mail.outlook) with open('./erased_voucher_config.json') as config_file: data = json.load(config_file) targets = data['targets'] cc = data['cc'] mail_body = f'<p>Dear Concern,</p> {len(df)} number of SR has been processed between {start_time} to {end_time} <p>Thanks</p>' mail.send_mail_to(targets, cc, 'CRM RPA Report', mail_body, location) mail.send()
def sendMail(ciIndex_thisWeek, ciIndex_lastWeek=0): COMMIT_AND_TIME = "<table border='1' align=center> <caption><font size='3'><b>用户感知指标</b></font></caption>" repo_list = localConfig.cf.get('ciIndex', 'commitCount').split(',') #用户感知指标: 各个repo的commit的数目 for repo in repo_list: key = '%s_commitCount' % repo.split('/')[1] thisWeekCommit = ciIndex_thisWeek[key] lastWeekCommit = ciIndex_lastWeek[key] if ciIndex_lastWeek != 0 else 0 if ciIndex_lastWeek == 0: DIFF_RATE = "%.2f" % float( (thisWeekCommit - 0.00001) / 0.00001 * 100) else: DIFF_RATE = "%.2f" % float( abs(thisWeekCommit - lastWeekCommit) / lastWeekCommit * 100) if thisWeekCommit - lastWeekCommit > 0: COMMIT_AND_TIME += "<tr align=center><td bgcolor='#d0d0d0'>%s commit数/个</td><td bgcolor='#ff6eb4'>%s(↑ %s" % ( repo.split('/')[1], thisWeekCommit, DIFF_RATE) COMMIT_AND_TIME += "%)</td></tr>" else: COMMIT_AND_TIME += "<tr align=center><td bgcolor='#d0d0d0'>%s commit数/个</td><td bgcolor='#b5c4b1'>%s(↓ %s" % ( repo.split('/')[1], thisWeekCommit, DIFF_RATE) COMMIT_AND_TIME += "%)</td></tr>" #用户感知指标: commit从提交到返回结果平均耗时 thisweek_MAX_CONSUMTIME = 0 CI_NAME_list = localConfig.cf.get('ciIndex', 'ci_name').split(',') #所有的ci for ci in CI_NAME_list: if float(ciIndex_thisWeek['%s_average_consum_time' % ci]) > thisweek_MAX_CONSUMTIME: thisweek_MAX_CONSUMTIME = float( ciIndex_thisWeek['%s_average_consum_time' % ci]) max_CONSUMTIME_CI = ci lastweek_MAX_CONSUMTIME = float(ciIndex_lastWeek['%s_average_consum_time' % max_CONSUMTIME_CI]) consum_DIFF_RATE = "%.2f" % float( abs(thisweek_MAX_CONSUMTIME - lastweek_MAX_CONSUMTIME) / lastweek_MAX_CONSUMTIME * 100) if thisweek_MAX_CONSUMTIME - lastweek_MAX_CONSUMTIME > 0: COMMIT_AND_TIME += "<tr align=center><td bgcolor='#d0d0d0'>commit从提交到返回结果平均耗时/min</td><td bgcolor='#ff6eb4'>%s(↑ %s" % ( thisweek_MAX_CONSUMTIME, consum_DIFF_RATE) COMMIT_AND_TIME += "%)</td></tr>" else: COMMIT_AND_TIME += "<tr align=center><td bgcolor='#d0d0d0'>commit从提交到返回结果平均耗时/min</td><td bgcolor='#b5c4b1'>%s(↓ %s" % ( thisweek_MAX_CONSUMTIME, consum_DIFF_RATE) COMMIT_AND_TIME += "%)</td></tr>" #用户感知指标: commit从提交到返回结果最长耗时 thisweek_Longest_Time = float(ciIndex_thisWeek['LongestTime']) lastweek_Longest_Time = float(ciIndex_lastWeek['LongestTime']) longestTime_DIFF_RATE = "%.2f" % float( abs(thisweek_Longest_Time - lastweek_Longest_Time) / lastweek_Longest_Time * 100) if thisweek_Longest_Time - lastweek_Longest_Time > 0: COMMIT_AND_TIME += "<tr align=center><td bgcolor='#d0d0d0'>commit从提交到返回结果最长耗时/min</td><td bgcolor='#ff6eb4'>%s(↑ %s" % ( thisweek_Longest_Time, longestTime_DIFF_RATE) COMMIT_AND_TIME += "%)</td></tr>" else: COMMIT_AND_TIME += "<tr align=center><td bgcolor='#d0d0d0'>commit从提交到返回结果最长耗时/min</td><td bgcolor='#b5c4b1'>%s(↓ %s" % ( thisweek_Longest_Time, longestTime_DIFF_RATE) COMMIT_AND_TIME += "%)</td></tr>" #用户感知指标: 单测随机挂引起的RERUN占比 thisWeek_rerun_index = testRerun( '%s 00:00:00' % ciIndex_thisWeek['startTime'], '%s 00:00:00' % ciIndex_thisWeek['endTime']) lastWeek_rerun_index = testRerun( '%s 00:00:00' % ciIndex_lastWeek['startTime'], '%s 00:00:00' % ciIndex_lastWeek['endTime']) rerun_DIFF_RATE = "%.2f" % float( abs( float(thisWeek_rerun_index['all_testfailed_rerunRatio']) - float(lastWeek_rerun_index['all_testfailed_rerunRatio']))) if float(thisWeek_rerun_index['all_testfailed_rerunRatio']) - float( lastWeek_rerun_index['all_testfailed_rerunRatio']) > 0: COMMIT_AND_TIME += "<tr align=center><td bgcolor='#d0d0d0'>单测随机挂引起的RERUN占比</td>" RERUN_INDEX_DIFF = "<td bgcolor='#ff6eb4'>%s" % thisWeek_rerun_index[ 'all_testfailed_rerunRatio'] + "%(↑ " + "%s" % rerun_DIFF_RATE + "%)</td>" else: COMMIT_AND_TIME += "<tr align=center><td bgcolor='#d0d0d0'>单测随机挂引起的RERUN占比</td>" RERUN_INDEX_DIFF = "<td bgcolor='#b5c4b1'>%s" % thisWeek_rerun_index[ 'all_testfailed_rerunRatio'] + "%(↓ " + "%s" % rerun_DIFF_RATE + "%)</td>" COMMIT_AND_TIME += RERUN_INDEX_DIFF COMMIT_AND_TIME += "</tr>" #用户感知指标: 平均失败率最大的CI及大小 thisweek_MAX_FAILEDRATE = 0 for ci in CI_NAME_list: if float(ciIndex_thisWeek['%s_failRate' % ci]) > thisweek_MAX_FAILEDRATE: thisweek_MAX_FAILEDRATE = float(ciIndex_thisWeek['%s_failRate' % ci]) max_FAILEDRATE_CI = ci lastweek_MAX_FAILEDRATE = float(ciIndex_lastWeek['%s_failRate' % max_FAILEDRATE_CI]) failed_DIFF_RATE = "%.2f" % float( abs(float(thisweek_MAX_FAILEDRATE) - float(lastweek_MAX_FAILEDRATE)) * 100) if float(thisweek_MAX_FAILEDRATE) - float(lastweek_MAX_FAILEDRATE) > 0: COMMIT_AND_TIME += "<tr align=center><td bgcolor='#d0d0d0'>平均失败率最大的CI及大小</td>" FAILED_INDEX_DIFF = "<td bgcolor='#ff6eb4'>%s: %.2f" % ( max_FAILEDRATE_CI, thisweek_MAX_FAILEDRATE * 100) + "%(↑ " + "%s" % failed_DIFF_RATE + "%)</td>" else: COMMIT_AND_TIME += "<tr align=center><td bgcolor='#d0d0d0'>平均失败率最大的CI及大小</td>" FAILED_INDEX_DIFF = "<td bgcolor='#b5c4b1'>%s: %.2f" % ( max_FAILEDRATE_CI, thisweek_MAX_FAILEDRATE * 100) + "%(↓ " + "%s" % failed_DIFF_RATE + "%)</td>" COMMIT_AND_TIME += FAILED_INDEX_DIFF COMMIT_AND_TIME += "</tr></table>" #效率云CI由于单测随机挂RERUN占比 thisWeek_RERUN_TABLE = "<table border='1' align=center> <caption><font size='3'><b>效率云CI由于单测随机挂RERUN占比</b></font></caption>" thisWeek_RERUN_TABLE += "<tr align=center><td bgcolor='#d0d0d0'>CI名称</td><td>整体</td><td>PR-CI-Coverage</td><td> PR-CI-Py3</td><td>PR-CI-Mac</td><td>PR-CI-Mac-Python3</td><td>PR-CI-Windows</td>" thisWeek_RERUN_TABLE += "</tr><tr align=center><td bgcolor='#d0d0d0'>单测随机挂引起的Rerun</td><td>%s" % thisWeek_rerun_index[ 'all_testfailed_rerunRatio'] thisWeek_RERUN_TABLE += "%</td>" rerun_ci_by_utfail_list = localConfig.cf.get( 'ciIndex', 'rerun_ci_by_utfail').split(',') for ci in rerun_ci_by_utfail_list: thisWeek_RERUN_TABLE += "<td>{}%</td>".format( thisWeek_rerun_index['%s_testfailed_rerunRatio' % ci]) thisWeek_RERUN_TABLE += "</tr></table>" #失败原因占比 EXCODE_TABLE = excode('%s 00:00:00' % ciIndex_thisWeek['startTime'], '%s 00:00:00' % ciIndex_thisWeek['endTime']) #对内关键指标与对内详细指标 KEY_CI_INDEX_TABLE, DETAIL_CI_INDEX_TABLE = get_key_detail_ci_index( ciIndex_thisWeek, ciIndex_lastWeek) #汇总表格 HTML_CONTENT = "<html><body><p>Hi, ALL:</p> <p>本周(%s 00:00:00 ~ %s 00:00:00)CI评价指标详细信息可参考如下表格:</p> <p>CI评价指标的计算方式可见: http://agroup.baidu.com/paddle-ci/md/article/3352500</p><p>现在机器资源如下: V100(coverage/py3) 17台, P4(Inference/CPU) 4台, Mac 8台, Windows 15台</p> %s" % ( ciIndex_thisWeek['startTime'], ciIndex_thisWeek['endTime'], COMMIT_AND_TIME) HTML_CONTENT = HTML_CONTENT + "<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td height='10'></td</tr></table>" \ + KEY_CI_INDEX_TABLE + "<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td height='10'></td</tr></table>" \ + thisWeek_RERUN_TABLE + "<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td height='10'></td</tr></table>" \ + EXCODE_TABLE + "<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td height='10'></td</tr></table>"\ + DETAIL_CI_INDEX_TABLE + "<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td height='10'></td</tr></table>" \ + "<p>如有问题,请反馈到CE and CI值班群(xxx)或联系张春乐.</p> <p>张春乐</p></body></html>" mail = Mail() mail.set_sender('xxxx') mail.set_receivers(['*****@*****.**']) mail.set_title( '效率云%s~%s CI评价指标统计' % (ciIndex_thisWeek['startTime'], ciIndex_thisWeek['endTime'])) mail.set_message(HTML_CONTENT, messageType='html', encoding='gb2312') mail.send()
def sendMonitorMail(content): mail = Mail() mail.set_sender('*****@*****.**') mail.set_receivers(['*****@*****.**']) mail.set_title('gitClone时间超过10min!') mail.set_message(content, messageType='html', encoding='gb2312') mail.send()
def query_balance(self): range = {'from': get_early_time2(), 'to': get_today_time2()} bw_range = {'from': get_early_time(), 'to': get_today_time()} try: bw = self.get_bw(bw_range) customer = self.get_customer() libs = self.get_libs() billing = self.get_billing() dispose = LumDispose(customer, libs) balance = dispose.dispose_balance(bw, billing, range) if balance['calc_balance'] < 50: mail = Mail() mail.send_content_mail(FLOW_MSG % balance['calc_balance'], '余额不足', MAIL_BALANCE_SEND) mail.close() except: if IS_FAIL: mail = Mail() mail.send_content_mail(FAIL_MSG % 'query_balance', FAIL_TITLE_MSG, MAIL_TEST) mail.close()
def main(): global proxy_client, proxy_server LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s" log_filename = 'logs/test_' + time.strftime("%Y%m%d", time.localtime()) + '.log' logging.basicConfig(filename=log_filename, level=logging.INFO, format=LOG_FORMAT) # 当前脚本所在目录路径 path = os.path.dirname(os.path.realpath(__file__)) test_config = TestConfig(path) if len(sys.argv) > 1: report_name = sys.argv[1] if 'ls' == sys.argv[1]: files = getFileName(path + '/config/') print(Util.pretty(files)) return else: report_name = 'default' # 获取测试用例数据 config = test_config.get_test_case(sys.argv) # 是否开启代理 is_open_proxy = config.get('BROWSER').get('proxy') if is_open_proxy: from browsermobproxy import Server bmp_path = config.get('BROWSER').get('bmp_path') logging.info('开启代理 ' + bmp_path) proxy_server = Server(bmp_path) proxy_server.start() proxy_client = proxy_server.create_proxy() # 浏览器类型 browser_type = config.get('BROWSER').get('type') # 是否使用H5测试,并指定移动设备名称 h5 = config.get('BROWSER').get('H5', False) device_name = config.get('BROWSER').get('device_name', 'iPhone 7') # 是否开启无头模式 headless = config.get('BROWSER').get('headless', False) if browser_type == 'Firefox': options = FirefoxOptions() if headless: options.add_argument("-headless") options.page_load_strategy = 'normal' if h5: user_agent = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16" options.set_preference("general.useragent.override", user_agent) if is_open_proxy: options.add_argument('--proxy-server={0}'.format( proxy_client.proxy)) browser = webdriver.Firefox(options=options) elif browser_type == 'Chrome': options = ChromeOptions() if headless: options.add_argument("--window-size=1920,1080") # 设置浏览器分辨率(窗口大小) options.add_argument( "--start-maximized") # 最大化运行(全屏窗口),不设置,获取元素可能会报错 options.add_argument("--disable-extensions") options.add_argument('--no-sandbox') # 取消沙盒模式,浏览器的安全性会降低 # 禁用GPU加速,降低资源损耗,only for Windows but not a valid configuration for Linux OS options.add_argument('--disable-gpu') options.add_argument('--disable-dev-shm-usage') # 解决资源有限的问题 options.add_argument('--lang=en_US') options.add_argument("--headless") options.page_load_strategy = 'normal' if h5: mobileEmulation = {'deviceName': device_name} options.add_experimental_option('mobileEmulation', mobileEmulation) if is_open_proxy: options.add_argument('--proxy-server={0}'.format( proxy_client.proxy)) browser = webdriver.Chrome(options=options) else: print('浏览器' + browser_type + ':类型不支持') return False logging.info('开始使用 ' + browser_type + ' 浏览器进行自动化测试') if is_open_proxy: proxy_client.new_har("req", options={ 'captureHeaders': True, 'captureContent': True }) if browser_type == 'Firefox' and h5: browser.set_window_size(360, 640) else: browser.maximize_window() # 浏览器等待时间 # browser.implicitly_wait(10) url = config.get('WEBSITE').get('url') browser.get(url) if is_open_proxy: Http.logHar(proxy_client.har) # 执行配置的TEST对象 test = config.get('TEST') suite = unittest.TestSuite() m = Menu() for key in test: menus = m.getMenuConfig(config, key) try: if is_open_proxy: test_data = [browser, menus, proxy_client] else: test_data = [browser, menus] suite.addTest( ParametrizedTestCase.parametrize(Action, 'test_menu', param=test_data)) except AssertExcetion: print(key + " 断言失败") # 是否生成报告,默认开启调试模式,不生成报告 debug = config.get('DEBUG', True) if debug: runner = unittest.TextTestRunner() runner.run(suite) else: # 报告是否含有截图,DEBUG为False且IMAGE设置为True时生效 image = config.get('IMAGE', False) report_path = path + '/reports/' report_file = report_name + "_" + time.strftime( "%Y%m%d", time.localtime()) + '.html' fp = open(report_path + report_file, 'wb') report_title = '你的测试报告' report_desc = '使用配置:' + report_name + '生成的测试报告' runner = HTMLTestRunner(stream=fp, verbosity=2, images=image, title=report_title, description=report_desc, tester='pystest') runner.run(suite) fp.close() sleep(5) browser.quit() # send mail or not mail = config.get('MAIL') if not debug and mail and mail.get('SEND'): email_title = report_title email_content = report_desc smtp = Mail(config.get('MAIL'), report_path) smtp.connect() smtp.login() smtp.send(email_title, email_content, report_file) smtp.quit() if is_open_proxy: proxy_client.close() proxy_server.stop()
def sendCloseMail(content, repo, receivers, CloseDay): mail = Mail() mail.set_sender('*****@*****.**') mail.set_receivers(receivers) mail.set_title('%s repo关闭超过%s天未更新的issue/pr通知' % (repo, CloseDay)) mail.set_message(content, messageType='html', encoding='gb2312') mail.send()
def sendMail(self, mailContent): """ this function will send alarm mail. """ mail = Mail() mail.set_sender('*****@*****.**') mail.set_receivers(['*****@*****.**']) mail.set_title('[告警]任务等待超时') mail.set_message(mailContent, messageType='html', encoding='gb2312') mail.send()
def test(): mail = Mail() mail.send_content_mail('你好世界', '测试', MAIL_TEST) # path = 'C:\\Users\\27390\\Desktop\\agent-reptile\\export\\Flow_2019_02_01_2019_03_28.csv' # mail.send_file_mail('上个月的流量报表', '流量报表', path, MAIL_TEST) mail.close()
# coding=utf-8 # Time : 2019/6/19 15:00 # Author : zhonglin.zhang # File : notice.py.py from datetime import datetime from utils import get_ini_value from utils.jenkins import Jenkins from utils.mail import Mail if __name__ == '__main__': now = datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S") mail_info = dict() mail_info['subject'] = '接口自动化测试报告' + now mail_info['to'] = get_ini_value('EMAIL', 'to') mail_info['from'] = get_ini_value('EMAIL', 'from') mail_info['cc'] = get_ini_value('EMAIL', 'cc') mail_info['result'] = Jenkins().get_result_info() ml = Mail(mail_info) ml.send_html_email()