1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3#
4# Copyright (c) 2023 Huawei Device Co., Ltd.
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17import os
18import smtplib
19
20from email.mime.image import MIMEImage
21from email.mime.multipart import MIMEMultipart
22from email.mime.text import MIMEText
23import yaml
24
25
26def add_content(content, file_name, test_part):
27    if file_name == "":
28        content += f'<p style="text-align:center;color:red;font-size:25px"> {test_part} not complete yet </p>'
29        return content
30    if not os.path.exists(file_name):
31        content += f'<p style="text-align:center;color:red;font-size:25px"> {test_part} run failed </p>'
32        return content
33    with open(file_name, 'r', encoding='utf-8') as f:
34        content += f.read()
35        return content
36
37
38def add_attachment(msg, file_list):
39    for file in file_list:
40        if os.path.exists(file):
41            with open(file, 'rb') as f:
42                attachment = MIMEText(f.read(), 'base64', 'utf-8')
43                attachment['Content-Disposition'] = f'attachment; filename="{os.path.basename(file)}"'
44                msg.attach(attachment)
45
46
47def add_image(msg, img_dic):
48    for path in img_dic:
49        if os.path.exists(path):
50            with open(path, 'rb') as f:
51                img = MIMEImage(f.read())
52                img.add_header('Content-ID', img_dic[path])
53                msg.attach(img)
54
55
56def send_email():
57    os.chdir(os.path.dirname(os.path.abspath(__file__)))
58    with open(r"../config.yaml", 'r') as f:
59        data = yaml.safe_load(f.read())
60
61    user_name = data["user_name"]
62    sender = data["sender_email_address"]
63    auth_code = data["auth_code"]
64    receiver = data["receiver_list"]
65    smtp_server = data["smtp_server"]
66    smtp_port = data["smtp_port"]
67    xts_test = data["xts_report_file"]
68    sdk_test = data["sdk_report_file"]
69    perf_test = data["perf_report_file"]
70    commit_log = data["commit_log_report_file"]
71    attachment_files = data["attatchment_files"]
72    image_files = data["image_files"]
73
74    msg = MIMEMultipart()
75    msg['From'] = sender
76    msg['To'] = ", ".join(receiver)
77    msg['Subject'] = "Arkcompiler Test"
78
79    html = ""
80    dividing_line = '<hr align="center" width="80%" color="gray" size="8">'
81    html = add_content(html, xts_test, "xts_test")
82    html += dividing_line
83    html = add_content(html, sdk_test, "sdk_test")
84    html += dividing_line
85    html = add_content(html, perf_test, "perf_test")
86    msg.attach(MIMEText(html, 'html', 'utf-8'))
87    html = add_content(html, commit_log, "commit_log")
88    msg.attach(MIMEText(html, 'html', 'utf-8'))
89    add_attachment(msg, attachment_files)
90    add_image(msg, image_files)
91    smtp = smtplib.SMTP(smtp_server, smtp_port)
92    smtp.login(user_name, auth_code)
93    smtp.sendmail(sender, receiver, msg.as_string())
94    smtp.quit()
95
96
97if __name__ == "__main__":
98    send_email()