• 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
16from email.policy import default
17import optparse
18import os
19import sys
20import shutil
21import json
22
23from zipfile import ZipFile  # noqa: E402
24from util import build_utils  # noqa: E402
25
26def parse_args(args):
27    args = build_utils.expand_file_args(args)
28
29    parser = optparse.OptionParser()
30    build_utils.add_depfile_option(parser)
31    parser.add_option('--resources-dir', help='resources directory')
32    parser.add_option('--app-profile', default=False, help='path to app profile')
33    parser.add_option('--hap-profile', help='path to hap profile')
34    parser.add_option('--generated-profile', help='path to generated profile')
35    options, _ = parser.parse_args(args)
36    options.resources_dir = build_utils.parse_gn_list(options.resources_dir)
37    return options
38
39def merge_profile(options):
40    if len(options.app_profile) == 0:
41        shutil.copyfile(options.hap_profile, options.generated_profile)
42    else:
43        all_data = {}
44        with open(options.hap_profile) as f0:
45            module_data = json.load(f0)["module"]
46            with open(options.app_profile) as f1:
47                app_data = json.load(f1)["app"]
48                all_data["app"] = app_data
49                all_data["module"] = module_data
50                f1.close()
51            f0.close()
52        f3 = open(options.generated_profile, "w")
53        json.dump(all_data, f3, indent=4, ensure_ascii=False)
54        f3.close()
55
56
57def main(args):
58    options = parse_args(args)
59    inputs = [options.hap_profile]
60    if not options.app_profile:
61        inputs += options.app_profile
62    depfiles = []
63    for directory in options.resources_dir:
64        depfiles += (build_utils.get_all_files(directory))
65
66    input_strings = []
67    outputs = [options.generated_profile]
68    build_utils.call_and_write_depfile_if_stale(
69        lambda: merge_profile(options),
70        options,
71        depfile_deps=depfiles,
72        input_paths=inputs + depfiles,
73        input_strings=input_strings,
74        output_paths=(outputs),
75        force=False,
76        add_pydeps=False)
77
78if __name__ == '__main__':
79    sys.exit(main(sys.argv[1:]))
80