• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# Copyright 2018 The Chromium Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Create a wrapper script to manage an Android App Bundle."""
7
8import argparse
9import os
10import string
11import sys
12
13from util import build_utils
14import action_helpers  # build_utils adds //build to sys.path.
15
16SCRIPT_TEMPLATE = string.Template("""\
17#!/usr/bin/env python3
18#
19# This file was generated by build/android/gyp/create_bundle_wrapper_script.py
20
21import os
22import sys
23
24def main():
25  script_directory = os.path.dirname(__file__)
26  resolve = lambda p: p if p is None else os.path.abspath(os.path.join(
27      script_directory, p))
28  sys.path.append(resolve(${WRAPPED_SCRIPT_DIR}))
29  import apk_operations
30
31  additional_apk_paths = [resolve(p) for p in ${ADDITIONAL_APK_PATHS}]
32  apk_operations.RunForBundle(output_directory=resolve(${OUTPUT_DIR}),
33                              bundle_path=resolve(${BUNDLE_PATH}),
34                              bundle_apks_path=resolve(${BUNDLE_APKS_PATH}),
35                              additional_apk_paths=additional_apk_paths,
36                              aapt2_path=resolve(${AAPT2_PATH}),
37                              keystore_path=resolve(${KEYSTORE_PATH}),
38                              keystore_password=${KEYSTORE_PASSWORD},
39                              keystore_alias=${KEY_NAME},
40                              package_name=${PACKAGE_NAME},
41                              command_line_flags_file=${FLAGS_FILE},
42                              proguard_mapping_path=resolve(${MAPPING_PATH}),
43                              target_cpu=${TARGET_CPU},
44                              system_image_locales=${SYSTEM_IMAGE_LOCALES},
45                              default_modules=${DEFAULT_MODULES})
46
47if __name__ == '__main__':
48  sys.exit(main())
49""")
50
51def main(args):
52  args = build_utils.ExpandFileArgs(args)
53  parser = argparse.ArgumentParser()
54  parser.add_argument('--script-output-path', required=True,
55                      help='Output path for executable script.')
56  parser.add_argument('--bundle-path', required=True)
57  parser.add_argument('--bundle-apks-path', required=True)
58  parser.add_argument(
59      '--additional-apk-path',
60      action='append',
61      dest='additional_apk_paths',
62      default=[],
63      help='Paths to APKs to be installed prior to --apk-path.')
64  parser.add_argument('--package-name', required=True)
65  parser.add_argument('--aapt2-path', required=True)
66  parser.add_argument('--keystore-path', required=True)
67  parser.add_argument('--keystore-password', required=True)
68  parser.add_argument('--key-name', required=True)
69  parser.add_argument('--command-line-flags-file')
70  parser.add_argument('--proguard-mapping-path')
71  parser.add_argument('--target-cpu')
72  parser.add_argument('--system-image-locales')
73  parser.add_argument('--default-modules', nargs='*', default=[])
74  args = parser.parse_args(args)
75
76  def relativize(path):
77    """Returns the path relative to the output script directory."""
78    if path is None:
79      return path
80    return os.path.relpath(path, os.path.dirname(args.script_output_path))
81
82  wrapped_script_dir = os.path.join(os.path.dirname(__file__), os.path.pardir)
83  wrapped_script_dir = relativize(wrapped_script_dir)
84  with open(args.script_output_path, 'w') as script:
85    script_dict = {
86        'WRAPPED_SCRIPT_DIR':
87        repr(wrapped_script_dir),
88        'OUTPUT_DIR':
89        repr(relativize('.')),
90        'BUNDLE_PATH':
91        repr(relativize(args.bundle_path)),
92        'BUNDLE_APKS_PATH':
93        repr(relativize(args.bundle_apks_path)),
94        'ADDITIONAL_APK_PATHS':
95        [relativize(p) for p in args.additional_apk_paths],
96        'PACKAGE_NAME':
97        repr(args.package_name),
98        'AAPT2_PATH':
99        repr(relativize(args.aapt2_path)),
100        'KEYSTORE_PATH':
101        repr(relativize(args.keystore_path)),
102        'KEYSTORE_PASSWORD':
103        repr(args.keystore_password),
104        'KEY_NAME':
105        repr(args.key_name),
106        'MAPPING_PATH':
107        repr(relativize(args.proguard_mapping_path)),
108        'FLAGS_FILE':
109        repr(args.command_line_flags_file),
110        'TARGET_CPU':
111        repr(args.target_cpu),
112        'SYSTEM_IMAGE_LOCALES':
113        repr(action_helpers.parse_gn_list(args.system_image_locales)),
114        'DEFAULT_MODULES':
115        repr(args.default_modules),
116    }
117    script.write(SCRIPT_TEMPLATE.substitute(script_dict))
118  os.chmod(args.script_output_path, 0o750)
119  return 0
120
121
122if __name__ == '__main__':
123  sys.exit(main(sys.argv[1:]))
124