1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3"""
4Copyright (c) 2020-2021 Huawei Device Co., Ltd.
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9  http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16"""
17
18import os
19import sys
20import subprocess
21import hashlib
22import json5
23
24
25def get_file_md5(file_path):
26    hash_md5 = hashlib.md5()
27    with open(file_path, "rb") as f:
28        for chunk in iter(lambda: f.read(4096), b""):
29            hash_md5.update(chunk)
30    return hash_md5.hexdigest()
31
32
33def get_hvigor_version(json_file):
34    with open(json_file, 'r') as f:
35        data = json5.load(f)
36        return data.get('hvigorVersion')
37    return "0.0.0"
38
39
40def output_unmatched_project(prject_list, filename):
41    print("")
42    print(
43        "Error: The {} in the following directory does not meet the requirements:"
44        .format(filename))
45    for prj in prject_list:
46        print(prj[0], prj[1])
47    print("")
48
49
50def check_hvigor_wrapper_js(acts_root_dir, hvigor_prj_list):
51    unmatch_info = []
52    baseline_file = os.path.join(acts_root_dir, 'arkcompiler/ecmanewfeatures/hvigor/hvigor-wrapper.js')
53    baseline_md5 = get_file_md5(os.path.join(baseline_file))
54    for dir in hvigor_prj_list:
55        filename = os.path.join(dir, 'hvigor', 'hvigor-wrapper.js')
56        md5 = get_file_md5(filename)
57        if md5 != baseline_md5:
58            unmatch_info.append((md5, filename))
59
60    if len(unmatch_info):
61        output_unmatched_project(unmatch_info, 'hvigor-wrapper.js')
62        print('Please copy from {}'.format(baseline_file))
63        return False
64    return True
65
66def check_hvigor_version(hvigor_prj_list):
67    unmatch_prj_list = []
68    baseline_version = '4.0.5'
69    for dir in hvigor_prj_list:
70        filename = os.path.join(dir, 'hvigor', 'hvigor-config.json5')
71        version = get_hvigor_version(filename)
72        if version != baseline_version:
73            unmatch_prj_list.append((version, filename))
74
75    if len(unmatch_prj_list):
76        output_unmatched_project(unmatch_prj_list, 'hvigor-config.json5')
77        print("Plesse use {}".format(baseline_version))
78        return False
79    return True
80
81def check_hvigorw_bat(acts_root_dir, hvigor_prj_list):
82    unmatch_info = []
83    baseline_file = os.path.join(acts_root_dir, 'arkcompiler/ecmanewfeatures/hvigorw.bat')
84    baseline_md5 = get_file_md5(os.path.join(baseline_file))
85    for dir in hvigor_prj_list:
86        filename = os.path.join(dir, 'hvigorw.bat')
87        md5 = get_file_md5(filename)
88        if md5 != baseline_md5:
89            unmatch_info.append((md5, filename))
90
91    if len(unmatch_info):
92        output_unmatched_project(unmatch_info, 'hvigorw.bat')
93        print('Please copy from {}'.format(baseline_file))
94        return False
95    return True
96
97
98def get_hvigor_prject_list(directory):
99    hvigor_prj_list = []
100    for root, dirs, files in os.walk(directory):
101        for dir in dirs:
102            if dir == 'hvigor':
103                hvigor_prj_list.append(root)
104    return hvigor_prj_list
105
106
107if __name__ == "__main__":
108    this_file = os.path.realpath(__file__)
109    acts_root_dir = os.path.dirname(this_file)
110    hvigor_prj_list = get_hvigor_prject_list(acts_root_dir)
111
112    js_valid = check_hvigor_wrapper_js(acts_root_dir, hvigor_prj_list)
113    json_valid = check_hvigor_version(hvigor_prj_list)
114    bat_valid = check_hvigorw_bat(acts_root_dir, hvigor_prj_list)
115
116    if not js_valid or not json_valid or not bat_valid:
117        sys.exit(1)
118
119    sys.exit(0)