blob: 7bb4b52a32b884ae10e26238b5aea565cb515910 [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'))
40 parser.add_argument('input', help='input test config file')
41 parser.add_argument('output', help='output test config file')
42 return parser.parse_args()
43
44
45def overwrite_package_name(test_config_doc, manifest_doc, package_name):
46
47 manifest = parse_manifest(manifest_doc)
48 original_package = manifest.getAttribute('package')
49 print('package: ' + original_package)
50
51 test_config = parse_test_config(test_config_doc)
52 tests = get_children_with_tag(test_config, 'test')
53
54 for test in tests:
55 options = get_children_with_tag(test, 'option')
56 for option in options:
57 if option.getAttribute('name') == "package" and option.getAttribute('value') == original_package:
58 option.setAttribute('value', package_name)
59
60def main():
61 """Program entry point."""
62 try:
63 args = parse_args()
64
65 doc = minidom.parse(args.input)
66
67 if args.package_name:
68 if not args.manifest:
69 raise RuntimeError('--manifest flag required for --package-name')
70 manifest_doc = minidom.parse(args.manifest)
71 overwrite_package_name(doc, manifest_doc, args.package_name)
72
73 with open(args.output, 'wb') as f:
74 write_xml(f, doc)
75
76 # pylint: disable=broad-except
77 except Exception as err:
78 print('error: ' + str(err), file=sys.stderr)
79 sys.exit(-1)
80
81if __name__ == '__main__':
82 main()