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
26
27def parse_args(args):
28    args = build_utils.expand_file_args(args)
29
30    parser = optparse.OptionParser()
31    build_utils.add_depfile_option(parser)
32    parser.add_option('--resources-dir', help='resources directory')
33    parser.add_option('--app-profile', default=False, help='path to app profile')
34    parser.add_option('--hap-profile', help='path to hap profile')
35    parser.add_option('--generated-profile', help='path to generated profile')
36    parser.add_option('--release-type', help='release type')
37    parser.add_option('--api-version', help='api version')
38    options, _ = parser.parse_args(args)
39    options.resources_dir = build_utils.parse_gn_list(options.resources_dir)
40    return options
41
42
43def merge_profile(options):
44    all_data = {}
45    with open(options.hap_profile) as f0:
46        if len(options.app_profile) == 0:
47            all_data = json.load(f0)
48        else:
49            module_data = json.load(f0)["module"]
50            with open(options.app_profile) as f1:
51                app_data = json.load(f1)["app"]
52                all_data["app"] = app_data
53                all_data["module"] = module_data
54                f1.close()
55        f0.close()
56    if str(all_data.get('app').get('targetAPIVersion')) == options.api_version:
57        all_data["app"]["apiReleaseType"] = options.release_type
58    else:
59        all_data["app"]["apiReleaseType"] = 'Release'
60    f3 = open(options.generated_profile, "w")
61    json.dump(all_data, f3, indent=4, ensure_ascii=False)
62    f3.close()
63
64
65def main(args):
66    options = parse_args(args)
67    inputs = [options.hap_profile]
68    if not options.app_profile:
69        inputs += options.app_profile
70    depfiles = []
71    for directory in options.resources_dir:
72        depfiles += (build_utils.get_all_files(directory))
73
74    input_strings = []
75    outputs = [options.generated_profile]
76    build_utils.call_and_write_depfile_if_stale(
77        lambda: merge_profile(options),
78        options,
79        depfile_deps=depfiles,
80        input_paths=inputs + depfiles,
81        input_strings=input_strings,
82        output_paths=(outputs),
83        force=False,
84        add_pydeps=False)
85
86if __name__ == '__main__':
87    sys.exit(main(sys.argv[1:]))
88