aboutsummaryrefslogtreecommitdiff
path: root/scripts/build-apex-bundle.py
blob: dcdd9ef7d8b178b538a351b86ffcd4e3bfd273ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python
#
# Copyright (C) 2022 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""A tool to create an APEX bundle out of Soong-built base.zip"""

from __future__ import print_function

import argparse
import sys
import tempfile
import zipfile
import os
import json
import subprocess


def parse_args():
  """Parse commandline arguments."""
  parser = argparse.ArgumentParser()
  parser.add_argument(
      '--overwrite',
      action='store_true',
      help='If set, any previous existing output will be overwritten')
  parser.add_argument('--output', help='specify the output .aab file')
  parser.add_argument(
      'input', help='specify the input <apex name>-base.zip file')
  return parser.parse_args()


def build_bundle(input, output, overwrite):
  base_zip = zipfile.ZipFile(input)

  tmpdir = tempfile.mkdtemp()
  tmp_base_zip = os.path.join(tmpdir, 'base.zip')
  tmp_bundle_config = os.path.join(tmpdir, 'bundle_config.json')

  bundle_config = None
  abi = []

  # This block performs three tasks
  # - extract/load bundle_config.json from input => bundle_config
  # - get ABI from input => abi
  # - discard bundle_config.json from input => tmp/base.zip
  with zipfile.ZipFile(tmp_base_zip, 'a') as out:
    for info in base_zip.infolist():

      # discard bundle_config.json
      if info.filename == 'bundle_config.json':
        bundle_config = json.load(base_zip.open(info.filename))
        continue

      # get ABI from apex/{abi}.img
      dir, basename = os.path.split(info.filename)
      name, ext = os.path.splitext(basename)
      if dir == 'apex' and ext == '.img':
        abi.append(name)

      # copy entries to tmp/base.zip
      out.writestr(info, base_zip.open(info.filename).read())

  base_zip.close()

  if not bundle_config:
    raise ValueError(f'bundle_config.json not found in {input}')
  if len(abi) != 1:
    raise ValueError(f'{input} should have only a single apex/*.img file')

  # add ABI to tmp/bundle_config.json
  apex_config = bundle_config['apex_config']
  if 'supported_abi_set' not in apex_config:
    apex_config['supported_abi_set'] = []
  supported_abi_set = apex_config['supported_abi_set']
  supported_abi_set.append({'abi': abi})

  with open(tmp_bundle_config, 'w') as out:
    json.dump(bundle_config, out)

  # invoke bundletool
  cmd = [
      'bundletool', 'build-bundle', '--config', tmp_bundle_config, '--modules',
      tmp_base_zip, '--output', output
  ]
  if overwrite:
    cmd.append('--overwrite')
  subprocess.check_call(cmd)


def main():
  """Program entry point."""
  try:
    args = parse_args()
    build_bundle(args.input, args.output, args.overwrite)

  # pylint: disable=broad-except
  except Exception as err:
    print('error: ' + str(err), file=sys.stderr)
    sys.exit(-1)


if __name__ == '__main__':
  main()