xref: /build/scripts/merge_profile.py (revision 5f9996aa)
15f9996aaSopenharmony_ci#!/usr/bin/env python
25f9996aaSopenharmony_ci# -*- coding: utf-8 -*-
35f9996aaSopenharmony_ci# Copyright (c) 2021 Huawei Device Co., Ltd.
45f9996aaSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
55f9996aaSopenharmony_ci# you may not use this file except in compliance with the License.
65f9996aaSopenharmony_ci# You may obtain a copy of the License at
75f9996aaSopenharmony_ci#
85f9996aaSopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
95f9996aaSopenharmony_ci#
105f9996aaSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
115f9996aaSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
125f9996aaSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135f9996aaSopenharmony_ci# See the License for the specific language governing permissions and
145f9996aaSopenharmony_ci# limitations under the License.
155f9996aaSopenharmony_ci
165f9996aaSopenharmony_cifrom email.policy import default
175f9996aaSopenharmony_ciimport optparse
185f9996aaSopenharmony_ciimport os
195f9996aaSopenharmony_ciimport sys
205f9996aaSopenharmony_ciimport shutil
215f9996aaSopenharmony_ciimport json
225f9996aaSopenharmony_ci
235f9996aaSopenharmony_cifrom zipfile import ZipFile  # noqa: E402
245f9996aaSopenharmony_cifrom util import build_utils  # noqa: E402
255f9996aaSopenharmony_ci
265f9996aaSopenharmony_ci
275f9996aaSopenharmony_cidef parse_args(args):
285f9996aaSopenharmony_ci    args = build_utils.expand_file_args(args)
295f9996aaSopenharmony_ci
305f9996aaSopenharmony_ci    parser = optparse.OptionParser()
315f9996aaSopenharmony_ci    build_utils.add_depfile_option(parser)
325f9996aaSopenharmony_ci    parser.add_option('--resources-dir', help='resources directory')
335f9996aaSopenharmony_ci    parser.add_option('--app-profile', default=False, help='path to app profile')
345f9996aaSopenharmony_ci    parser.add_option('--hap-profile', help='path to hap profile')
355f9996aaSopenharmony_ci    parser.add_option('--generated-profile', help='path to generated profile')
365f9996aaSopenharmony_ci    parser.add_option('--release-type', help='release type')
375f9996aaSopenharmony_ci    parser.add_option('--api-version', help='api version')
385f9996aaSopenharmony_ci    options, _ = parser.parse_args(args)
395f9996aaSopenharmony_ci    options.resources_dir = build_utils.parse_gn_list(options.resources_dir)
405f9996aaSopenharmony_ci    return options
415f9996aaSopenharmony_ci
425f9996aaSopenharmony_ci
435f9996aaSopenharmony_cidef merge_profile(options):
445f9996aaSopenharmony_ci    all_data = {}
455f9996aaSopenharmony_ci    with open(options.hap_profile) as f0:
465f9996aaSopenharmony_ci        if len(options.app_profile) == 0:
475f9996aaSopenharmony_ci            all_data = json.load(f0)
485f9996aaSopenharmony_ci        else:
495f9996aaSopenharmony_ci            module_data = json.load(f0)["module"]
505f9996aaSopenharmony_ci            with open(options.app_profile) as f1:
515f9996aaSopenharmony_ci                app_data = json.load(f1)["app"]
525f9996aaSopenharmony_ci                all_data["app"] = app_data
535f9996aaSopenharmony_ci                all_data["module"] = module_data
545f9996aaSopenharmony_ci                f1.close()
555f9996aaSopenharmony_ci        f0.close()
565f9996aaSopenharmony_ci    if str(all_data.get('app').get('targetAPIVersion')) == options.api_version:
575f9996aaSopenharmony_ci        all_data["app"]["apiReleaseType"] = options.release_type
585f9996aaSopenharmony_ci    else:
595f9996aaSopenharmony_ci        all_data["app"]["apiReleaseType"] = 'Release'
605f9996aaSopenharmony_ci    f3 = open(options.generated_profile, "w")
615f9996aaSopenharmony_ci    json.dump(all_data, f3, indent=4, ensure_ascii=False)
625f9996aaSopenharmony_ci    f3.close()
635f9996aaSopenharmony_ci
645f9996aaSopenharmony_ci
655f9996aaSopenharmony_cidef main(args):
665f9996aaSopenharmony_ci    options = parse_args(args)
675f9996aaSopenharmony_ci    inputs = [options.hap_profile]
685f9996aaSopenharmony_ci    if not options.app_profile:
695f9996aaSopenharmony_ci        inputs += options.app_profile
705f9996aaSopenharmony_ci    depfiles = []
715f9996aaSopenharmony_ci    for directory in options.resources_dir:
725f9996aaSopenharmony_ci        depfiles += (build_utils.get_all_files(directory))
735f9996aaSopenharmony_ci
745f9996aaSopenharmony_ci    input_strings = []
755f9996aaSopenharmony_ci    outputs = [options.generated_profile]
765f9996aaSopenharmony_ci    build_utils.call_and_write_depfile_if_stale(
775f9996aaSopenharmony_ci        lambda: merge_profile(options),
785f9996aaSopenharmony_ci        options,
795f9996aaSopenharmony_ci        depfile_deps=depfiles,
805f9996aaSopenharmony_ci        input_paths=inputs + depfiles,
815f9996aaSopenharmony_ci        input_strings=input_strings,
825f9996aaSopenharmony_ci        output_paths=(outputs),
835f9996aaSopenharmony_ci        force=False,
845f9996aaSopenharmony_ci        add_pydeps=False)
855f9996aaSopenharmony_ci
865f9996aaSopenharmony_ciif __name__ == '__main__':
875f9996aaSopenharmony_ci    sys.exit(main(sys.argv[1:]))
88