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 argparse
17import json
18import os
19import shutil
20import stat
21import time
22import utils
23
24
25def _get_args():
26    parser = argparse.ArgumentParser(add_help=True)
27    parser.add_argument(
28        "-rp",
29        "--root_path",
30        default=r".",
31        type=str,
32        help="Path of root",
33    )
34    parser.add_argument(
35        "-v",
36        "--variants",
37        default=r".",
38        type=str,
39        help="variants of build target",
40    )
41    args = parser.parse_args()
42    return args
43
44
45def _gen_syscap_para_info(variants_path, syscap_out_path):
46    syscap_json_file = os.path.join(variants_path, 'syscap.json')
47    syscap_json = utils.get_json(syscap_json_file)
48    syscap_value = syscap_json.get('syscap_value')
49    os.makedirs(syscap_out_path)
50    out_path = os.path.join(syscap_out_path, 'syscap.para')
51
52    flags = os.O_WRONLY | os.O_CREAT
53    modes = stat.S_IWUSR | stat.S_IRUSR
54    try:
55        with os.fdopen(os.open(out_path, flags, modes), 'w') as f:
56            for k, v in syscap_value.items():
57                f.write(f'{k}={str(v).lower()}' + '\n')
58    except Exception as e:
59        print(f"{out_path}: \n {e}")
60    return syscap_json
61
62
63def _re_gen_syscap_json(syscap_json, variants_out_path):
64    syscap_json_out_file = os.path.join(variants_out_path, "syscap.json")
65
66    del syscap_json['syscap_value']
67    flags = os.O_WRONLY | os.O_CREAT
68    modes = stat.S_IWUSR | stat.S_IRUSR
69    with os.fdopen(os.open(syscap_json_out_file, flags, modes), 'w') as f:
70        json.dump(syscap_json, f, indent=4)
71
72
73def main():
74    args = _get_args()
75    root_path = args.root_path
76    variants = args.variants
77    variants_path = os.path.join(root_path, 'build', 'indep_configs', "variants", variants)
78    variants_out_path = os.path.join(root_path, 'out', "preloader", variants)
79    etc_out_path = os.path.join(variants_out_path, "system", "etc")
80    syscap_out_path = os.path.join(etc_out_path, "param")
81
82    re_syscap_json = _gen_syscap_para_info(variants_path, syscap_out_path)
83    _re_gen_syscap_json(re_syscap_json, etc_out_path)
84
85    system_capability_file = os.path.join(variants_path, "SystemCapability.json")
86    features_file = os.path.join(variants_path, "features.json")
87    build_file = os.path.join(variants_path, "build_config.json")
88    paths_config_file = os.path.join(variants_path, "parts_config.json")
89
90    shutil.copy(system_capability_file, etc_out_path)
91    shutil.copy(features_file, variants_out_path)
92    shutil.copy(build_file, variants_out_path)
93    shutil.copy(paths_config_file, variants_out_path)
94
95
96if __name__ == '__main__':
97    main()
98