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(hvigor_prj_list):
51    unmatch_info = []
52    baseline_md5 = "558a3eec8ee9e753b63f99abe79c0ec3"
53    for dir in hvigor_prj_list:
54        filename = os.path.join(dir, 'hvigor', 'hvigor-wrapper.js')
55        md5 = get_file_md5(filename)
56        if md5 != baseline_md5:
57            unmatch_info.append((md5, filename))
58
59    if len(unmatch_info):
60        output_unmatched_project(unmatch_info, 'hvigor-wrapper.js')
61        print('Please copy from {}'.format('test/xts/acts/arkcompiler/ecmanewfeatures/hvigor/hvigor-wrapper.js'))
62        return False
63    return True
64
65
66def check_hvigor_version(hvigor_prj_list):
67    unmatch_prj_list = []
68    baseline_version = '4.0.2'
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
81
82def get_hvigor_prject_list(directory):
83    hvigor_prj_list = []
84    for root, dirs, files in os.walk(directory):
85        for dir in dirs:
86            if dir == 'hvigor':
87                hvigor_prj_list.append(root)
88    return hvigor_prj_list
89
90
91if __name__ == "__main__":
92    this_file = os.path.realpath(__file__)
93    curr_dir = os.path.dirname(this_file)
94    hvigor_prj_list = get_hvigor_prject_list(curr_dir)
95
96    js_valid = check_hvigor_wrapper_js(hvigor_prj_list)
97    json_valid = check_hvigor_version(hvigor_prj_list)
98
99    if not js_valid or not json_valid:
100        sys.exit(1)
101
102    sys.exit(0)