1c1ed15f1Sopenharmony_ci#!/usr/bin/env python
2c1ed15f1Sopenharmony_ci# coding: utf-8
3c1ed15f1Sopenharmony_ci
4c1ed15f1Sopenharmony_ci"""
5c1ed15f1Sopenharmony_ciCopyright (c) 2023 Huawei Device Co., Ltd.
6c1ed15f1Sopenharmony_ciLicensed under the Apache License, Version 2.0 (the "License");
7c1ed15f1Sopenharmony_ciyou may not use this file except in compliance with the License.
8c1ed15f1Sopenharmony_ciYou may obtain a copy of the License at
9c1ed15f1Sopenharmony_ci
10c1ed15f1Sopenharmony_ci    http://www.apache.org/licenses/LICENSE-2.0
11c1ed15f1Sopenharmony_ci
12c1ed15f1Sopenharmony_ciUnless required by applicable law or agreed to in writing, software
13c1ed15f1Sopenharmony_cidistributed under the License is distributed on an "AS IS" BASIS,
14c1ed15f1Sopenharmony_ciWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15c1ed15f1Sopenharmony_ciSee the License for the specific language governing permissions and
16c1ed15f1Sopenharmony_cilimitations under the License.
17c1ed15f1Sopenharmony_ci
18c1ed15f1Sopenharmony_ci"""
19c1ed15f1Sopenharmony_ci
20c1ed15f1Sopenharmony_ciimport json
21c1ed15f1Sopenharmony_ciimport os
22c1ed15f1Sopenharmony_ciimport subprocess
23c1ed15f1Sopenharmony_ci
24c1ed15f1Sopenharmony_ci
25c1ed15f1Sopenharmony_cidef read_json_file(input_file):
26c1ed15f1Sopenharmony_ci    data = None
27c1ed15f1Sopenharmony_ci    try:
28c1ed15f1Sopenharmony_ci        with open(input_file, 'r') as input_f:
29c1ed15f1Sopenharmony_ci            data = json.load(input_f)
30c1ed15f1Sopenharmony_ci    except json.decoder.JSONDecodeError:
31c1ed15f1Sopenharmony_ci        print('The file \'{}\' format is incorrect.'.format(input_file))
32c1ed15f1Sopenharmony_ci        raise
33c1ed15f1Sopenharmony_ci    except:
34c1ed15f1Sopenharmony_ci        print('read file \'{}\' failed.'.format(input_file))
35c1ed15f1Sopenharmony_ci        raise
36c1ed15f1Sopenharmony_ci    return data
37c1ed15f1Sopenharmony_ci
38c1ed15f1Sopenharmony_ci
39c1ed15f1Sopenharmony_cidef read_file(input_file):
40c1ed15f1Sopenharmony_ci    lines = []
41c1ed15f1Sopenharmony_ci    with open(input_file, 'r') as fp:
42c1ed15f1Sopenharmony_ci        for line in fp.readlines():
43c1ed15f1Sopenharmony_ci            lines.append(line.strip())
44c1ed15f1Sopenharmony_ci    return lines
45c1ed15f1Sopenharmony_ci
46c1ed15f1Sopenharmony_ci
47c1ed15f1Sopenharmony_cidef run_command(in_cmd):
48c1ed15f1Sopenharmony_ci    ret = subprocess.run(in_cmd, shell=False).returncode
49c1ed15f1Sopenharmony_ci    if ret != 0:
50c1ed15f1Sopenharmony_ci        raise Exception(ret)
51c1ed15f1Sopenharmony_ci
52c1ed15f1Sopenharmony_ci
53c1ed15f1Sopenharmony_cidef check_empty_row(policy_file):
54c1ed15f1Sopenharmony_ci    err = 0
55c1ed15f1Sopenharmony_ci    with open(policy_file, 'r') as fp:
56c1ed15f1Sopenharmony_ci        lines = fp.readlines()
57c1ed15f1Sopenharmony_ci        if len(lines) == 0:
58c1ed15f1Sopenharmony_ci            return 0
59c1ed15f1Sopenharmony_ci        last_line = lines[-1]
60c1ed15f1Sopenharmony_ci        if '\n' not in last_line:
61c1ed15f1Sopenharmony_ci            print("".join([policy_file, " : need an empty line at end\n"]))
62c1ed15f1Sopenharmony_ci            err = 1
63c1ed15f1Sopenharmony_ci    return err
64c1ed15f1Sopenharmony_ci
65c1ed15f1Sopenharmony_ci
66c1ed15f1Sopenharmony_cidef traverse_folder_in_type(search_dir, file_suffix):
67c1ed15f1Sopenharmony_ci    policy_file_list = []
68c1ed15f1Sopenharmony_ci    flag = 0
69c1ed15f1Sopenharmony_ci    for root, _, files in sorted(os.walk(search_dir)):
70c1ed15f1Sopenharmony_ci        for each_file in files:
71c1ed15f1Sopenharmony_ci            if each_file.endswith(file_suffix):
72c1ed15f1Sopenharmony_ci                path = os.path.join(root, each_file)
73c1ed15f1Sopenharmony_ci                flag |= check_empty_row(path)
74c1ed15f1Sopenharmony_ci                policy_file_list.append(path)
75c1ed15f1Sopenharmony_ci    policy_file_list.sort()
76c1ed15f1Sopenharmony_ci    return policy_file_list, flag
77c1ed15f1Sopenharmony_ci
78c1ed15f1Sopenharmony_ci
79c1ed15f1Sopenharmony_cidef traverse_file_in_each_type(dir_list, file_suffix):
80c1ed15f1Sopenharmony_ci    policy_files_list = []
81c1ed15f1Sopenharmony_ci    err = 0
82c1ed15f1Sopenharmony_ci    folder_list = dir_list.split(":")
83c1ed15f1Sopenharmony_ci    for folder in folder_list:
84c1ed15f1Sopenharmony_ci        type_file_list, flag = traverse_folder_in_type(
85c1ed15f1Sopenharmony_ci            folder, file_suffix)
86c1ed15f1Sopenharmony_ci        err |= flag
87c1ed15f1Sopenharmony_ci        if len(type_file_list) == 0:
88c1ed15f1Sopenharmony_ci            continue
89c1ed15f1Sopenharmony_ci        policy_files_list.extend(type_file_list)
90c1ed15f1Sopenharmony_ci    if err:
91c1ed15f1Sopenharmony_ci        raise Exception(err)
92c1ed15f1Sopenharmony_ci    return policy_files_list
93