• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 time
20import stat
21import utils
22
23
24def _get_args():
25    parser = argparse.ArgumentParser(add_help=True)
26    parser.add_argument(
27        "-p",
28        "--input_path",
29        default=r"./",
30        type=str,
31        help="Path of the folder where the collection of txt files to be processed is located.",
32    )
33    parser.add_argument(
34        "-o",
35        "--output_path",
36        default=r"./",
37        type=str,
38        help="path of output file. default: ./",
39    )
40    args = parser.parse_args()
41    return args
42
43
44def _scan_dir_to_get_info(bundle_path):
45    dirs_info = dict()
46    file_list = list()
47    for entry in os.scandir(bundle_path):
48        if entry.name == 'bundle.json':
49            pass
50        elif entry.is_dir():
51            dirs_info.update({entry.name: [f"{entry.name}/*"]})
52        elif entry.is_file():
53
54            file_list.append(entry.name)
55        else:
56            print(f'{entry.name} is not file or dir ')
57    dirs_info.update({"./": file_list})
58    return dirs_info
59
60
61def _out_bundle_json(bundle_json, file_name):
62    flags = os.O_WRONLY | os.O_CREAT
63    modes = stat.S_IWUSR | stat.S_IRUSR
64    with os.fdopen(os.open(file_name, flags, modes), 'w') as f:
65        json.dump(bundle_json, f, indent=2)
66
67
68def main():
69    args = _get_args()
70    hpmcache_path = args.input_path
71    dependences_file = os.path.join(hpmcache_path, 'dependences.json')
72    dependences_json = utils.get_json(dependences_file)
73    for part_name, part_info in dependences_json.items():
74        part_path = part_info['installPath']
75        bundle_path = os.path.join(hpmcache_path, part_path[1:], 'bundle.json')
76        bundle_json = utils.get_json(bundle_path)
77        dirs_info = _scan_dir_to_get_info(os.path.join(hpmcache_path, part_path[1:]))
78        bundle_json.update({"dirs": dirs_info})
79        _out_bundle_json(bundle_json, bundle_path)
80
81
82if __name__ == '__main__':
83    main()
84