1*9e94795aSAndroid Build Coastguard Worker#!/usr/bin/env python3 2*9e94795aSAndroid Build Coastguard Worker# 3*9e94795aSAndroid Build Coastguard Worker# Copyright (C) 2017 The Android Open Source Project 4*9e94795aSAndroid Build Coastguard Worker# 5*9e94795aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 6*9e94795aSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 7*9e94795aSAndroid Build Coastguard Worker# You may obtain a copy of the License at 8*9e94795aSAndroid Build Coastguard Worker# 9*9e94795aSAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 10*9e94795aSAndroid Build Coastguard Worker# 11*9e94795aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 12*9e94795aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 13*9e94795aSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*9e94795aSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 15*9e94795aSAndroid Build Coastguard Worker# limitations under the License. 16*9e94795aSAndroid Build Coastguard Worker 17*9e94795aSAndroid Build Coastguard Worker"""A tool to generate TradeFed test config file. 18*9e94795aSAndroid Build Coastguard Worker""" 19*9e94795aSAndroid Build Coastguard Worker 20*9e94795aSAndroid Build Coastguard Workerimport argparse 21*9e94795aSAndroid Build Coastguard Workerimport re 22*9e94795aSAndroid Build Coastguard Workerimport os 23*9e94795aSAndroid Build Coastguard Workerimport shutil 24*9e94795aSAndroid Build Coastguard Workerimport sys 25*9e94795aSAndroid Build Coastguard Workerfrom xml.dom.minidom import parse 26*9e94795aSAndroid Build Coastguard Worker 27*9e94795aSAndroid Build Coastguard WorkerATTRIBUTE_LABEL = 'android:label' 28*9e94795aSAndroid Build Coastguard WorkerATTRIBUTE_RUNNER = 'android:name' 29*9e94795aSAndroid Build Coastguard WorkerATTRIBUTE_PACKAGE = 'package' 30*9e94795aSAndroid Build Coastguard Worker 31*9e94795aSAndroid Build Coastguard WorkerPLACEHOLDER_LABEL = '{LABEL}' 32*9e94795aSAndroid Build Coastguard WorkerPLACEHOLDER_EXTRA_CONFIGS = '{EXTRA_CONFIGS}' 33*9e94795aSAndroid Build Coastguard WorkerPLACEHOLDER_MODULE = '{MODULE}' 34*9e94795aSAndroid Build Coastguard WorkerPLACEHOLDER_PACKAGE = '{PACKAGE}' 35*9e94795aSAndroid Build Coastguard WorkerPLACEHOLDER_RUNNER = '{RUNNER}' 36*9e94795aSAndroid Build Coastguard WorkerPLACEHOLDER_TEST_TYPE = '{TEST_TYPE}' 37*9e94795aSAndroid Build Coastguard WorkerPLACEHOLDER_EXTRA_TEST_RUNNER_CONFIGS = '{EXTRA_TEST_RUNNER_CONFIGS}' 38*9e94795aSAndroid Build Coastguard Worker 39*9e94795aSAndroid Build Coastguard Worker 40*9e94795aSAndroid Build Coastguard Workerdef main(argv): 41*9e94795aSAndroid Build Coastguard Worker """Entry point of auto_gen_test_config. 42*9e94795aSAndroid Build Coastguard Worker 43*9e94795aSAndroid Build Coastguard Worker Args: 44*9e94795aSAndroid Build Coastguard Worker argv: A list of arguments. 45*9e94795aSAndroid Build Coastguard Worker Returns: 46*9e94795aSAndroid Build Coastguard Worker 0 if no error, otherwise 1. 47*9e94795aSAndroid Build Coastguard Worker """ 48*9e94795aSAndroid Build Coastguard Worker 49*9e94795aSAndroid Build Coastguard Worker parser = argparse.ArgumentParser() 50*9e94795aSAndroid Build Coastguard Worker parser.add_argument( 51*9e94795aSAndroid Build Coastguard Worker "target_config", 52*9e94795aSAndroid Build Coastguard Worker help="Path to the generated output config.") 53*9e94795aSAndroid Build Coastguard Worker parser.add_argument( 54*9e94795aSAndroid Build Coastguard Worker "android_manifest", 55*9e94795aSAndroid Build Coastguard Worker help="Path to AndroidManifest.xml or output of 'aapt2 dump xmltree' with .xmltree extension.") 56*9e94795aSAndroid Build Coastguard Worker parser.add_argument( 57*9e94795aSAndroid Build Coastguard Worker "empty_config", 58*9e94795aSAndroid Build Coastguard Worker help="Path to the empty config template.") 59*9e94795aSAndroid Build Coastguard Worker parser.add_argument( 60*9e94795aSAndroid Build Coastguard Worker "instrumentation_test_config_template", 61*9e94795aSAndroid Build Coastguard Worker help="Path to the instrumentation test config template.") 62*9e94795aSAndroid Build Coastguard Worker parser.add_argument("--extra-configs", default="") 63*9e94795aSAndroid Build Coastguard Worker parser.add_argument("--extra-test-runner-configs", default="") 64*9e94795aSAndroid Build Coastguard Worker args = parser.parse_args(argv) 65*9e94795aSAndroid Build Coastguard Worker 66*9e94795aSAndroid Build Coastguard Worker target_config = args.target_config 67*9e94795aSAndroid Build Coastguard Worker android_manifest = args.android_manifest 68*9e94795aSAndroid Build Coastguard Worker empty_config = args.empty_config 69*9e94795aSAndroid Build Coastguard Worker instrumentation_test_config_template = args.instrumentation_test_config_template 70*9e94795aSAndroid Build Coastguard Worker extra_configs = '\n'.join(args.extra_configs.split('\\n')) 71*9e94795aSAndroid Build Coastguard Worker extra_test_runner_configs = '\n'.join(args.extra_test_runner_configs.split('\\n')) 72*9e94795aSAndroid Build Coastguard Worker 73*9e94795aSAndroid Build Coastguard Worker module = os.path.splitext(os.path.basename(target_config))[0] 74*9e94795aSAndroid Build Coastguard Worker 75*9e94795aSAndroid Build Coastguard Worker # If the AndroidManifest.xml is not available, but the APK is, this tool also 76*9e94795aSAndroid Build Coastguard Worker # accepts the output of `aapt2 dump xmltree <apk> AndroidManifest.xml` written 77*9e94795aSAndroid Build Coastguard Worker # into a file. This is a custom structured aapt2 output - not raw XML! 78*9e94795aSAndroid Build Coastguard Worker if android_manifest.endswith(".xmltree"): 79*9e94795aSAndroid Build Coastguard Worker label = module 80*9e94795aSAndroid Build Coastguard Worker with open(android_manifest, encoding="utf-8") as manifest: 81*9e94795aSAndroid Build Coastguard Worker # e.g. A: package="android.test.example.helloworld" (Raw: "android.test.example.helloworld") 82*9e94795aSAndroid Build Coastguard Worker # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 83*9e94795aSAndroid Build Coastguard Worker pattern = re.compile(r"\(Raw:\s\"(.*)\"\)$") 84*9e94795aSAndroid Build Coastguard Worker curr_element = None 85*9e94795aSAndroid Build Coastguard Worker for line in manifest: 86*9e94795aSAndroid Build Coastguard Worker curr_line = line.strip() 87*9e94795aSAndroid Build Coastguard Worker if curr_line.startswith("E:"): 88*9e94795aSAndroid Build Coastguard Worker # e.g. "E: instrumentation (line=9)" 89*9e94795aSAndroid Build Coastguard Worker # ^^^^^^^^^^^^^^^ 90*9e94795aSAndroid Build Coastguard Worker curr_element = curr_line.split(" ")[1] 91*9e94795aSAndroid Build Coastguard Worker if curr_element == "instrumentation": 92*9e94795aSAndroid Build Coastguard Worker if ATTRIBUTE_RUNNER in curr_line: 93*9e94795aSAndroid Build Coastguard Worker runner = re.findall(pattern, curr_line)[0] 94*9e94795aSAndroid Build Coastguard Worker if ATTRIBUTE_LABEL in curr_line: 95*9e94795aSAndroid Build Coastguard Worker label = re.findall(pattern, curr_line)[0] 96*9e94795aSAndroid Build Coastguard Worker if curr_element == "manifest": 97*9e94795aSAndroid Build Coastguard Worker if ATTRIBUTE_PACKAGE in curr_line: 98*9e94795aSAndroid Build Coastguard Worker package = re.findall(pattern, curr_line)[0] 99*9e94795aSAndroid Build Coastguard Worker 100*9e94795aSAndroid Build Coastguard Worker if not (runner and label and package): 101*9e94795aSAndroid Build Coastguard Worker # Failed to locate instrumentation or manifest element in AndroidManifest. 102*9e94795aSAndroid Build Coastguard Worker # file. Empty test config file will be created. 103*9e94795aSAndroid Build Coastguard Worker shutil.copyfile(empty_config, target_config) 104*9e94795aSAndroid Build Coastguard Worker return 0 105*9e94795aSAndroid Build Coastguard Worker 106*9e94795aSAndroid Build Coastguard Worker else: 107*9e94795aSAndroid Build Coastguard Worker # If the AndroidManifest.xml file is directly available, read it as an XML file. 108*9e94795aSAndroid Build Coastguard Worker manifest = parse(android_manifest) 109*9e94795aSAndroid Build Coastguard Worker instrumentation_elements = manifest.getElementsByTagName('instrumentation') 110*9e94795aSAndroid Build Coastguard Worker manifest_elements = manifest.getElementsByTagName('manifest') 111*9e94795aSAndroid Build Coastguard Worker if len(instrumentation_elements) != 1 or len(manifest_elements) != 1: 112*9e94795aSAndroid Build Coastguard Worker # Failed to locate instrumentation or manifest element in AndroidManifest. 113*9e94795aSAndroid Build Coastguard Worker # file. Empty test config file will be created. 114*9e94795aSAndroid Build Coastguard Worker shutil.copyfile(empty_config, target_config) 115*9e94795aSAndroid Build Coastguard Worker return 0 116*9e94795aSAndroid Build Coastguard Worker 117*9e94795aSAndroid Build Coastguard Worker instrumentation = instrumentation_elements[0] 118*9e94795aSAndroid Build Coastguard Worker manifest = manifest_elements[0] 119*9e94795aSAndroid Build Coastguard Worker if ATTRIBUTE_LABEL in instrumentation.attributes: 120*9e94795aSAndroid Build Coastguard Worker label = instrumentation.attributes[ATTRIBUTE_LABEL].value 121*9e94795aSAndroid Build Coastguard Worker else: 122*9e94795aSAndroid Build Coastguard Worker label = module 123*9e94795aSAndroid Build Coastguard Worker runner = instrumentation.attributes[ATTRIBUTE_RUNNER].value 124*9e94795aSAndroid Build Coastguard Worker package = manifest.attributes[ATTRIBUTE_PACKAGE].value 125*9e94795aSAndroid Build Coastguard Worker 126*9e94795aSAndroid Build Coastguard Worker test_type = ('InstrumentationTest' 127*9e94795aSAndroid Build Coastguard Worker if runner.endswith('.InstrumentationTestRunner') 128*9e94795aSAndroid Build Coastguard Worker else 'AndroidJUnitTest') 129*9e94795aSAndroid Build Coastguard Worker 130*9e94795aSAndroid Build Coastguard Worker with open(instrumentation_test_config_template) as template: 131*9e94795aSAndroid Build Coastguard Worker config = template.read() 132*9e94795aSAndroid Build Coastguard Worker config = config.replace(PLACEHOLDER_LABEL, label) 133*9e94795aSAndroid Build Coastguard Worker config = config.replace(PLACEHOLDER_MODULE, module) 134*9e94795aSAndroid Build Coastguard Worker config = config.replace(PLACEHOLDER_PACKAGE, package) 135*9e94795aSAndroid Build Coastguard Worker config = config.replace(PLACEHOLDER_TEST_TYPE, test_type) 136*9e94795aSAndroid Build Coastguard Worker config = config.replace(PLACEHOLDER_EXTRA_CONFIGS, extra_configs) 137*9e94795aSAndroid Build Coastguard Worker config = config.replace(PLACEHOLDER_EXTRA_TEST_RUNNER_CONFIGS, extra_test_runner_configs) 138*9e94795aSAndroid Build Coastguard Worker config = config.replace(PLACEHOLDER_RUNNER, runner) 139*9e94795aSAndroid Build Coastguard Worker with open(target_config, 'w') as config_file: 140*9e94795aSAndroid Build Coastguard Worker config_file.write(config) 141*9e94795aSAndroid Build Coastguard Worker return 0 142*9e94795aSAndroid Build Coastguard Worker 143*9e94795aSAndroid Build Coastguard Workerif __name__ == '__main__': 144*9e94795aSAndroid Build Coastguard Worker sys.exit(main(sys.argv[1:])) 145