• 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 zipfile
17import os
18import re
19import openpyxl as op
20from typedef.compare import Output, DoesItExist
21
22
23output_result_list = []
24
25
26def unzip(folder_path):
27    # 获取文件夹中所有的 zip 文件
28    zip_files = [f for f in os.listdir(folder_path) if f.endswith('.zip')]
29
30    # 遍历每个 zip 文件并解压到目标文件夹
31    for zip_file in zip_files:
32        with zipfile.ZipFile(os.path.join(folder_path, zip_file), 'r') as zip_ref:
33            zip_ref.extractall(path=folder_path)
34
35
36def generate_excel(output_file_path):
37    data = []
38    for output_result in output_result_list:
39        info_data = []
40        info_data.append(output_result.platform)
41        info_data.append(output_result.file_path)
42        info_data.append(output_result.old_is_exist)
43        info_data.append(output_result.new_is_exist)
44        data.append(info_data)
45    wb = op.Workbook()
46    ws = wb['Sheet']
47    ws.append(['序号', '平台', '文件路径', '旧版本是否存在', '新版本是否存在'])
48    number = 1
49    for title in data:
50        d = number, title[0], title[1], title[2], title[3]
51        ws.append(d)
52        number += 1
53    wb.save(output_file_path)
54
55
56def do_diff(old_dir, new_dir, system_type):
57    old_file_list = os.listdir(old_dir)
58    new_file_list = os.listdir(new_dir)
59    diff_list(old_file_list, new_file_list, old_dir, new_dir, system_type)
60
61
62def diff_list(old_file_list, new_file_list, old_dir, new_dir, system_type):
63    all_list = set(old_file_list + new_file_list)
64    if len(all_list) == 0:
65        return
66    for target_file in all_list:
67        if (target_file in old_file_list
68                and target_file not in new_file_list):
69            diff_file_path = os.path.join(old_dir, target_file)
70            del_old_file(diff_file_path, system_type)
71        elif (target_file in new_file_list
72                and target_file not in old_file_list):
73            diff_file_path = os.path.join(new_dir, target_file)
74            add_new_file(diff_file_path, system_type)
75        else:
76            # 同时存在的文件夹
77            if os.path.isdir(os.path.join(new_dir, target_file)):
78                do_diff(os.path.join(old_dir, target_file), os.path.join(new_dir, target_file), system_type)
79            # 同时存在的文件
80            else:
81                target_path = extract_file_path(os.path.join(new_dir, target_file))
82                out_put = Output(system_type, target_path, DoesItExist.EXIST.value, DoesItExist.EXIST.value)
83                output_result_list.append(out_put)
84
85
86def add_new_file(diff_file_path, system_type):
87    if os.path.isdir(diff_file_path):
88        add_file(diff_file_path, system_type)
89    else:
90        target_path = extract_file_path(diff_file_path)
91        out_put = Output(system_type, target_path, DoesItExist.ABSENT.value, DoesItExist.EXIST.value)
92        output_result_list.append(out_put)
93
94
95def del_old_file(diff_file_path, system_type):
96    if os.path.isdir(diff_file_path):
97        del_file(diff_file_path, system_type)
98    else:
99        target_path = extract_file_path(diff_file_path)
100        out_put = Output(system_type, target_path, DoesItExist.EXIST.value, DoesItExist.ABSENT.value)
101        output_result_list.append(out_put)
102
103
104def del_file(dir_path, system_type):
105    file_list = os.listdir(dir_path)
106    for file in file_list:
107        file_path = os.path.join(dir_path, file)
108        if os.path.isdir(file_path):
109            del_file(file_path, system_type)
110        else:
111            target_path = extract_file_path(file_path)
112            out_put = Output(system_type, target_path, DoesItExist.EXIST.value, DoesItExist.ABSENT.value)
113            output_result_list.append(out_put)
114
115
116def add_file(dir_path, system_type):
117    file_list = os.listdir(dir_path)
118    for file in file_list:
119        file_path = os.path.join(dir_path, file)
120        if os.path.isdir(file_path):
121            add_file(file_path, system_type)
122        else:
123            target_path = extract_file_path(file_path)
124            out_put = Output(system_type, target_path, DoesItExist.ABSENT.value, DoesItExist.EXIST.value)
125            output_result_list.append(out_put)
126
127
128def extract_file_path(file_path):
129    if len(file_path) == 0:
130        return ''
131    pattern = re.compile("ohos-sdk(.*)")
132    result = re.search(pattern, file_path).group(0)
133    return result
134
135
136def start_do_diff(old_file_path, new_file_path, output_file_path):
137    old_platform = os.path.basename(old_file_path)
138    system_type = old_platform
139    unzip(old_file_path)
140    unzip(new_file_path)
141    do_diff(old_file_path, new_file_path, system_type)
142    # 输出Excel
143    if os.path.isdir(output_file_path):
144        output_file_path = os.path.join(output_file_path, 'diff.xlsx')
145    generate_excel(output_file_path)
146