13af6ab5fSopenharmony_ci#!/usr/bin/env python3
23af6ab5fSopenharmony_ci# -*- coding: utf-8 -*-
33af6ab5fSopenharmony_ci#
43af6ab5fSopenharmony_ci# Copyright (c) 2023 Huawei Device Co., Ltd.
53af6ab5fSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
63af6ab5fSopenharmony_ci# you may not use this file except in compliance with the License.
73af6ab5fSopenharmony_ci# You may obtain a copy of the License at
83af6ab5fSopenharmony_ci#
93af6ab5fSopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0
103af6ab5fSopenharmony_ci#
113af6ab5fSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
123af6ab5fSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
133af6ab5fSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
143af6ab5fSopenharmony_ci# See the License for the specific language governing permissions and
153af6ab5fSopenharmony_ci# limitations under the License.
163af6ab5fSopenharmony_ci
173af6ab5fSopenharmony_ciimport os
183af6ab5fSopenharmony_ciimport smtplib
193af6ab5fSopenharmony_ci
203af6ab5fSopenharmony_cifrom email.mime.image import MIMEImage
213af6ab5fSopenharmony_cifrom email.mime.multipart import MIMEMultipart
223af6ab5fSopenharmony_cifrom email.mime.text import MIMEText
233af6ab5fSopenharmony_ciimport yaml
243af6ab5fSopenharmony_ci
253af6ab5fSopenharmony_ci
263af6ab5fSopenharmony_cidef add_content(content, file_name, test_part):
273af6ab5fSopenharmony_ci    if file_name == "":
283af6ab5fSopenharmony_ci        content += f'<p style="text-align:center;color:red;font-size:25px"> {test_part} not complete yet </p>'
293af6ab5fSopenharmony_ci        return content
303af6ab5fSopenharmony_ci    if not os.path.exists(file_name):
313af6ab5fSopenharmony_ci        content += f'<p style="text-align:center;color:red;font-size:25px"> {test_part} run failed </p>'
323af6ab5fSopenharmony_ci        return content
333af6ab5fSopenharmony_ci    with open(file_name, 'r', encoding='utf-8') as f:
343af6ab5fSopenharmony_ci        content += f.read()
353af6ab5fSopenharmony_ci        return content
363af6ab5fSopenharmony_ci
373af6ab5fSopenharmony_ci
383af6ab5fSopenharmony_cidef add_attachment(msg, file_list):
393af6ab5fSopenharmony_ci    for file in file_list:
403af6ab5fSopenharmony_ci        if os.path.exists(file):
413af6ab5fSopenharmony_ci            with open(file, 'rb') as f:
423af6ab5fSopenharmony_ci                attachment = MIMEText(f.read(), 'base64', 'utf-8')
433af6ab5fSopenharmony_ci                attachment['Content-Disposition'] = f'attachment; filename="{os.path.basename(file)}"'
443af6ab5fSopenharmony_ci                msg.attach(attachment)
453af6ab5fSopenharmony_ci
463af6ab5fSopenharmony_ci
473af6ab5fSopenharmony_cidef add_image(msg, img_dic):
483af6ab5fSopenharmony_ci    for path in img_dic:
493af6ab5fSopenharmony_ci        if os.path.exists(path):
503af6ab5fSopenharmony_ci            with open(path, 'rb') as f:
513af6ab5fSopenharmony_ci                img = MIMEImage(f.read())
523af6ab5fSopenharmony_ci                img.add_header('Content-ID', img_dic[path])
533af6ab5fSopenharmony_ci                msg.attach(img)
543af6ab5fSopenharmony_ci
553af6ab5fSopenharmony_ci
563af6ab5fSopenharmony_cidef send_email():
573af6ab5fSopenharmony_ci    os.chdir(os.path.dirname(os.path.abspath(__file__)))
583af6ab5fSopenharmony_ci    with open(r"../config.yaml", 'r') as f:
593af6ab5fSopenharmony_ci        data = yaml.safe_load(f.read())
603af6ab5fSopenharmony_ci
613af6ab5fSopenharmony_ci    user_name = data["user_name"]
623af6ab5fSopenharmony_ci    sender = data["sender_email_address"]
633af6ab5fSopenharmony_ci    auth_code = data["auth_code"]
643af6ab5fSopenharmony_ci    receiver = data["receiver_list"]
653af6ab5fSopenharmony_ci    smtp_server = data["smtp_server"]
663af6ab5fSopenharmony_ci    smtp_port = data["smtp_port"]
673af6ab5fSopenharmony_ci    xts_test = data["xts_report_file"]
683af6ab5fSopenharmony_ci    sdk_test = data["sdk_report_file"]
693af6ab5fSopenharmony_ci    perf_test = data["perf_report_file"]
703af6ab5fSopenharmony_ci    commit_log = data["commit_log_report_file"]
713af6ab5fSopenharmony_ci    attachment_files = data["attatchment_files"]
723af6ab5fSopenharmony_ci    image_files = data["image_files"]
733af6ab5fSopenharmony_ci
743af6ab5fSopenharmony_ci    msg = MIMEMultipart()
753af6ab5fSopenharmony_ci    msg['From'] = sender
763af6ab5fSopenharmony_ci    msg['To'] = ", ".join(receiver)
773af6ab5fSopenharmony_ci    msg['Subject'] = "Arkcompiler Test"
783af6ab5fSopenharmony_ci
793af6ab5fSopenharmony_ci    html = ""
803af6ab5fSopenharmony_ci    dividing_line = '<hr align="center" width="80%" color="gray" size="8">'
813af6ab5fSopenharmony_ci    html = add_content(html, xts_test, "xts_test")
823af6ab5fSopenharmony_ci    html += dividing_line
833af6ab5fSopenharmony_ci    html = add_content(html, sdk_test, "sdk_test")
843af6ab5fSopenharmony_ci    html += dividing_line
853af6ab5fSopenharmony_ci    html = add_content(html, perf_test, "perf_test")
863af6ab5fSopenharmony_ci    msg.attach(MIMEText(html, 'html', 'utf-8'))
873af6ab5fSopenharmony_ci    html = add_content(html, commit_log, "commit_log")
883af6ab5fSopenharmony_ci    msg.attach(MIMEText(html, 'html', 'utf-8'))
893af6ab5fSopenharmony_ci    add_attachment(msg, attachment_files)
903af6ab5fSopenharmony_ci    add_image(msg, image_files)
913af6ab5fSopenharmony_ci    smtp = smtplib.SMTP(smtp_server, smtp_port)
923af6ab5fSopenharmony_ci    smtp.login(user_name, auth_code)
933af6ab5fSopenharmony_ci    smtp.sendmail(sender, receiver, msg.as_string())
943af6ab5fSopenharmony_ci    smtp.quit()
953af6ab5fSopenharmony_ci
963af6ab5fSopenharmony_ci
973af6ab5fSopenharmony_ciif __name__ == "__main__":
983af6ab5fSopenharmony_ci    send_email()