• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2
3# Copyright 2015 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"""Creates a script to run an "_incremental" .apk."""
8
9import argparse
10import os
11import pprint
12import sys
13
14sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir))
15sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, 'gyp'))
16
17from pylib.constants import host_paths
18from util import build_utils
19
20
21SCRIPT_TEMPLATE = """\
22#!/usr/bin/env python
23#
24# This file was generated by:
25#     //build/android/incremental_install/create_install_script.py
26
27import os
28import subprocess
29import sys
30
31
32def _ResolvePath(path):
33  script_directory = os.path.dirname(__file__)
34  return os.path.abspath(os.path.join(script_directory, path))
35
36
37# Exported to allow test runner to be able to install incremental apks.
38def GetInstallParameters():
39  apk_path = {apk_path}
40  dex_files = {dex_files}
41  dont_even_try = {dont_even_try}
42  native_libs = {native_libs}
43  show_proguard_warning = {show_proguard_warning}
44  splits = {splits}
45
46  return dict(apk_path=_ResolvePath(apk_path),
47              dex_files=[_ResolvePath(p) for p in dex_files],
48              dont_even_try=dont_even_try,
49              native_libs=[_ResolvePath(p) for p in native_libs],
50              show_proguard_warning=show_proguard_warning,
51              splits=[_ResolvePath(p) for p in splits])
52
53
54def main():
55  output_directory = {output_directory}
56  cmd_path = {cmd_path}
57  params = GetInstallParameters()
58  cmd_args = [
59      _ResolvePath(cmd_path),
60      '--output-directory', _ResolvePath(output_directory),
61  ]
62  for native_lib in params['native_libs']:
63    cmd_args.extend(('--native_lib', native_lib))
64  for dex_path in params['dex_files']:
65    cmd_args.extend(('--dex-file', dex_path))
66  for split in params['splits']:
67    cmd_args.extend(('--split', split))
68  cmd_args.append(params['apk_path'])
69  if params['dont_even_try']:
70    cmd_args.extend(('--dont-even-try', params['dont_even_try']))
71  if params['show_proguard_warning']:
72    cmd_args.append('--show-proguard-warning')
73  return subprocess.call(cmd_args + sys.argv[1:])
74
75if __name__ == '__main__':
76  sys.exit(main())
77"""
78
79
80def _ParseArgs(args):
81  args = build_utils.ExpandFileArgs(args)
82  parser = argparse.ArgumentParser()
83  build_utils.AddDepfileOption(parser)
84  parser.add_argument('--script-output-path',
85                      help='Output path for executable script.',
86                      required=True)
87  parser.add_argument('--output-directory',
88                      help='Path to the root build directory.',
89                      default='.')
90  parser.add_argument('--apk-path',
91                      help='Path to the .apk to install.',
92                      required=True)
93  parser.add_argument('--split',
94                      action='append',
95                      dest='splits',
96                      default=[],
97                      help='A glob matching the apk splits. '
98                           'Can be specified multiple times.')
99  parser.add_argument('--native-libs',
100                      action='append',
101                      default=[],
102                      help='GYP-list of paths to native libraries. Can be '
103                      'repeated.')
104  parser.add_argument('--dex-file',
105                      action='append',
106                      default=[],
107                      dest='dex_files',
108                      help='List of dex files to include.')
109  parser.add_argument('--dex-file-list',
110                      help='GYP-list of dex files.')
111  parser.add_argument('--show-proguard-warning',
112                      action='store_true',
113                      default=False,
114                      help='Print a warning about proguard being disabled')
115  parser.add_argument('--dont-even-try',
116                      help='Prints this message and exits.')
117
118  options = parser.parse_args(args)
119  options.dex_files += build_utils.ParseGypList(options.dex_file_list)
120  all_libs = []
121  for gyp_list in options.native_libs:
122    all_libs.extend(build_utils.ParseGypList(gyp_list))
123  options.native_libs = all_libs
124  return options
125
126
127def main(args):
128  options = _ParseArgs(args)
129
130  def relativize(path):
131    script_dir = os.path.dirname(options.script_output_path)
132    return path and os.path.relpath(path, script_dir)
133
134  installer_path = os.path.join(host_paths.DIR_SOURCE_ROOT, 'build', 'android',
135                                'incremental_install', 'installer.py')
136  pformat = pprint.pformat
137  template_args = {
138      'cmd_path': pformat(relativize(installer_path)),
139      'apk_path': pformat(relativize(options.apk_path)),
140      'output_directory': pformat(relativize(options.output_directory)),
141      'native_libs': pformat([relativize(p) for p in options.native_libs]),
142      'dex_files': pformat([relativize(p) for p in options.dex_files]),
143      'dont_even_try': pformat(options.dont_even_try),
144      'show_proguard_warning': pformat(options.show_proguard_warning),
145      'splits': pformat([relativize(p) for p in options.splits]),
146  }
147
148  with open(options.script_output_path, 'w') as script:
149    script.write(SCRIPT_TEMPLATE.format(**template_args))
150
151  os.chmod(options.script_output_path, 0750)
152
153  if options.depfile:
154    build_utils.WriteDepfile(
155        options.depfile,
156        build_utils.GetPythonDependencies())
157
158
159if __name__ == '__main__':
160  sys.exit(main(sys.argv[1:]))
161