blob: 3dbc22ec211953b765276ee5df72595cc607b32d [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 Sabnisee1c1992022-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
34def parse_args():
35 """Parse commandline arguments."""
36
37 parser = argparse.ArgumentParser()
38 parser.add_argument('--manifest', default='', dest='manifest',
39 help=('AndroidManifest.xml that contains the original package name'))
40 parser.add_argument('--package-name', default='', dest='package_name',
41 help=('overwrite package fields in the test config'))
Jaewoong Jung39982342020-01-14 10:27:18 -080042 parser.add_argument('--test-file-name', default='', dest='test_file_name',
43 help=('overwrite test file name in the test config'))
Jaewoong Junge5cd4e12019-11-22 14:34:55 -080044 parser.add_argument('input', help='input test config file')
45 parser.add_argument('output', help='output test config file')
46 return parser.parse_args()
47
48
49def overwrite_package_name(test_config_doc, manifest_doc, package_name):
50
51 manifest = parse_manifest(manifest_doc)
52 original_package = manifest.getAttribute('package')
Jaewoong Junge5cd4e12019-11-22 14:34:55 -080053
54 test_config = parse_test_config(test_config_doc)
55 tests = get_children_with_tag(test_config, 'test')
56
57 for test in tests:
58 options = get_children_with_tag(test, 'option')
59 for option in options:
60 if option.getAttribute('name') == "package" and option.getAttribute('value') == original_package:
61 option.setAttribute('value', package_name)
62
Jaewoong Jung39982342020-01-14 10:27:18 -080063def overwrite_test_file_name(test_config_doc, test_file_name):
64
65 test_config = parse_test_config(test_config_doc)
66 tests = get_children_with_tag(test_config, 'target_preparer')
67
68 for test in tests:
Rahul Sabnisee1c1992022-04-19 18:00:10 -070069 if test.getAttribute('class') in KNOWN_PREPARERS:
Jaewoong Jung39982342020-01-14 10:27:18 -080070 options = get_children_with_tag(test, 'option')
71 for option in options:
72 if option.getAttribute('name') == "test-file-name":
73 option.setAttribute('value', test_file_name)
74
Jaewoong Junge5cd4e12019-11-22 14:34:55 -080075def main():
76 """Program entry point."""
77 try:
78 args = parse_args()
79
80 doc = minidom.parse(args.input)
81
82 if args.package_name:
83 if not args.manifest:
84 raise RuntimeError('--manifest flag required for --package-name')
85 manifest_doc = minidom.parse(args.manifest)
86 overwrite_package_name(doc, manifest_doc, args.package_name)
87
Jaewoong Jung39982342020-01-14 10:27:18 -080088 if args.test_file_name:
89 overwrite_test_file_name(doc, args.test_file_name)
90
Cole Faustc41dd722021-11-09 15:08:26 -080091 with open(args.output, 'w') as f:
Jaewoong Junge5cd4e12019-11-22 14:34:55 -080092 write_xml(f, doc)
93
94 # pylint: disable=broad-except
95 except Exception as err:
96 print('error: ' + str(err), file=sys.stderr)
97 sys.exit(-1)
98
99if __name__ == '__main__':
100 main()