1#!/usr/bin/env python 2# 3# Copyright (C) 2019 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17"""A tool for modifying values in a test config.""" 18 19from __future__ import print_function 20 21import argparse 22import sys 23from xml.dom import minidom 24 25 26from manifest import get_children_with_tag 27from manifest import parse_manifest 28from manifest import parse_test_config 29from manifest import write_xml 30 31KNOWN_PREPARERS = ['com.android.tradefed.targetprep.TestAppInstallSetup', 32 'com.android.tradefed.targetprep.suite.SuiteApkInstaller'] 33 34MAINLINE_CONTROLLER = 'com.android.tradefed.testtype.suite.module.MainlineTestModuleController' 35 36def parse_args(): 37 """Parse commandline arguments.""" 38 39 parser = argparse.ArgumentParser() 40 parser.add_argument('--manifest', default='', dest='manifest', 41 help=('AndroidManifest.xml that contains the original package name')) 42 parser.add_argument('--package-name', default='', dest='package_name', 43 help=('overwrite package fields in the test config')) 44 parser.add_argument('--test-file-name', default='', dest='test_file_name', 45 help=('overwrite test file name in the test config')) 46 parser.add_argument('--mainline-package-name', default='', dest='mainline_package_name', 47 help=('overwrite mainline module package name in the test config')) 48 parser.add_argument('input', help='input test config file') 49 parser.add_argument('output', help='output test config file') 50 return parser.parse_args() 51 52 53def overwrite_package_name(test_config_doc, manifest_doc, package_name): 54 55 manifest = parse_manifest(manifest_doc) 56 original_package = manifest.getAttribute('package') 57 58 test_config = parse_test_config(test_config_doc) 59 tests = get_children_with_tag(test_config, 'test') 60 61 for test in tests: 62 options = get_children_with_tag(test, 'option') 63 for option in options: 64 if option.getAttribute('name') == "package" and option.getAttribute('value') == original_package: 65 option.setAttribute('value', package_name) 66 67def overwrite_test_file_name(test_config_doc, test_file_name): 68 69 test_config = parse_test_config(test_config_doc) 70 tests = get_children_with_tag(test_config, 'target_preparer') 71 72 for test in tests: 73 if test.getAttribute('class') in KNOWN_PREPARERS: 74 options = get_children_with_tag(test, 'option') 75 for option in options: 76 if option.getAttribute('name') == "test-file-name": 77 option.setAttribute('value', test_file_name) 78 79def overwrite_mainline_module_package_name(test_config_doc, mainline_package_name): 80 81 test_config = parse_test_config(test_config_doc) 82 83 for obj in get_children_with_tag(test_config, 'object'): 84 if obj.getAttribute('class') == MAINLINE_CONTROLLER: 85 for option in get_children_with_tag(obj, 'option'): 86 if option.getAttribute('name') == "mainline-module-package-name": 87 option.setAttribute('value', mainline_package_name) 88 89def main(): 90 """Program entry point.""" 91 try: 92 args = parse_args() 93 94 doc = minidom.parse(args.input) 95 96 if args.package_name: 97 if not args.manifest: 98 raise RuntimeError('--manifest flag required for --package-name') 99 manifest_doc = minidom.parse(args.manifest) 100 overwrite_package_name(doc, manifest_doc, args.package_name) 101 102 if args.test_file_name: 103 overwrite_test_file_name(doc, args.test_file_name) 104 105 if args.mainline_package_name: 106 overwrite_mainline_module_package_name(doc, args.mainline_package_name) 107 108 with open(args.output, 'w') as f: 109 write_xml(f, doc) 110 111 # pylint: disable=broad-except 112 except Exception as err: 113 print('error: ' + str(err), file=sys.stderr) 114 sys.exit(-1) 115 116if __name__ == '__main__': 117 main() 118