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 parser.add_option('--release-type', help='release type') 36 options, _ = parser.parse_args(args) 37 options.resources_dir = build_utils.parse_gn_list(options.resources_dir) 38 return options 39 40def merge_profile(options): 41 all_data = {} 42 with open(options.hap_profile) as f0: 43 if len(options.app_profile) == 0: 44 all_data = json.load(f0) 45 else: 46 module_data = json.load(f0)["module"] 47 with open(options.app_profile) as f1: 48 app_data = json.load(f1)["app"] 49 all_data["app"] = app_data 50 all_data["module"] = module_data 51 f1.close() 52 f0.close() 53 all_data["app"]["apiReleaseType"] = options.release_type 54 f3 = open(options.generated_profile, "w") 55 json.dump(all_data, f3, indent=4, ensure_ascii=False) 56 f3.close() 57 58def main(args): 59 options = parse_args(args) 60 inputs = [options.hap_profile] 61 if not options.app_profile: 62 inputs += options.app_profile 63 depfiles = [] 64 for directory in options.resources_dir: 65 depfiles += (build_utils.get_all_files(directory)) 66 67 input_strings = [] 68 outputs = [options.generated_profile] 69 build_utils.call_and_write_depfile_if_stale( 70 lambda: merge_profile(options), 71 options, 72 depfile_deps=depfiles, 73 input_paths=inputs + depfiles, 74 input_strings=input_strings, 75 output_paths=(outputs), 76 force=False, 77 add_pydeps=False) 78 79if __name__ == '__main__': 80 sys.exit(main(sys.argv[1:])) 81