1#!/usr/bin/env python3 2# Copyright 2022 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"""Create a wrapper script to run a test apk using apk_operations.py.""" 6 7import argparse 8import os 9import string 10import sys 11 12from util import build_utils 13 14SCRIPT_TEMPLATE = string.Template("""\ 15#!/usr/bin/env python3 16# 17# This file was generated by build/android/gyp/create_test_apk_wrapper_script.py 18 19import os 20import sys 21 22def main(): 23 script_directory = os.path.dirname(__file__) 24 resolve = lambda p: p if p is None else os.path.abspath(os.path.join( 25 script_directory, p)) 26 sys.path.append(resolve(${WRAPPED_SCRIPT_DIR})) 27 import apk_operations 28 29 additional_apk_paths = [resolve(p) for p in ${ADDITIONAL_APKS}] 30 apk_operations.RunForTestApk( 31 output_directory=resolve(${OUTPUT_DIR}), 32 package_name=${PACKAGE_NAME}, 33 test_apk_path=resolve(${TEST_APK}), 34 test_apk_json=resolve(${TEST_APK_JSON}), 35 proguard_mapping_path=resolve(${MAPPING_PATH}), 36 additional_apk_paths=additional_apk_paths) 37 38if __name__ == '__main__': 39 sys.exit(main()) 40""") 41 42 43def main(args): 44 args = build_utils.ExpandFileArgs(args) 45 parser = argparse.ArgumentParser() 46 parser.add_argument('--script-output-path', 47 required=True, 48 help='Output path for executable script.') 49 parser.add_argument('--package-name', required=True) 50 parser.add_argument('--test-apk') 51 parser.add_argument('--test-apk-incremental-install-json') 52 parser.add_argument('--proguard-mapping-path') 53 parser.add_argument('--additional-apk', 54 action='append', 55 dest='additional_apks', 56 default=[], 57 help='Paths to APKs to be installed prior to --apk-path.') 58 args = parser.parse_args(args) 59 60 def relativize(path): 61 """Returns the path relative to the output script directory.""" 62 if path is None: 63 return path 64 return os.path.relpath(path, os.path.dirname(args.script_output_path)) 65 66 wrapped_script_dir = os.path.join(os.path.dirname(__file__), os.path.pardir) 67 wrapped_script_dir = relativize(wrapped_script_dir) 68 with open(args.script_output_path, 'w') as script: 69 script_dict = { 70 'WRAPPED_SCRIPT_DIR': repr(wrapped_script_dir), 71 'OUTPUT_DIR': repr(relativize('.')), 72 'PACKAGE_NAME': repr(args.package_name), 73 'TEST_APK': repr(relativize(args.test_apk)), 74 'TEST_APK_JSON': 75 repr(relativize(args.test_apk_incremental_install_json)), 76 'MAPPING_PATH': repr(relativize(args.proguard_mapping_path)), 77 'ADDITIONAL_APKS': [relativize(p) for p in args.additional_apks], 78 } 79 script.write(SCRIPT_TEMPLATE.substitute(script_dict)) 80 os.chmod(args.script_output_path, 0o750) 81 return 0 82 83 84if __name__ == '__main__': 85 sys.exit(main(sys.argv[1:])) 86