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 android test using build/android/test_runner.py. 8""" 9 10import argparse 11import os 12import sys 13 14from util import build_utils 15 16SCRIPT_TEMPLATE = """\ 17#!/usr/bin/env python 18# 19# This file was generated by build/android/gyp/create_test_runner_script.py 20 21import os 22import subprocess 23import sys 24 25def main(): 26 script_directory = os.path.dirname(__file__) 27 28 def ResolvePath(path): 29 \"\"\"Returns an absolute filepath given a path relative to this script. 30 \"\"\" 31 return os.path.abspath(os.path.join(script_directory, path)) 32 33 test_runner_path = ResolvePath('{test_runner_path}') 34 test_runner_args = {test_runner_args} 35 test_runner_path_args = {test_runner_path_args} 36 for arg, path in test_runner_path_args: 37 test_runner_args.extend([arg, ResolvePath(path)]) 38 39 test_runner_cmd = [test_runner_path] + test_runner_args + sys.argv[1:] 40 return subprocess.call(test_runner_cmd) 41 42if __name__ == '__main__': 43 sys.exit(main()) 44""" 45 46def main(args): 47 parser = argparse.ArgumentParser() 48 parser.add_argument('--script-output-path', 49 help='Output path for executable script.') 50 parser.add_argument('--depfile', 51 help='Path to the depfile. This must be specified as ' 52 "the action's first output.") 53 parser.add_argument('--test-runner-path', 54 help='Path to test_runner.py (optional).') 55 # We need to intercept any test runner path arguments and make all 56 # of the paths relative to the output script directory. 57 group = parser.add_argument_group('Test runner path arguments.') 58 group.add_argument('--additional-apk', action='append', 59 dest='additional_apks', default=[]) 60 group.add_argument('--additional-apk-list') 61 group.add_argument('--apk-under-test') 62 group.add_argument('--apk-under-test-incremental-install-script') 63 group.add_argument('--executable-dist-dir') 64 group.add_argument('--isolate-file-path') 65 group.add_argument('--output-directory') 66 group.add_argument('--test-apk') 67 group.add_argument('--test-apk-incremental-install-script') 68 group.add_argument('--coverage-dir') 69 args, test_runner_args = parser.parse_known_args( 70 build_utils.ExpandFileArgs(args)) 71 72 def RelativizePathToScript(path): 73 """Returns the path relative to the output script directory.""" 74 return os.path.relpath(path, os.path.dirname(args.script_output_path)) 75 76 test_runner_path = args.test_runner_path or os.path.join( 77 os.path.dirname(__file__), os.path.pardir, 'test_runner.py') 78 test_runner_path = RelativizePathToScript(test_runner_path) 79 80 test_runner_path_args = [] 81 if args.additional_apk_list: 82 args.additional_apks.extend( 83 build_utils.ParseGypList(args.additional_apk_list)) 84 if args.additional_apks: 85 test_runner_path_args.extend( 86 ('--additional-apk', RelativizePathToScript(a)) 87 for a in args.additional_apks) 88 if args.apk_under_test: 89 test_runner_path_args.append( 90 ('--apk-under-test', RelativizePathToScript(args.apk_under_test))) 91 if args.apk_under_test_incremental_install_script: 92 test_runner_path_args.append( 93 ('--apk-under-test-incremental-install-script', 94 RelativizePathToScript( 95 args.apk_under_test_incremental_install_script))) 96 if args.executable_dist_dir: 97 test_runner_path_args.append( 98 ('--executable-dist-dir', 99 RelativizePathToScript(args.executable_dist_dir))) 100 if args.isolate_file_path: 101 test_runner_path_args.append( 102 ('--isolate-file-path', RelativizePathToScript(args.isolate_file_path))) 103 if args.output_directory: 104 test_runner_path_args.append( 105 ('--output-directory', RelativizePathToScript(args.output_directory))) 106 if args.test_apk: 107 test_runner_path_args.append( 108 ('--test-apk', RelativizePathToScript(args.test_apk))) 109 if args.test_apk_incremental_install_script: 110 test_runner_path_args.append( 111 ('--test-apk-incremental-install-script', 112 RelativizePathToScript(args.test_apk_incremental_install_script))) 113 if args.coverage_dir: 114 test_runner_path_args.append( 115 ('--coverage-dir', RelativizePathToScript(args.coverage_dir))) 116 117 with open(args.script_output_path, 'w') as script: 118 script.write(SCRIPT_TEMPLATE.format( 119 test_runner_path=str(test_runner_path), 120 test_runner_args=str(test_runner_args), 121 test_runner_path_args=str(test_runner_path_args))) 122 123 os.chmod(args.script_output_path, 0750) 124 125 if args.depfile: 126 build_utils.WriteDepfile( 127 args.depfile, 128 build_utils.GetPythonDependencies()) 129 130if __name__ == '__main__': 131 sys.exit(main(sys.argv[1:])) 132