• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2023 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 os
17import sys
18import argparse
19from containers.status import throw_exception
20from exceptions.ohos_exception import OHOSException
21from scripts.util.file_utils import read_json_file, write_json_file  # noqa: E402 E501
22from util.log_util import LogUtil
23
24_default_subsystem = {"build": "build"}
25
26
27@throw_exception
28def _read_config(subsystem_config_file, example_subsystem_file):
29    if not os.path.exists(subsystem_config_file):
30        raise OHOSException(
31            "config file '{}' doesn't exist.".format(subsystem_config_file), "2013")
32    subsystem_config = read_json_file(subsystem_config_file)
33    if subsystem_config is None:
34        raise OHOSException("read file '{}' failed.".format(
35            subsystem_config_file), "2013")
36
37    # example subsystem
38    if example_subsystem_file:
39        example_subsystem_config = read_json_file(example_subsystem_file)
40        if example_subsystem_config is not None:
41            subsystem_config.update(example_subsystem_config)
42
43    subsystem_info = {}
44    for key, val in subsystem_config.items():
45        if 'path' not in val:
46            raise OHOSException(
47                "subsystem '{}' not config path.".format(key), "2013")
48        subsystem_info[key] = val.get('path')
49    return subsystem_info
50
51
52def _scan_build_file(subsystem_path):
53    _files = []
54    _bundle_files = []
55    for root, dirs, files in os.walk(subsystem_path):
56        for name in files:
57            if name == 'ohos.build':
58                _files.append(os.path.join(root, name))
59            elif name == 'bundle.json':
60                _bundle_files.append(os.path.join(root, name))
61    if _bundle_files:
62        _files.extend(_bundle_files)
63    return _files
64
65
66def _check_path_prefix(paths):
67    allow_path_prefix = ['vendor', 'device']
68    result = list(
69        filter(lambda x: x is False,
70               map(lambda p: p.split('/')[0] in allow_path_prefix, paths)))
71    return len(result) <= 1
72
73
74@throw_exception
75def scan(subsystem_config_file, example_subsystem_file, source_root_dir):
76    subsystem_infos = _read_config(subsystem_config_file,
77                                   example_subsystem_file)
78    # add common subsystem info
79    subsystem_infos.update(_default_subsystem)
80
81    no_src_subsystem = {}
82    _build_configs = {}
83    for key, val in subsystem_infos.items():
84        _all_build_config_files = []
85        if not isinstance(val, list):
86            val = [val]
87        else:
88            if not _check_path_prefix(val):
89                raise OHOSException(
90                    "subsystem '{}' path configuration is incorrect.".format(
91                        key), "2013")
92        _info = {'path': val}
93        for _path in val:
94            _subsystem_path = os.path.join(source_root_dir, _path)
95            _build_config_files = _scan_build_file(_subsystem_path)
96            _all_build_config_files.extend(_build_config_files)
97        if _all_build_config_files:
98            _info['build_files'] = _all_build_config_files
99            _build_configs[key] = _info
100        else:
101            no_src_subsystem[key] = val
102
103    scan_result = {
104        'source_path': source_root_dir,
105        'subsystem': _build_configs,
106        'no_src_subsystem': no_src_subsystem
107    }
108    LogUtil.hb_info('subsytem config scan completed')
109    return scan_result
110
111
112def main():
113    parser = argparse.ArgumentParser()
114    parser.add_argument('--subsystem-config-file', required=True)
115    parser.add_argument('--subsystem-config-overlay-file', required=True)
116    parser.add_argument('--example-subsystem-file', required=False)
117    parser.add_argument('--source-root-dir', required=True)
118    parser.add_argument('--output-dir', required=True)
119    args = parser.parse_args()
120
121    build_configs = scan(args.subsystem_config_file,
122                         args.example_subsystem_file, args.source_root_dir)
123
124    build_configs_file = os.path.join(args.output_dir,
125                                      "subsystem_build_config.json")
126    write_json_file(build_configs_file, build_configs)
127    return 0
128
129
130if __name__ == '__main__':
131    sys.exit(main())
132