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