blob: c150e8cfd8484a17ca54298db9e21f07858487b9 [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
31
32def parse_args():
33 """Parse commandline arguments."""
34
35 parser = argparse.ArgumentParser()
36 parser.add_argument('--manifest', default='', dest='manifest',
37 help=('AndroidManifest.xml that contains the original package name'))
38 parser.add_argument('--package-name', default='', dest='package_name',
39 help=('overwrite package fields in the test config'))
Jaewoong Jung39982342020-01-14 10:27:18 -080040 parser.add_argument('--test-file-name', default='', dest='test_file_name',
41 help=('overwrite test file name in the test config'))
Jaewoong Junge5cd4e12019-11-22 14:34:55 -080042 parser.add_argument('input', help='input test config file')
43 parser.add_argument('output', help='output test config file')
44 return parser.parse_args()
45
46
47def overwrite_package_name(test_config_doc, manifest_doc, package_name):
48
49 manifest = parse_manifest(manifest_doc)
50 original_package = manifest.getAttribute('package')
Jaewoong Junge5cd4e12019-11-22 14:34:55 -080051
52 test_config = parse_test_config(test_config_doc)
53 tests = get_children_with_tag(test_config, 'test')
54
55 for test in tests:
56 options = get_children_with_tag(test, 'option')
57 for option in options:
58 if option.getAttribute('name') == "package" and option.getAttribute('value') == original_package:
59 option.setAttribute('value', package_name)
60
Jaewoong Jung39982342020-01-14 10:27:18 -080061def overwrite_test_file_name(test_config_doc, test_file_name):
62
63 test_config = parse_test_config(test_config_doc)
64 tests = get_children_with_tag(test_config, 'target_preparer')
65
66 for test in tests:
67 if test.getAttribute('class') == "com.android.tradefed.targetprep.TestAppInstallSetup":
68 options = get_children_with_tag(test, 'option')
69 for option in options:
70 if option.getAttribute('name') == "test-file-name":
71 option.setAttribute('value', test_file_name)
72
Jaewoong Junge5cd4e12019-11-22 14:34:55 -080073def main():
74 """Program entry point."""
75 try:
76 args = parse_args()
77
78 doc = minidom.parse(args.input)
79
80 if args.package_name:
81 if not args.manifest:
82 raise RuntimeError('--manifest flag required for --package-name')
83 manifest_doc = minidom.parse(args.manifest)
84 overwrite_package_name(doc, manifest_doc, args.package_name)
85
Jaewoong Jung39982342020-01-14 10:27:18 -080086 if args.test_file_name:
87 overwrite_test_file_name(doc, args.test_file_name)
88
Cole Faustc41dd722021-11-09 15:08:26 -080089 with open(args.output, 'w') as f:
Jaewoong Junge5cd4e12019-11-22 14:34:55 -080090 write_xml(f, doc)
91
92 # pylint: disable=broad-except
93 except Exception as err:
94 print('error: ' + str(err), file=sys.stderr)
95 sys.exit(-1)
96
97if __name__ == '__main__':
98 main()