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_ciimport optparse 175f9996aaSopenharmony_ciimport os 185f9996aaSopenharmony_ciimport sys 195f9996aaSopenharmony_ciimport shutil 205f9996aaSopenharmony_ciimport json 215f9996aaSopenharmony_ci 225f9996aaSopenharmony_cifrom zipfile import ZipFile # noqa: E402 235f9996aaSopenharmony_cifrom util import build_utils # noqa: E402 245f9996aaSopenharmony_ci 255f9996aaSopenharmony_ci 265f9996aaSopenharmony_cidef parse_args(args): 275f9996aaSopenharmony_ci args = build_utils.expand_file_args(args) 285f9996aaSopenharmony_ci 295f9996aaSopenharmony_ci parser = optparse.OptionParser() 305f9996aaSopenharmony_ci build_utils.add_depfile_option(parser) 315f9996aaSopenharmony_ci parser.add_option('--output-resources-zipfile', 325f9996aaSopenharmony_ci help='path to packaged resources') 335f9996aaSopenharmony_ci parser.add_option('--output-header-file', 345f9996aaSopenharmony_ci help='path to generated ResourceTable.h') 355f9996aaSopenharmony_ci parser.add_option('--resources-dir', help='resources directory') 365f9996aaSopenharmony_ci parser.add_option('--restool-path', help='path to restool') 375f9996aaSopenharmony_ci parser.add_option('--hap-profile', help='path to hap profile') 385f9996aaSopenharmony_ci parser.add_option('--package-name', help='package name of resource file') 395f9996aaSopenharmony_ci parser.add_option('--app-profile', default = False, help = 'path to app profile') 405f9996aaSopenharmony_ci 415f9996aaSopenharmony_ci options, _ = parser.parse_args(args) 425f9996aaSopenharmony_ci options.resources_dir = build_utils.parse_gn_list(options.resources_dir) 435f9996aaSopenharmony_ci return options 445f9996aaSopenharmony_ci 455f9996aaSopenharmony_ci 465f9996aaSopenharmony_cidef get_package_name_from_profile(options): 475f9996aaSopenharmony_ci with open(options.hap_profile) as fp: 485f9996aaSopenharmony_ci if not options.app_profile: 495f9996aaSopenharmony_ci return json.load(fp)['module']['package'] 505f9996aaSopenharmony_ci else: 515f9996aaSopenharmony_ci return json.load(fp)['app']['bundleName'] 525f9996aaSopenharmony_ci 535f9996aaSopenharmony_ci 545f9996aaSopenharmony_cidef compile_resources(options): 555f9996aaSopenharmony_ci with build_utils.temp_dir() as build: 565f9996aaSopenharmony_ci res_dir = os.path.join(build, 'gen/compile/resources') 575f9996aaSopenharmony_ci gen_dir = os.path.join(os.path.dirname(options.output_header_file), "gen") 585f9996aaSopenharmony_ci gen_dir = os.path.abspath(gen_dir) 595f9996aaSopenharmony_ci header_dir = os.path.join(build, 'header') 605f9996aaSopenharmony_ci os.makedirs(res_dir, exist_ok=True) 615f9996aaSopenharmony_ci if os.path.exists(gen_dir): 625f9996aaSopenharmony_ci shutil.rmtree(gen_dir) 635f9996aaSopenharmony_ci os.makedirs(gen_dir, exist_ok=True) 645f9996aaSopenharmony_ci os.makedirs(header_dir, exist_ok=True) 655f9996aaSopenharmony_ci 665f9996aaSopenharmony_ci cmd = [options.restool_path] 675f9996aaSopenharmony_ci for directory in options.resources_dir: 685f9996aaSopenharmony_ci dest_res_dir = os.path.join(res_dir, os.path.dirname(directory)) 695f9996aaSopenharmony_ci shutil.copytree(os.path.dirname(directory), dest_res_dir) 705f9996aaSopenharmony_ci cmd.extend(['-i', dest_res_dir]) 715f9996aaSopenharmony_ci cmd.extend(['-j', options.hap_profile]) 725f9996aaSopenharmony_ci if options.package_name != "" and options.package_name is not None: 735f9996aaSopenharmony_ci package_name = options.package_name 745f9996aaSopenharmony_ci else: 755f9996aaSopenharmony_ci package_name = get_package_name_from_profile(options) 765f9996aaSopenharmony_ci generated_header_file = os.path.join( 775f9996aaSopenharmony_ci header_dir, os.path.basename(options.output_header_file)) 785f9996aaSopenharmony_ci cmd.extend( 795f9996aaSopenharmony_ci ['-p', package_name, '-o', gen_dir, '-r', generated_header_file]) 805f9996aaSopenharmony_ci build_utils.check_output(cmd) 815f9996aaSopenharmony_ci txt_path = os.path.join(gen_dir, 'R.txt') 825f9996aaSopenharmony_ci if os.path.exists(txt_path): 835f9996aaSopenharmony_ci os.unlink(txt_path) 845f9996aaSopenharmony_ci if options.output_resources_zipfile: 855f9996aaSopenharmony_ci build_utils.zip_dir(options.output_resources_zipfile, gen_dir) 865f9996aaSopenharmony_ci if options.output_header_file: 875f9996aaSopenharmony_ci shutil.copy(generated_header_file, options.output_header_file) 885f9996aaSopenharmony_ci 895f9996aaSopenharmony_ci 905f9996aaSopenharmony_cidef main(args): 915f9996aaSopenharmony_ci options = parse_args(args) 925f9996aaSopenharmony_ci 935f9996aaSopenharmony_ci if options.resources_dir == []: 945f9996aaSopenharmony_ci with ZipFile(options.output_resources_zipfile, 'w') as file: 955f9996aaSopenharmony_ci return 965f9996aaSopenharmony_ci inputs = ([options.restool_path, options.hap_profile]) 975f9996aaSopenharmony_ci depfiles = [] 985f9996aaSopenharmony_ci for directory in options.resources_dir: 995f9996aaSopenharmony_ci depfiles += (build_utils.get_all_files(directory)) 1005f9996aaSopenharmony_ci 1015f9996aaSopenharmony_ci input_strings = [options.package_name] if options.package_name else [] 1025f9996aaSopenharmony_ci outputs = [] 1035f9996aaSopenharmony_ci if options.output_resources_zipfile: 1045f9996aaSopenharmony_ci outputs.append(options.output_resources_zipfile) 1055f9996aaSopenharmony_ci if options.output_header_file: 1065f9996aaSopenharmony_ci outputs.append(options.output_header_file) 1075f9996aaSopenharmony_ci build_utils.call_and_write_depfile_if_stale( 1085f9996aaSopenharmony_ci lambda: compile_resources(options), 1095f9996aaSopenharmony_ci options, 1105f9996aaSopenharmony_ci depfile_deps=depfiles, 1115f9996aaSopenharmony_ci input_paths=inputs + depfiles, 1125f9996aaSopenharmony_ci input_strings=input_strings, 1135f9996aaSopenharmony_ci output_paths=(outputs), 1145f9996aaSopenharmony_ci force=False, 1155f9996aaSopenharmony_ci add_pydeps=False) 1165f9996aaSopenharmony_ci 1175f9996aaSopenharmony_ci 1185f9996aaSopenharmony_ciif __name__ == '__main__': 1195f9996aaSopenharmony_ci sys.exit(main(sys.argv[1:])) 120