blob: fa018f4579cdd8ff657517a43df4fb64ac3de357 [file] [log] [blame]
Dan Shiefb892d2017-12-06 15:57:31 -08001#!/usr/bin/env python
2#
3# Copyright (C) 2017 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 to generate TradeFed test config file.
18"""
19
20import os
21import shutil
22import sys
23from xml.dom.minidom import parse
24
25ATTRIBUTE_LABEL = 'android:label'
26ATTRIBUTE_RUNNER = 'android:name'
27ATTRIBUTE_TARGET_PACKAGE = 'android:targetPackage'
28
29PLACEHOLDER_LABEL = '{LABEL}'
30PLACEHOLDER_MODULE = '{MODULE}'
31PLACEHOLDER_PACKAGE = '{PACKAGE}'
32PLACEHOLDER_RUNNER = '{RUNNER}'
33PLACEHOLDER_TEST_TYPE = '{TEST_TYPE}'
34
35
36def main(argv):
37 """Entry point of auto_gen_test_config.
38
39 Args:
40 argv: A list of arguments.
41 Returns:
42 0 if no error, otherwise 1.
43 """
44 if len(argv) != 4:
45 sys.stderr.write(
46 'Invalid arguements. The script requires 4 arguments for file paths: '
47 'target_config android_manifest empty_config '
48 'instrumentation_test_config_template.\n')
49 return 1
50 target_config = argv[0]
51 android_manifest = argv[1]
52 empty_config = argv[2]
53 instrumentation_test_config_template = argv[3]
54
55 manifest = parse(android_manifest)
56 instrumentation_elements = manifest.getElementsByTagName('instrumentation')
57 if len(instrumentation_elements) != 1:
58 # Failed to locate instrumentation element in AndroidManifest file.
59 # Empty test config file will be created.
60 shutil.copyfile(empty_config, target_config)
61 return 0
62
63 module = os.path.splitext(os.path.basename(target_config))[0]
64 instrumentation = instrumentation_elements[0]
65 if instrumentation.attributes.has_key(ATTRIBUTE_LABEL):
66 label = instrumentation.attributes[ATTRIBUTE_LABEL].value
67 else:
68 label = module
69 runner = instrumentation.attributes[ATTRIBUTE_RUNNER].value
70 package = instrumentation.attributes[ATTRIBUTE_TARGET_PACKAGE].value
71 test_type = ('AndroidJUnitTest' if runner.endswith('.AndroidJUnitRunner')
72 else 'InstrumentationTest')
73
74 with open(instrumentation_test_config_template) as template:
75 config = template.read()
76 config = config.replace(PLACEHOLDER_LABEL, label)
77 config = config.replace(PLACEHOLDER_MODULE, module)
78 config = config.replace(PLACEHOLDER_PACKAGE, package)
79 config = config.replace(PLACEHOLDER_TEST_TYPE, test_type)
80 config = config.replace(PLACEHOLDER_RUNNER, runner)
81 with open(target_config, 'w') as config_file:
82 config_file.write(config)
83 return 0
84
85if __name__ == '__main__':
86 sys.exit(main(sys.argv[1:]))