• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2021 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 json
17import os
18
19from hb_internal.common.utils import read_json_file
20
21
22def _read_lite_component_configs(file):
23    subsytem_name = os.path.basename(file)[:-5]
24    configs = {}
25    configs['subsystem_name'] = subsytem_name
26    with open(file, 'rb') as fin:
27        data = json.load(fin)
28        components = data.get('components')
29        parts = {}
30        for com in components:
31            part = {}
32            targets = com.get('targets')
33            test_targets = []
34            non_test_targets = []
35            for item in targets:
36                target_names = item.strip('"').split(':')
37                if len(target_names) > 1 and 'test' in target_names[1]:
38                    test_targets.append(item)
39                else:
40                    non_test_targets.append(item)
41            part['module_list'] = non_test_targets
42            if test_targets != []:
43                part['test_list'] = test_targets
44            part_name = com.get('component')
45            parts[part_name] = part
46        configs['parts'] = parts
47    return configs
48
49
50def _save_as_ohos_build(config, ohos_build):
51    new_config = json.dumps(config, indent=2, sort_keys=True)
52    with open(ohos_build, 'w') as fout:
53        fout.write(new_config)
54
55
56def parse_lite_subsystem_config(lite_components_dir, output_dir,
57                                source_root_dir, subsystem_config_file):
58    subsystem_infos = read_json_file(subsystem_config_file)
59    for root, _, files in os.walk(lite_components_dir):
60        for file in files:
61            if file[-5:] == '.json':
62                configs = _read_lite_component_configs(os.path.join(
63                    root, file))
64                subsystem_name = configs.get('subsystem_name')
65                ohos_build = os.path.join(
66                    output_dir, '{}/ohos.build'.format(subsystem_name))
67                os.makedirs(os.path.dirname(ohos_build), exist_ok=True)
68                _save_as_ohos_build(configs, ohos_build)
69                subsystem_infos[subsystem_name] = {
70                    'name':
71                    subsystem_name,
72                    "path":
73                    os.path.relpath(os.path.dirname(ohos_build),
74                                    source_root_dir),
75                }
76    return subsystem_infos
77