• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2024 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import os
17import stat
18
19
20def sort_by_kit(result_info_list: list, output_path):
21    """
22    Description:列表按kit排序
23    """
24    sort_by_kit_list = []
25    if 1 == len(result_info_list):
26        sort_by_kit_list.extend(result_info_list)
27        write_data_in_md(result_info_list[0].kit_name, result_info_list, output_path)
28        return sort_by_kit_list
29    if result_info_list:
30        kit_list = sorted(result_info_list, key=lambda obj: obj.kit_name)
31        first_kit_name = kit_list[0].kit_name
32        same_element = []
33        for obj_element in kit_list:
34            if obj_element.kit_name == first_kit_name:
35                same_element.append(obj_element)
36            else:
37                sorted_data = sort_by_file_path(same_element)
38                write_data_in_md(first_kit_name, sorted_data, output_path)
39                sort_by_kit_list.extend(sorted_data)
40                first_kit_name = obj_element.kit_name
41                same_element = [obj_element]
42        if same_element:
43            sorted_data = sort_by_file_path(same_element)
44            write_data_in_md(first_kit_name, sorted_data, output_path)
45            sort_by_kit_list.extend(sorted_data)
46
47    return sort_by_kit_list
48
49
50def sort_by_file_path(result_info_list_kit: list):
51    """
52    Description:列表按文件路径排序
53    """
54    sorted_by_file_list = []
55    if 1 == len(result_info_list_kit):
56        sorted_by_file_list.extend(result_info_list_kit)
57        return sorted_by_file_list
58    if result_info_list_kit:
59        file_list = sorted(result_info_list_kit, key=lambda obj: obj.api_file_path)
60        first_file_name = file_list[0].api_file_path
61        same_element = []
62        for obj_element in file_list:
63            if obj_element.api_file_path == first_file_name:
64                same_element.append(obj_element)
65            else:
66                sorted_data = sort_by_type(same_element)
67                sorted_by_file_list.extend(sorted_data)
68                first_file_name = obj_element.api_file_path
69                same_element = [obj_element]
70        if same_element:
71            sorted_data = sort_by_type(same_element)
72            sorted_by_file_list.extend(sorted_data)
73
74    return sorted_by_file_list
75
76
77def sort_by_type(result_info_list_file: list):
78    """
79    Description:列表按操作类型排序
80    """
81    sorted_by_type_list = []
82    if result_info_list_file:
83        sorted_by_type_list = sorted(result_info_list_file, key=lambda obj: obj.diff_type.name)
84
85    return sorted_by_type_list
86
87
88def change_to_md_data(data):
89    """
90    Description:将列表中字典元素数据转为str的数据
91    """
92    headers = list(data[0].keys())
93    markdown_table = '{}{}{}'.format("|", "|".join(headers), "|\n")
94    markdown_table += '{}{}{}'.format("|", "|".join(["---"] * len(headers)), "|\n")
95    for element in data:
96        markdown_table += '{}{}{}'.format("|", "|".join(str(element[header]) for header in headers), "|\n")
97    return markdown_table
98
99
100def change_list_obj_to_dict(list_of_obj: list):
101    """
102    Description:将列表中对象元素数据转为字典元素数据
103    """
104    data_list = []
105    if list_of_obj:
106        for obj_element in list_of_obj:
107            obj_dict = {
108                '操作': obj_element.diff_type.name,
109                '旧版本': obj_element.old_api_full_text.replace('\n', '<br />'),
110                '新版本': obj_element.new_api_full_text.replace('\n', '<br />'),
111                '.h文件': obj_element.api_file_path
112            }
113            data_list.append(obj_dict)
114
115    return data_list
116
117
118def write_data_in_md(kit_name: str, write_data: list, output_path):
119    """
120    Description:生成.md表格
121    """
122    if not kit_name:
123        kit_name = r'nullOfKit'
124    file_name = '{}{}'.format(kit_name, '.md')
125    list_element_dict = change_list_obj_to_dict(write_data)
126    md_data = change_to_md_data(list_element_dict)
127    if md_data:
128        diff_str = r'diff合集'
129        path_str = os.path.abspath(os.path.join(output_path, diff_str))
130        if not os.path.exists(path_str):
131            os.mkdir(path_str)
132        md_name = os.path.abspath(os.path.join(path_str, file_name))
133        modes = stat.S_IRWXO | stat.S_IRWXG | stat.S_IRWXU
134        fd = os.open(md_name, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, mode=modes)
135        os.write(fd, md_data.encode())
136        os.close(fd)
137
138
139def write_md_entrance(result_info_list, output_path):
140    """
141    Description:将数据生成.md表格入口
142    """
143    sorted_data = sort_by_kit(result_info_list, output_path)
144    return sorted_data
145