• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/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_PACKAGE = 'package'
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  manifest_elements = manifest.getElementsByTagName('manifest')
58  if len(instrumentation_elements) != 1 or len(manifest_elements) != 1:
59    # Failed to locate instrumentation or manifest element in AndroidManifest.
60    # file. Empty test config file will be created.
61    shutil.copyfile(empty_config, target_config)
62    return 0
63
64  module = os.path.splitext(os.path.basename(target_config))[0]
65  instrumentation = instrumentation_elements[0]
66  manifest = manifest_elements[0]
67  if instrumentation.attributes.has_key(ATTRIBUTE_LABEL):
68    label = instrumentation.attributes[ATTRIBUTE_LABEL].value
69  else:
70    label = module
71  runner = instrumentation.attributes[ATTRIBUTE_RUNNER].value
72  package = manifest.attributes[ATTRIBUTE_PACKAGE].value
73  test_type = ('InstrumentationTest'
74               if runner.endswith('.InstrumentationTestRunner')
75               else 'AndroidJUnitTest')
76
77  with open(instrumentation_test_config_template) as template:
78    config = template.read()
79    config = config.replace(PLACEHOLDER_LABEL, label)
80    config = config.replace(PLACEHOLDER_MODULE, module)
81    config = config.replace(PLACEHOLDER_PACKAGE, package)
82    config = config.replace(PLACEHOLDER_TEST_TYPE, test_type)
83    config = config.replace(PLACEHOLDER_RUNNER, runner)
84    with open(target_config, 'w') as config_file:
85      config_file.write(config)
86  return 0
87
88if __name__ == '__main__':
89  sys.exit(main(sys.argv[1:]))
90