Jaewoong Jung | e5cd4e1 | 2019-11-22 14:34:55 -0800 | [diff] [blame] | 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 | |
| 19 | from __future__ import print_function |
| 20 | |
| 21 | import argparse |
| 22 | import sys |
| 23 | from xml.dom import minidom |
| 24 | |
| 25 | |
| 26 | from manifest import get_children_with_tag |
| 27 | from manifest import parse_manifest |
| 28 | from manifest import parse_test_config |
| 29 | from manifest import write_xml |
| 30 | |
Rahul Sabnis | 48a8f0d | 2022-04-19 18:00:10 -0700 | [diff] [blame] | 31 | KNOWN_PREPARERS = ['com.android.tradefed.targetprep.TestAppInstallSetup', |
| 32 | 'com.android.tradefed.targetprep.suite.SuiteApkInstaller'] |
Jaewoong Jung | e5cd4e1 | 2019-11-22 14:34:55 -0800 | [diff] [blame] | 33 | |
Seth Moore | c6f4b53 | 2023-02-02 13:22:26 -0800 | [diff] [blame^] | 34 | MAINLINE_CONTROLLER = 'com.android.tradefed.testtype.suite.module.MainlineTestModuleController' |
| 35 | |
Jaewoong Jung | e5cd4e1 | 2019-11-22 14:34:55 -0800 | [diff] [blame] | 36 | def 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')) |
Jaewoong Jung | 3998234 | 2020-01-14 10:27:18 -0800 | [diff] [blame] | 44 | parser.add_argument('--test-file-name', default='', dest='test_file_name', |
| 45 | help=('overwrite test file name in the test config')) |
Seth Moore | c6f4b53 | 2023-02-02 13:22:26 -0800 | [diff] [blame^] | 46 | parser.add_argument('--mainline-package-name', default='', dest='mainline_package_name', |
| 47 | help=('overwrite mainline module package name in the test config')) |
Jaewoong Jung | e5cd4e1 | 2019-11-22 14:34:55 -0800 | [diff] [blame] | 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 | |
| 53 | def overwrite_package_name(test_config_doc, manifest_doc, package_name): |
| 54 | |
| 55 | manifest = parse_manifest(manifest_doc) |
| 56 | original_package = manifest.getAttribute('package') |
Jaewoong Jung | e5cd4e1 | 2019-11-22 14:34:55 -0800 | [diff] [blame] | 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 | |
Jaewoong Jung | 3998234 | 2020-01-14 10:27:18 -0800 | [diff] [blame] | 67 | def 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: |
Rahul Sabnis | 48a8f0d | 2022-04-19 18:00:10 -0700 | [diff] [blame] | 73 | if test.getAttribute('class') in KNOWN_PREPARERS: |
Jaewoong Jung | 3998234 | 2020-01-14 10:27:18 -0800 | [diff] [blame] | 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 | |
Seth Moore | c6f4b53 | 2023-02-02 13:22:26 -0800 | [diff] [blame^] | 79 | def 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 | |
Jaewoong Jung | e5cd4e1 | 2019-11-22 14:34:55 -0800 | [diff] [blame] | 89 | def 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 | |
Jaewoong Jung | 3998234 | 2020-01-14 10:27:18 -0800 | [diff] [blame] | 102 | if args.test_file_name: |
| 103 | overwrite_test_file_name(doc, args.test_file_name) |
| 104 | |
Seth Moore | c6f4b53 | 2023-02-02 13:22:26 -0800 | [diff] [blame^] | 105 | if args.mainline_package_name: |
| 106 | overwrite_mainline_module_package_name(doc, args.mainline_package_name) |
| 107 | |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 108 | with open(args.output, 'w') as f: |
Jaewoong Jung | e5cd4e1 | 2019-11-22 14:34:55 -0800 | [diff] [blame] | 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 | |
| 116 | if __name__ == '__main__': |
| 117 | main() |