• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16import optparse
17import os
18import sys
19import shutil
20import json
21
22from zipfile import ZipFile  # noqa: E402
23from util import build_utils  # noqa: E402
24
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('--output-resources-zipfile',
32                      help='path to packaged resources')
33    parser.add_option('--output-header-file',
34                      help='path to generated ResourceTable.h')
35    parser.add_option('--resources-dir', help='resources directory')
36    parser.add_option('--restool-path', help='path to restool')
37    parser.add_option('--hap-profile', help='path to hap profile')
38    parser.add_option('--package-name', help='package name of resource file')
39    parser.add_option('--app-profile', default = False, help='path to app profile')
40
41    options, _ = parser.parse_args(args)
42    options.resources_dir = build_utils.parse_gn_list(options.resources_dir)
43    return options
44
45
46def get_package_name_from_profile(options):
47    with open(options.hap_profile) as fp:
48        if not options.app_profile:
49            return json.load(fp)['module']['package']
50        else:
51            return json.load(fp)['app']['bundleName']
52
53
54def compile_resources(options):
55    with build_utils.temp_dir() as build:
56        res_dir = os.path.join(build, 'gen/compile/resources')
57        gen_dir = os.path.join(os.path.dirname(options.output_header_file), "gen")
58        gen_dir = os.path.abspath(gen_dir)
59        header_dir = os.path.join(build, 'header')
60        os.makedirs(res_dir, exist_ok=True)
61        if os.path.exists(gen_dir):
62            shutil.rmtree(gen_dir)
63        os.makedirs(gen_dir, exist_ok=True)
64        os.makedirs(header_dir, exist_ok=True)
65
66        cmd = [options.restool_path]
67        for directory in options.resources_dir:
68            dest_res_dir = os.path.join(res_dir, os.path.dirname(directory))
69            shutil.copytree(os.path.dirname(directory), dest_res_dir)
70            cmd.extend(['-i', dest_res_dir])
71        cmd.extend(['-j', options.hap_profile])
72        if options.package_name != "" and options.package_name is not None:
73            package_name = options.package_name
74        else:
75            package_name = get_package_name_from_profile(options)
76        generated_header_file = os.path.join(
77            header_dir, os.path.basename(options.output_header_file))
78        cmd.extend(
79            ['-p', package_name, '-o', gen_dir, '-r', generated_header_file])
80        build_utils.check_output(cmd)
81        R_txt_path = os.path.join(gen_dir, 'R.txt')
82        if os.path.exists(R_txt_path):
83            os.unlink(R_txt_path)
84        if options.output_resources_zipfile:
85            build_utils.zip_dir(options.output_resources_zipfile, gen_dir)
86        if options.output_header_file:
87            shutil.copy(generated_header_file, options.output_header_file)
88
89
90def main(args):
91    options = parse_args(args)
92
93    if options.resources_dir == []:
94        with ZipFile(options.output_resources_zipfile, 'w') as file:
95            return
96    inputs = ([options.restool_path, options.hap_profile])
97    depfiles = []
98    for directory in options.resources_dir:
99        depfiles += (build_utils.get_all_files(directory))
100
101    input_strings = [options.package_name] if options.package_name else []
102    outputs = []
103    if options.output_resources_zipfile:
104        outputs.append(options.output_resources_zipfile)
105    if options.output_header_file:
106        outputs.append(options.output_header_file)
107    build_utils.call_and_write_depfile_if_stale(
108        lambda: compile_resources(options),
109        options,
110        depfile_deps=depfiles,
111        input_paths=inputs + depfiles,
112        input_strings=input_strings,
113        output_paths=(outputs),
114        force=False,
115        add_pydeps=False)
116
117
118if __name__ == '__main__':
119    sys.exit(main(sys.argv[1:]))
120