1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3"""
4Copyright (c) 2021 Huawei Device Co., Ltd.
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9
10  http://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing, software
13distributed under the License is distributed on an "AS IS" BASIS,
14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15See the License for the specific language governing permissions and
16limitations under the License.
17"""
18
19import json
20import sys
21
22
23def filter_modules(subsystem_data_file, test_packages):
24    subsystems = set()
25    with open(subsystem_data_file) as fd:
26        subsystem_data = json.load(fd)
27        subsystems = set(subsystem_data)
28    subsystems.add("demo")
29    subsystems.add("storage")
30    subsystems.add("arkXtest")
31    subsystems.add("validator")
32    subsystems.add("pcs")
33    subsystems.add("appbuild")
34    selected_packages = []
35    for dep in test_packages:
36        if 'selected_subsystem=' in dep:
37            selected_subsystem = dep.split('=')[1]
38            init_subsystems = subsystems.copy()
39            subsystems.clear()
40            for item in selected_subsystem.split(','):
41                if item in init_subsystems:
42                    subsystems.add(item)
43            break
44    for dep in test_packages:
45        rslash_index = dep.find('/hits')
46        if rslash_index < 0:
47            rslash_index = dep.find('/acts')
48        if rslash_index < 0:
49            rslash_index = dep.find('/hats')
50        if rslash_index < 0:
51            rslash_index = dep.find('/dcts')
52        colon_index = dep.find(':')
53        if rslash_index < 0 or colon_index < 0:
54            continue
55        rslash_index += 5
56        subsystem_name = dep[rslash_index + 1:colon_index]
57        if subsystem_name in subsystems:
58            selected_packages.append(dep)
59        else:
60            pass
61    return selected_packages
62
63
64if __name__ == "__main__":
65    main_subsystem_data_file = sys.argv[1]
66    main_test_packages = sys.argv[2:]
67    main_selected_packages = filter_modules(main_subsystem_data_file,
68                                            main_test_packages)
69    sys.stdout.write('\n'.join(main_selected_packages))
70