1c1ed15f1Sopenharmony_ci#!/usr/bin/env python
2c1ed15f1Sopenharmony_ci# coding: utf-8
3c1ed15f1Sopenharmony_ci
4c1ed15f1Sopenharmony_ci"""
5c1ed15f1Sopenharmony_ciCopyright (c) 2021-2022 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 os
21c1ed15f1Sopenharmony_ciimport argparse
22c1ed15f1Sopenharmony_ciimport re
23c1ed15f1Sopenharmony_ciimport shutil
24c1ed15f1Sopenharmony_ciimport subprocess
25c1ed15f1Sopenharmony_ciimport sys
26c1ed15f1Sopenharmony_ciimport platform
27c1ed15f1Sopenharmony_cifrom collections import defaultdict
28c1ed15f1Sopenharmony_ci
29c1ed15f1Sopenharmony_ci
30c1ed15f1Sopenharmony_cidef parse_args():
31c1ed15f1Sopenharmony_ci    parser = argparse.ArgumentParser()
32c1ed15f1Sopenharmony_ci    parser.add_argument(
33c1ed15f1Sopenharmony_ci        '--dst-dir', help='the output dest path', required=True)
34c1ed15f1Sopenharmony_ci    parser.add_argument('--tool-path',
35c1ed15f1Sopenharmony_ci                        help='the sefcontext_compile bin path', required=True)
36c1ed15f1Sopenharmony_ci    parser.add_argument('--policy-file',
37c1ed15f1Sopenharmony_ci                        help='the policy.31 file', required=True)
38c1ed15f1Sopenharmony_ci    parser.add_argument('--source-root-dir',
39c1ed15f1Sopenharmony_ci                        help='prj root path', required=True)
40c1ed15f1Sopenharmony_ci    parser.add_argument('--policy_dir_list',
41c1ed15f1Sopenharmony_ci                        help='policy dirs need to be included', required=True)
42c1ed15f1Sopenharmony_ci    parser.add_argument('--components',
43c1ed15f1Sopenharmony_ci                        help='system or vendor or default', required=True)
44c1ed15f1Sopenharmony_ci    return parser.parse_args()
45c1ed15f1Sopenharmony_ci
46c1ed15f1Sopenharmony_ci
47c1ed15f1Sopenharmony_cidef run_command(in_cmd):
48c1ed15f1Sopenharmony_ci    cmdstr = " ".join(in_cmd)
49c1ed15f1Sopenharmony_ci    ret = subprocess.run(cmdstr, shell=True).returncode
50c1ed15f1Sopenharmony_ci    if ret != 0:
51c1ed15f1Sopenharmony_ci        raise Exception(ret)
52c1ed15f1Sopenharmony_ci
53c1ed15f1Sopenharmony_ci
54c1ed15f1Sopenharmony_cidef traverse_folder_in_type(search_dir_list, file_suffix):
55c1ed15f1Sopenharmony_ci    """
56c1ed15f1Sopenharmony_ci    for special folder search_dir, find all files endwith file_suffix.
57c1ed15f1Sopenharmony_ci    :param search_dir: path to search
58c1ed15f1Sopenharmony_ci    :param file_suffix: postfix of file name
59c1ed15f1Sopenharmony_ci    :return: file list
60c1ed15f1Sopenharmony_ci    """
61c1ed15f1Sopenharmony_ci    flag = 0
62c1ed15f1Sopenharmony_ci    policy_file_list = []
63c1ed15f1Sopenharmony_ci
64c1ed15f1Sopenharmony_ci    for item in search_dir_list:
65c1ed15f1Sopenharmony_ci        for root, _, files in sorted(os.walk(item)):
66c1ed15f1Sopenharmony_ci            filtered_files = [f for f in files if f.endswith(file_suffix)]
67c1ed15f1Sopenharmony_ci            for each_file in filtered_files:
68c1ed15f1Sopenharmony_ci                file_list_path = os.path.join(root, each_file)
69c1ed15f1Sopenharmony_ci                flag |= check_contexts_file(file_list_path)
70c1ed15f1Sopenharmony_ci                policy_file_list.append(file_list_path)
71c1ed15f1Sopenharmony_ci
72c1ed15f1Sopenharmony_ci    if flag:
73c1ed15f1Sopenharmony_ci        raise Exception(flag)
74c1ed15f1Sopenharmony_ci    policy_file_list.sort()
75c1ed15f1Sopenharmony_ci    return " ".join(str(x) for x in policy_file_list)
76c1ed15f1Sopenharmony_ci
77c1ed15f1Sopenharmony_ci
78c1ed15f1Sopenharmony_cidef check_contexts_file(contexts_file):
79c1ed15f1Sopenharmony_ci    """
80c1ed15f1Sopenharmony_ci    Check the format of contexts file.
81c1ed15f1Sopenharmony_ci    :param contexts_file: list of te file
82c1ed15f1Sopenharmony_ci    :return:
83c1ed15f1Sopenharmony_ci    """
84c1ed15f1Sopenharmony_ci    err = 0
85c1ed15f1Sopenharmony_ci    lines = []
86c1ed15f1Sopenharmony_ci    with open(contexts_file, 'rb') as fp:
87c1ed15f1Sopenharmony_ci        lines = fp.readlines()
88c1ed15f1Sopenharmony_ci    if len(lines) == 0:
89c1ed15f1Sopenharmony_ci        return 0
90c1ed15f1Sopenharmony_ci    last_line = lines[-1]
91c1ed15f1Sopenharmony_ci    if b'\n' not in last_line:
92c1ed15f1Sopenharmony_ci        print("".join((contexts_file, " : need an empty line at end \n")))
93c1ed15f1Sopenharmony_ci        err = 1
94c1ed15f1Sopenharmony_ci    for line in lines:
95c1ed15f1Sopenharmony_ci        if line.endswith(b'\r\n') or line.endswith(b'\r'):
96c1ed15f1Sopenharmony_ci            print("".join((contexts_file, " : must be unix format\n")))
97c1ed15f1Sopenharmony_ci            err = 1
98c1ed15f1Sopenharmony_ci            break
99c1ed15f1Sopenharmony_ci    return err
100c1ed15f1Sopenharmony_ci
101c1ed15f1Sopenharmony_ci
102c1ed15f1Sopenharmony_cidef combine_contexts_file(file_contexts_list, combined_file_contexts):
103c1ed15f1Sopenharmony_ci    cat_cmd = ["cat",
104c1ed15f1Sopenharmony_ci               file_contexts_list,
105c1ed15f1Sopenharmony_ci               ">", combined_file_contexts + "_tmp"]
106c1ed15f1Sopenharmony_ci    run_command(cat_cmd)
107c1ed15f1Sopenharmony_ci
108c1ed15f1Sopenharmony_ci    grep_cmd = ["grep -v ^#",
109c1ed15f1Sopenharmony_ci                combined_file_contexts + "_tmp",
110c1ed15f1Sopenharmony_ci                "| grep -v ^$",
111c1ed15f1Sopenharmony_ci                ">", combined_file_contexts]
112c1ed15f1Sopenharmony_ci    run_command(grep_cmd)
113c1ed15f1Sopenharmony_ci
114c1ed15f1Sopenharmony_ci
115c1ed15f1Sopenharmony_cidef check_redefinition(contexts_file):
116c1ed15f1Sopenharmony_ci    type_hash = defaultdict(list)
117c1ed15f1Sopenharmony_ci    err = 0
118c1ed15f1Sopenharmony_ci    with open(contexts_file, 'r') as contexts_read:
119c1ed15f1Sopenharmony_ci        pattern = re.compile(r'(\S+)\s+u:object_r:\S+:s0')
120c1ed15f1Sopenharmony_ci        line_index = 0
121c1ed15f1Sopenharmony_ci        for line in contexts_read:
122c1ed15f1Sopenharmony_ci            line_ = line.lstrip()
123c1ed15f1Sopenharmony_ci            line_index += 1
124c1ed15f1Sopenharmony_ci            if line_.startswith('#') or line_.strip() == '':
125c1ed15f1Sopenharmony_ci                continue
126c1ed15f1Sopenharmony_ci            match = pattern.match(line_)
127c1ed15f1Sopenharmony_ci            if match:
128c1ed15f1Sopenharmony_ci                type_hash[match.group(1)].append(line_index)
129c1ed15f1Sopenharmony_ci            else:
130c1ed15f1Sopenharmony_ci                print(contexts_file + ":" +
131c1ed15f1Sopenharmony_ci                      str(line_index) + " format check fail")
132c1ed15f1Sopenharmony_ci                err = 1
133c1ed15f1Sopenharmony_ci        contexts_read.close()
134c1ed15f1Sopenharmony_ci    if err:
135c1ed15f1Sopenharmony_ci        print("***********************************************************")
136c1ed15f1Sopenharmony_ci        print("please check whether the format meets the following rules:")
137c1ed15f1Sopenharmony_ci        print("[required format]: * u:object_r:*:s0")
138c1ed15f1Sopenharmony_ci        print("***********************************************************")
139c1ed15f1Sopenharmony_ci        raise Exception(err)
140c1ed15f1Sopenharmony_ci    err = 0
141c1ed15f1Sopenharmony_ci    for type_key in type_hash.keys():
142c1ed15f1Sopenharmony_ci        if len(type_hash[type_key]) > 1:
143c1ed15f1Sopenharmony_ci            err = 1
144c1ed15f1Sopenharmony_ci            str_seq = (contexts_file, ":")
145c1ed15f1Sopenharmony_ci            err_msg = "".join(str_seq)
146c1ed15f1Sopenharmony_ci            for linenum in type_hash[type_key]:
147c1ed15f1Sopenharmony_ci                str_seq = (err_msg, str(linenum), ":")
148c1ed15f1Sopenharmony_ci                err_msg = "".join(str_seq)
149c1ed15f1Sopenharmony_ci            str_seq = (err_msg, "'type ", str(type_key), " is redefinition'")
150c1ed15f1Sopenharmony_ci            err_msg = "".join(str_seq)
151c1ed15f1Sopenharmony_ci            print(err_msg)
152c1ed15f1Sopenharmony_ci    if err:
153c1ed15f1Sopenharmony_ci        raise Exception(err)
154c1ed15f1Sopenharmony_ci
155c1ed15f1Sopenharmony_ci
156c1ed15f1Sopenharmony_cidef check_common_contexts(args, contexts_file):
157c1ed15f1Sopenharmony_ci    """
158c1ed15f1Sopenharmony_ci    check whether context used in contexts_file is defined in policy.31.
159c1ed15f1Sopenharmony_ci    :param args:
160c1ed15f1Sopenharmony_ci    :param contexts_file: path of contexts file
161c1ed15f1Sopenharmony_ci    :return:
162c1ed15f1Sopenharmony_ci    """
163c1ed15f1Sopenharmony_ci    check_redefinition(contexts_file)
164c1ed15f1Sopenharmony_ci
165c1ed15f1Sopenharmony_ci    check_cmd = [os.path.join(args.tool_path, "sefcontext_compile"),
166c1ed15f1Sopenharmony_ci                 "-o", contexts_file + ".bin",
167c1ed15f1Sopenharmony_ci                 "-p", args.policy_file,
168c1ed15f1Sopenharmony_ci                 contexts_file]
169c1ed15f1Sopenharmony_ci    run_command(check_cmd)
170c1ed15f1Sopenharmony_ci    if os.path.exists(contexts_file + ".bin"):
171c1ed15f1Sopenharmony_ci        os.unlink(contexts_file + ".bin")
172c1ed15f1Sopenharmony_ci
173c1ed15f1Sopenharmony_ci
174c1ed15f1Sopenharmony_cidef echo_error():
175c1ed15f1Sopenharmony_ci    print("***********************************************************")
176c1ed15f1Sopenharmony_ci    print("please check whether the format meets the following rules:")
177c1ed15f1Sopenharmony_ci    print("[required format]: apl=* name=* domain=* type=*")
178c1ed15f1Sopenharmony_ci    print("apl=*, apl should be one of system_core|system_basic|normal")
179c1ed15f1Sopenharmony_ci    print("name=*, name is 'optional'")
180c1ed15f1Sopenharmony_ci    print("domain=*, hapdomain selinux type")
181c1ed15f1Sopenharmony_ci    print("type=*, hapdatafile selinux type")
182c1ed15f1Sopenharmony_ci    print("***********************************************************")
183c1ed15f1Sopenharmony_ci
184c1ed15f1Sopenharmony_ci
185c1ed15f1Sopenharmony_cidef sehap_process_line(line, line_index, contexts_write, domain, contexts_file):
186c1ed15f1Sopenharmony_ci    line_ = line.lstrip()
187c1ed15f1Sopenharmony_ci    if line_.startswith('#') or line_.strip() == '':
188c1ed15f1Sopenharmony_ci        contexts_write.write(line)
189c1ed15f1Sopenharmony_ci        return
190c1ed15f1Sopenharmony_ci
191c1ed15f1Sopenharmony_ci    pattern = re.compile(
192c1ed15f1Sopenharmony_ci        r'apl=(system_core|system_basic|normal)\s+'
193c1ed15f1Sopenharmony_ci        r'((name|debuggable)=\S+\s+)?'
194c1ed15f1Sopenharmony_ci        r'(extra=\S+\s+)?'
195c1ed15f1Sopenharmony_ci        r'domain=(\S+)\s+'
196c1ed15f1Sopenharmony_ci        r'type=(\S+)\s*\n'
197c1ed15f1Sopenharmony_ci    )
198c1ed15f1Sopenharmony_ci    match = pattern.match(line_)
199c1ed15f1Sopenharmony_ci    if match:
200c1ed15f1Sopenharmony_ci        if domain:
201c1ed15f1Sopenharmony_ci            line = match.group(1) + " u:r:" + match.group(5) + ":s0\n"
202c1ed15f1Sopenharmony_ci        else:
203c1ed15f1Sopenharmony_ci            line = match.group(1) + " u:object_r:" + match.group(6) + ":s0\n"
204c1ed15f1Sopenharmony_ci        contexts_write.write(line)
205c1ed15f1Sopenharmony_ci    else:
206c1ed15f1Sopenharmony_ci        print(contexts_file + ":" + str(line_index) + " format check fail")
207c1ed15f1Sopenharmony_ci        raise Exception(1)
208c1ed15f1Sopenharmony_ci
209c1ed15f1Sopenharmony_ci
210c1ed15f1Sopenharmony_cidef check_sehap_contexts(args, contexts_file, domain):
211c1ed15f1Sopenharmony_ci    """
212c1ed15f1Sopenharmony_ci    check domain or type defined in sehap_contexts.
213c1ed15f1Sopenharmony_ci    :param args:
214c1ed15f1Sopenharmony_ci    :param contexts_file: path of contexts file
215c1ed15f1Sopenharmony_ci    :param domain: true for domain, false for type
216c1ed15f1Sopenharmony_ci    :return:
217c1ed15f1Sopenharmony_ci    """
218c1ed15f1Sopenharmony_ci    shutil.copyfile(contexts_file, contexts_file + "_bk")
219c1ed15f1Sopenharmony_ci    try:
220c1ed15f1Sopenharmony_ci        with open(contexts_file + "_bk", 'r') as contexts_read, open(contexts_file, 'w') as contexts_write:
221c1ed15f1Sopenharmony_ci            line_index = 0
222c1ed15f1Sopenharmony_ci            for line in contexts_read:
223c1ed15f1Sopenharmony_ci                line_index += 1
224c1ed15f1Sopenharmony_ci                sehap_process_line(line, line_index, contexts_write, domain, contexts_file)
225c1ed15f1Sopenharmony_ci    except Exception as e:
226c1ed15f1Sopenharmony_ci        shutil.move(contexts_file + "_bk", contexts_file)
227c1ed15f1Sopenharmony_ci        echo_error()
228c1ed15f1Sopenharmony_ci        raise e
229c1ed15f1Sopenharmony_ci
230c1ed15f1Sopenharmony_ci    check_cmd = [os.path.join(args.tool_path, "sefcontext_compile"),
231c1ed15f1Sopenharmony_ci                 "-o", contexts_file + ".bin",
232c1ed15f1Sopenharmony_ci                 "-p", args.policy_file,
233c1ed15f1Sopenharmony_ci                 contexts_file]
234c1ed15f1Sopenharmony_ci    ret = subprocess.run(" ".join(check_cmd), shell=True).returncode
235c1ed15f1Sopenharmony_ci
236c1ed15f1Sopenharmony_ci    if ret != 0:
237c1ed15f1Sopenharmony_ci        shutil.move(contexts_file + "_bk", contexts_file)
238c1ed15f1Sopenharmony_ci        raise Exception(ret)
239c1ed15f1Sopenharmony_ci
240c1ed15f1Sopenharmony_ci    shutil.move(contexts_file + "_bk", contexts_file)
241c1ed15f1Sopenharmony_ci
242c1ed15f1Sopenharmony_ci    if os.path.exists(contexts_file + ".bin"):
243c1ed15f1Sopenharmony_ci        os.unlink(contexts_file + ".bin")
244c1ed15f1Sopenharmony_ci
245c1ed15f1Sopenharmony_ci
246c1ed15f1Sopenharmony_cidef build_file_contexts(args, output_path, policy_path, all_policy_path):
247c1ed15f1Sopenharmony_ci    if args.components == "default":
248c1ed15f1Sopenharmony_ci        all_combined_file_contexts = os.path.join(output_path, "file_contexts")
249c1ed15f1Sopenharmony_ci    else:
250c1ed15f1Sopenharmony_ci        all_combined_file_contexts = os.path.join(output_path, "all_file_contexts")
251c1ed15f1Sopenharmony_ci        file_contexts_list = traverse_folder_in_type(policy_path, "file_contexts")
252c1ed15f1Sopenharmony_ci        combined_file_contexts = os.path.join(output_path, "file_contexts")
253c1ed15f1Sopenharmony_ci        combine_contexts_file(file_contexts_list, combined_file_contexts)
254c1ed15f1Sopenharmony_ci
255c1ed15f1Sopenharmony_ci    all_file_contexts_list = traverse_folder_in_type(
256c1ed15f1Sopenharmony_ci        all_policy_path, "file_contexts")
257c1ed15f1Sopenharmony_ci    combine_contexts_file(all_file_contexts_list, all_combined_file_contexts)
258c1ed15f1Sopenharmony_ci
259c1ed15f1Sopenharmony_ci    check_redefinition(all_combined_file_contexts)
260c1ed15f1Sopenharmony_ci
261c1ed15f1Sopenharmony_ci    build_bin_cmd = [os.path.join(args.tool_path, "sefcontext_compile"),
262c1ed15f1Sopenharmony_ci                     "-o", os.path.join(args.dst_dir, "file_contexts.bin"),
263c1ed15f1Sopenharmony_ci                     "-p", args.policy_file,
264c1ed15f1Sopenharmony_ci                     all_combined_file_contexts]
265c1ed15f1Sopenharmony_ci    run_command(build_bin_cmd)
266c1ed15f1Sopenharmony_ci
267c1ed15f1Sopenharmony_ci
268c1ed15f1Sopenharmony_cidef build_common_contexts(args, output_path, contexts_file_name, policy_path, all_policy_path):
269c1ed15f1Sopenharmony_ci    if args.components == "default":
270c1ed15f1Sopenharmony_ci        all_combined_contexts = output_path + contexts_file_name
271c1ed15f1Sopenharmony_ci    else:
272c1ed15f1Sopenharmony_ci        all_combined_contexts = output_path + "all_" + contexts_file_name
273c1ed15f1Sopenharmony_ci        contexts_list = traverse_folder_in_type(policy_path, contexts_file_name)
274c1ed15f1Sopenharmony_ci        combined_contexts = output_path + contexts_file_name
275c1ed15f1Sopenharmony_ci        combine_contexts_file(contexts_list, combined_contexts)
276c1ed15f1Sopenharmony_ci
277c1ed15f1Sopenharmony_ci    all_contexts_list = traverse_folder_in_type(all_policy_path, contexts_file_name)
278c1ed15f1Sopenharmony_ci    combine_contexts_file(all_contexts_list, all_combined_contexts)
279c1ed15f1Sopenharmony_ci    check_common_contexts(args, all_combined_contexts)
280c1ed15f1Sopenharmony_ci
281c1ed15f1Sopenharmony_ci
282c1ed15f1Sopenharmony_cidef build_sehap_contexts(args, output_path, policy_path):
283c1ed15f1Sopenharmony_ci    contexts_list = traverse_folder_in_type(
284c1ed15f1Sopenharmony_ci        policy_path, "sehap_contexts")
285c1ed15f1Sopenharmony_ci
286c1ed15f1Sopenharmony_ci    combined_contexts = os.path.join(output_path, "sehap_contexts")
287c1ed15f1Sopenharmony_ci    combine_contexts_file(contexts_list, combined_contexts)
288c1ed15f1Sopenharmony_ci
289c1ed15f1Sopenharmony_ci    check_sehap_contexts(args, combined_contexts, 1)
290c1ed15f1Sopenharmony_ci    check_sehap_contexts(args, combined_contexts, 0)
291c1ed15f1Sopenharmony_ci
292c1ed15f1Sopenharmony_ci
293c1ed15f1Sopenharmony_cidef prepare_build_path(dir_list, root_dir, build_dir_list):
294c1ed15f1Sopenharmony_ci    build_contexts_list = \
295c1ed15f1Sopenharmony_ci        ["base/security/selinux_adapter/sepolicy/base", "base/security/selinux_adapter/sepolicy/ohos_policy"]
296c1ed15f1Sopenharmony_ci    build_contexts_list += dir_list.split(":")
297c1ed15f1Sopenharmony_ci
298c1ed15f1Sopenharmony_ci    for i in build_contexts_list:
299c1ed15f1Sopenharmony_ci        if i == "" or i == "default":
300c1ed15f1Sopenharmony_ci            continue
301c1ed15f1Sopenharmony_ci        path = os.path.join(root_dir, i)
302c1ed15f1Sopenharmony_ci        if (os.path.exists(path)):
303c1ed15f1Sopenharmony_ci            build_dir_list.append(path)
304c1ed15f1Sopenharmony_ci        else:
305c1ed15f1Sopenharmony_ci            print("following path not exists!! {}".format(path))
306c1ed15f1Sopenharmony_ci            exit(-1)
307c1ed15f1Sopenharmony_ci
308c1ed15f1Sopenharmony_ci
309c1ed15f1Sopenharmony_cidef traverse_folder_in_dir_name(search_dir, folder_suffix):
310c1ed15f1Sopenharmony_ci    folder_list = []
311c1ed15f1Sopenharmony_ci    for root, dirs, _ in sorted(os.walk(search_dir)):
312c1ed15f1Sopenharmony_ci        for dir_i in dirs:
313c1ed15f1Sopenharmony_ci            if dir_i == folder_suffix:
314c1ed15f1Sopenharmony_ci                folder_list.append(os.path.join(root, dir_i))
315c1ed15f1Sopenharmony_ci    return folder_list
316c1ed15f1Sopenharmony_ci
317c1ed15f1Sopenharmony_ci
318c1ed15f1Sopenharmony_cidef main(args):
319c1ed15f1Sopenharmony_ci    if sys.platform == "linux" and platform.machine().lower() == "aarch64":
320c1ed15f1Sopenharmony_ci        libpcre2_path = os.path.realpath("./clang_arm64/thirdparty/pcre2/")
321c1ed15f1Sopenharmony_ci    else:
322c1ed15f1Sopenharmony_ci        libpcre2_path = os.path.realpath("./clang_x64/thirdparty/pcre2/")
323c1ed15f1Sopenharmony_ci    os.environ['LD_LIBRARY_PATH'] = libpcre2_path
324c1ed15f1Sopenharmony_ci    output_path = args.dst_dir
325c1ed15f1Sopenharmony_ci    policy_path = []
326c1ed15f1Sopenharmony_ci    prepare_build_path(args.policy_dir_list, args.source_root_dir, policy_path)
327c1ed15f1Sopenharmony_ci
328c1ed15f1Sopenharmony_ci    public_policy = []
329c1ed15f1Sopenharmony_ci    system_policy = []
330c1ed15f1Sopenharmony_ci    vendor_policy = []
331c1ed15f1Sopenharmony_ci
332c1ed15f1Sopenharmony_ci    for item in policy_path:
333c1ed15f1Sopenharmony_ci        public_policy += traverse_folder_in_dir_name(item, "public")
334c1ed15f1Sopenharmony_ci        system_policy += traverse_folder_in_dir_name(item, "system")
335c1ed15f1Sopenharmony_ci        vendor_policy += traverse_folder_in_dir_name(item, "vendor")
336c1ed15f1Sopenharmony_ci
337c1ed15f1Sopenharmony_ci    system_folder_list = public_policy + system_policy
338c1ed15f1Sopenharmony_ci    vendor_folder_list = public_policy + vendor_policy
339c1ed15f1Sopenharmony_ci    all_folder_list = public_policy + system_policy + vendor_policy
340c1ed15f1Sopenharmony_ci
341c1ed15f1Sopenharmony_ci    folder_list = []
342c1ed15f1Sopenharmony_ci
343c1ed15f1Sopenharmony_ci    if args.components == "system":
344c1ed15f1Sopenharmony_ci        folder_list = system_folder_list
345c1ed15f1Sopenharmony_ci    elif args.components == "vendor":
346c1ed15f1Sopenharmony_ci        folder_list = vendor_folder_list
347c1ed15f1Sopenharmony_ci    else:
348c1ed15f1Sopenharmony_ci        folder_list = all_folder_list
349c1ed15f1Sopenharmony_ci
350c1ed15f1Sopenharmony_ci    build_file_contexts(args, output_path, folder_list, all_folder_list)
351c1ed15f1Sopenharmony_ci    build_common_contexts(args, output_path, "service_contexts", folder_list, all_folder_list)
352c1ed15f1Sopenharmony_ci    build_common_contexts(args, output_path, "hdf_service_contexts", folder_list, all_folder_list)
353c1ed15f1Sopenharmony_ci    build_common_contexts(args, output_path, "parameter_contexts", folder_list, all_folder_list)
354c1ed15f1Sopenharmony_ci    build_sehap_contexts(args, output_path, all_folder_list)
355c1ed15f1Sopenharmony_ci
356c1ed15f1Sopenharmony_ci
357c1ed15f1Sopenharmony_ciif __name__ == "__main__":
358c1ed15f1Sopenharmony_ci    input_args = parse_args()
359c1ed15f1Sopenharmony_ci    main(input_args)
360