blob: 277e11261854f3bf3683fc4c22a009d33df4e49b [file] [log] [blame]
Jooyung Han7113b192022-09-20 17:00:27 +09001#!/usr/bin/env python
2#
3# Copyright (C) 2022 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17"""A tool to create an APEX bundle out of Soong-built base.zip"""
18
Jooyung Han7113b192022-09-20 17:00:27 +090019import argparse
20import sys
21import tempfile
22import zipfile
23import os
24import json
25import subprocess
26
27
28def parse_args():
29 """Parse commandline arguments."""
30 parser = argparse.ArgumentParser()
31 parser.add_argument(
32 '--overwrite',
33 action='store_true',
34 help='If set, any previous existing output will be overwritten')
35 parser.add_argument('--output', help='specify the output .aab file')
36 parser.add_argument(
37 'input', help='specify the input <apex name>-base.zip file')
38 return parser.parse_args()
39
40
41def build_bundle(input, output, overwrite):
42 base_zip = zipfile.ZipFile(input)
43
44 tmpdir = tempfile.mkdtemp()
45 tmp_base_zip = os.path.join(tmpdir, 'base.zip')
46 tmp_bundle_config = os.path.join(tmpdir, 'bundle_config.json')
47
48 bundle_config = None
49 abi = []
50
51 # This block performs three tasks
52 # - extract/load bundle_config.json from input => bundle_config
53 # - get ABI from input => abi
54 # - discard bundle_config.json from input => tmp/base.zip
55 with zipfile.ZipFile(tmp_base_zip, 'a') as out:
56 for info in base_zip.infolist():
57
58 # discard bundle_config.json
59 if info.filename == 'bundle_config.json':
60 bundle_config = json.load(base_zip.open(info.filename))
61 continue
62
63 # get ABI from apex/{abi}.img
64 dir, basename = os.path.split(info.filename)
65 name, ext = os.path.splitext(basename)
66 if dir == 'apex' and ext == '.img':
67 abi.append(name)
68
69 # copy entries to tmp/base.zip
70 out.writestr(info, base_zip.open(info.filename).read())
71
72 base_zip.close()
73
74 if not bundle_config:
75 raise ValueError(f'bundle_config.json not found in {input}')
76 if len(abi) != 1:
77 raise ValueError(f'{input} should have only a single apex/*.img file')
78
79 # add ABI to tmp/bundle_config.json
80 apex_config = bundle_config['apex_config']
81 if 'supported_abi_set' not in apex_config:
82 apex_config['supported_abi_set'] = []
83 supported_abi_set = apex_config['supported_abi_set']
84 supported_abi_set.append({'abi': abi})
85
86 with open(tmp_bundle_config, 'w') as out:
87 json.dump(bundle_config, out)
88
89 # invoke bundletool
90 cmd = [
91 'bundletool', 'build-bundle', '--config', tmp_bundle_config, '--modules',
92 tmp_base_zip, '--output', output
93 ]
94 if overwrite:
95 cmd.append('--overwrite')
96 subprocess.check_call(cmd)
97
98
99def main():
100 """Program entry point."""
101 try:
102 args = parse_args()
103 build_bundle(args.input, args.output, args.overwrite)
104
105 # pylint: disable=broad-except
106 except Exception as err:
107 print('error: ' + str(err), file=sys.stderr)
108 sys.exit(-1)
109
110
111if __name__ == '__main__':
112 main()