blob: 07e01a14a9652402312627f848dcd33dfa2994e6 [file] [log] [blame]
Jaewoong Junge5cd4e12019-11-22 14:34:55 -08001#!/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
Rahul Sabnis48a8f0d2022-04-19 18:00:10 -070031KNOWN_PREPARERS = ['com.android.tradefed.targetprep.TestAppInstallSetup',
32 'com.android.tradefed.targetprep.suite.SuiteApkInstaller']
Jaewoong Junge5cd4e12019-11-22 14:34:55 -080033
Seth Moorec6f4b532023-02-02 13:22:26 -080034MAINLINE_CONTROLLER = 'com.android.tradefed.testtype.suite.module.MainlineTestModuleController'
35
Jaewoong Junge5cd4e12019-11-22 14:34:55 -080036def 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 Jung39982342020-01-14 10:27:18 -080044 parser.add_argument('--test-file-name', default='', dest='test_file_name',
45 help=('overwrite test file name in the test config'))
Seth Moorec6f4b532023-02-02 13:22:26 -080046 parser.add_argument('--mainline-package-name', default='', dest='mainline_package_name',
47 help=('overwrite mainline module package name in the test config'))
Jaewoong Junge5cd4e12019-11-22 14:34:55 -080048 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')
Jaewoong Junge5cd4e12019-11-22 14:34:55 -080057
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 Jung39982342020-01-14 10:27:18 -080067def 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 Sabnis48a8f0d2022-04-19 18:00:10 -070073 if test.getAttribute('class') in KNOWN_PREPARERS:
Jaewoong Jung39982342020-01-14 10:27:18 -080074 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 Moorec6f4b532023-02-02 13:22:26 -080079def 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 Junge5cd4e12019-11-22 14:34:55 -080089def 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 Jung39982342020-01-14 10:27:18 -0800102 if args.test_file_name:
103 overwrite_test_file_name(doc, args.test_file_name)
104
Seth Moorec6f4b532023-02-02 13:22:26 -0800105 if args.mainline_package_name:
106 overwrite_mainline_module_package_name(doc, args.mainline_package_name)
107
Cole Faustc41dd722021-11-09 15:08:26 -0800108 with open(args.output, 'w') as f:
Jaewoong Junge5cd4e12019-11-22 14:34:55 -0800109 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()