1#!/usr/bin/env python3 2# Copyright 2017 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 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_apk_operations_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(${APK_OPERATIONS_DIR})) 27 import apk_operations 28 output_dir = resolve(${OUTPUT_DIR}) 29 apk_operations.Run( 30 output_dir, 31 resolve(${APK_PATH}), 32 [resolve(p) for p in ${ADDITIONAL_APK_PATHS}], 33 resolve(${INC_JSON_PATH}), 34 ${FLAGS_FILE}, 35 ${TARGET_CPU}, 36 resolve(${MAPPING_PATH})) 37 38 39if __name__ == '__main__': 40 sys.exit(main()) 41""") 42 43 44def main(args): 45 args = build_utils.ExpandFileArgs(args) 46 parser = argparse.ArgumentParser() 47 parser.add_argument('--script-output-path', 48 help='Output path for executable script.') 49 parser.add_argument('--apk-path') 50 parser.add_argument('--incremental-install-json-path') 51 parser.add_argument('--command-line-flags-file') 52 parser.add_argument('--target-cpu') 53 parser.add_argument( 54 '--additional-apk-path', 55 action='append', 56 dest='additional_apk_paths', 57 default=[], 58 help='Paths to APKs to be installed prior to --apk-path.') 59 parser.add_argument('--proguard-mapping-path') 60 args = parser.parse_args(args) 61 62 def relativize(path): 63 """Returns the path relative to the output script directory.""" 64 if path is None: 65 return path 66 return os.path.relpath(path, os.path.dirname(args.script_output_path)) 67 apk_operations_dir = os.path.join(os.path.dirname(__file__), os.path.pardir) 68 apk_operations_dir = relativize(apk_operations_dir) 69 70 with open(args.script_output_path, 'w') as script: 71 script_dict = { 72 'APK_OPERATIONS_DIR': repr(apk_operations_dir), 73 'OUTPUT_DIR': repr(relativize('.')), 74 'APK_PATH': repr(relativize(args.apk_path)), 75 'ADDITIONAL_APK_PATHS': 76 [relativize(p) for p in args.additional_apk_paths], 77 'INC_JSON_PATH': repr(relativize(args.incremental_install_json_path)), 78 'MAPPING_PATH': repr(relativize(args.proguard_mapping_path)), 79 'FLAGS_FILE': repr(args.command_line_flags_file), 80 'TARGET_CPU': repr(args.target_cpu), 81 } 82 script.write(SCRIPT_TEMPLATE.substitute(script_dict)) 83 os.chmod(args.script_output_path, 0o750) 84 return 0 85 86 87if __name__ == '__main__': 88 sys.exit(main(sys.argv[1:])) 89