1#!/usr/bin/env python
2#
3# Copyright 2013 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""Combines stripped libraries and incomplete APK into single standalone APK.
8
9"""
10
11import optparse
12import os
13import shutil
14import sys
15import tempfile
16
17from util import build_utils
18from util import md5_check
19
20def CreateStandaloneApk(options):
21  def DoZip():
22    with tempfile.NamedTemporaryFile(suffix='.zip') as intermediate_file:
23      intermediate_path = intermediate_file.name
24      shutil.copy(options.input_apk_path, intermediate_path)
25      apk_path_abs = os.path.abspath(intermediate_path)
26      build_utils.CheckOutput(
27          ['zip', '-r', '-1', apk_path_abs, 'lib'],
28          cwd=options.libraries_top_dir)
29      shutil.copy(intermediate_path, options.output_apk_path)
30
31  input_paths = [options.input_apk_path, options.libraries_top_dir]
32  record_path = '%s.standalone.stamp' % options.input_apk_path
33  md5_check.CallAndRecordIfStale(
34      DoZip,
35      record_path=record_path,
36      input_paths=input_paths)
37
38
39def main():
40  parser = optparse.OptionParser()
41  parser.add_option('--libraries-top-dir',
42      help='Top directory that contains libraries '
43      '(i.e. library paths are like '
44      'libraries_top_dir/lib/android_app_abi/foo.so).')
45  parser.add_option('--input-apk-path', help='Path to incomplete APK.')
46  parser.add_option('--output-apk-path', help='Path for standalone APK.')
47  parser.add_option('--stamp', help='Path to touch on success.')
48  options, _ = parser.parse_args()
49
50  required_options = ['libraries_top_dir', 'input_apk_path', 'output_apk_path']
51  build_utils.CheckOptions(options, parser, required=required_options)
52
53  CreateStandaloneApk(options)
54
55  if options.stamp:
56    build_utils.Touch(options.stamp)
57
58
59if __name__ == '__main__':
60  sys.exit(main())
61