Cole Faust | e03153b | 2023-01-26 17:29:13 -0800 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
Dan Shi | efb892d | 2017-12-06 15:57:31 -0800 | [diff] [blame] | 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 | |
| 20 | import os |
| 21 | import shutil |
| 22 | import sys |
| 23 | from xml.dom.minidom import parse |
| 24 | |
| 25 | ATTRIBUTE_LABEL = 'android:label' |
| 26 | ATTRIBUTE_RUNNER = 'android:name' |
Dan Shi | 9a50168 | 2017-12-22 13:32:48 -0800 | [diff] [blame] | 27 | ATTRIBUTE_PACKAGE = 'package' |
Dan Shi | efb892d | 2017-12-06 15:57:31 -0800 | [diff] [blame] | 28 | |
| 29 | PLACEHOLDER_LABEL = '{LABEL}' |
easoncylee | 9425870 | 2020-04-30 15:01:30 +0800 | [diff] [blame] | 30 | PLACEHOLDER_EXTRA_CONFIGS = '{EXTRA_CONFIGS}' |
Dan Shi | efb892d | 2017-12-06 15:57:31 -0800 | [diff] [blame] | 31 | PLACEHOLDER_MODULE = '{MODULE}' |
| 32 | PLACEHOLDER_PACKAGE = '{PACKAGE}' |
| 33 | PLACEHOLDER_RUNNER = '{RUNNER}' |
| 34 | PLACEHOLDER_TEST_TYPE = '{TEST_TYPE}' |
| 35 | |
| 36 | |
| 37 | def main(argv): |
| 38 | """Entry point of auto_gen_test_config. |
| 39 | |
| 40 | Args: |
| 41 | argv: A list of arguments. |
| 42 | Returns: |
| 43 | 0 if no error, otherwise 1. |
| 44 | """ |
easoncylee | 9425870 | 2020-04-30 15:01:30 +0800 | [diff] [blame] | 45 | if len(argv) != 4 and len(argv) != 6: |
Dan Shi | efb892d | 2017-12-06 15:57:31 -0800 | [diff] [blame] | 46 | sys.stderr.write( |
easoncylee | 9425870 | 2020-04-30 15:01:30 +0800 | [diff] [blame] | 47 | 'Invalid arguments. The script requires 4 arguments for file paths: ' |
Dan Shi | efb892d | 2017-12-06 15:57:31 -0800 | [diff] [blame] | 48 | 'target_config android_manifest empty_config ' |
easoncylee | 9425870 | 2020-04-30 15:01:30 +0800 | [diff] [blame] | 49 | 'instrumentation_test_config_template ' |
| 50 | 'and 2 optional arguments for extra configs: ' |
| 51 | '--extra-configs \'EXTRA_CONFIGS\'.\n') |
Dan Shi | efb892d | 2017-12-06 15:57:31 -0800 | [diff] [blame] | 52 | return 1 |
easoncylee | 9425870 | 2020-04-30 15:01:30 +0800 | [diff] [blame] | 53 | |
Dan Shi | efb892d | 2017-12-06 15:57:31 -0800 | [diff] [blame] | 54 | target_config = argv[0] |
| 55 | android_manifest = argv[1] |
| 56 | empty_config = argv[2] |
| 57 | instrumentation_test_config_template = argv[3] |
easoncylee | 9425870 | 2020-04-30 15:01:30 +0800 | [diff] [blame] | 58 | extra_configs = '\n'.join(argv[5].split('\\n')) if len(argv) == 6 else '' |
Dan Shi | efb892d | 2017-12-06 15:57:31 -0800 | [diff] [blame] | 59 | |
| 60 | manifest = parse(android_manifest) |
| 61 | instrumentation_elements = manifest.getElementsByTagName('instrumentation') |
Dan Shi | 9a50168 | 2017-12-22 13:32:48 -0800 | [diff] [blame] | 62 | manifest_elements = manifest.getElementsByTagName('manifest') |
| 63 | if len(instrumentation_elements) != 1 or len(manifest_elements) != 1: |
| 64 | # Failed to locate instrumentation or manifest element in AndroidManifest. |
| 65 | # file. Empty test config file will be created. |
Dan Shi | efb892d | 2017-12-06 15:57:31 -0800 | [diff] [blame] | 66 | shutil.copyfile(empty_config, target_config) |
| 67 | return 0 |
| 68 | |
| 69 | module = os.path.splitext(os.path.basename(target_config))[0] |
| 70 | instrumentation = instrumentation_elements[0] |
Dan Shi | 9a50168 | 2017-12-22 13:32:48 -0800 | [diff] [blame] | 71 | manifest = manifest_elements[0] |
Cole Faust | e03153b | 2023-01-26 17:29:13 -0800 | [diff] [blame^] | 72 | if ATTRIBUTE_LABEL in instrumentation.attributes: |
Dan Shi | efb892d | 2017-12-06 15:57:31 -0800 | [diff] [blame] | 73 | label = instrumentation.attributes[ATTRIBUTE_LABEL].value |
| 74 | else: |
| 75 | label = module |
| 76 | runner = instrumentation.attributes[ATTRIBUTE_RUNNER].value |
Dan Shi | 9a50168 | 2017-12-22 13:32:48 -0800 | [diff] [blame] | 77 | package = manifest.attributes[ATTRIBUTE_PACKAGE].value |
Dan Shi | 96068b7 | 2018-02-21 11:31:06 -0800 | [diff] [blame] | 78 | test_type = ('InstrumentationTest' |
| 79 | if runner.endswith('.InstrumentationTestRunner') |
| 80 | else 'AndroidJUnitTest') |
Dan Shi | efb892d | 2017-12-06 15:57:31 -0800 | [diff] [blame] | 81 | |
| 82 | with open(instrumentation_test_config_template) as template: |
| 83 | config = template.read() |
| 84 | config = config.replace(PLACEHOLDER_LABEL, label) |
| 85 | config = config.replace(PLACEHOLDER_MODULE, module) |
| 86 | config = config.replace(PLACEHOLDER_PACKAGE, package) |
| 87 | config = config.replace(PLACEHOLDER_TEST_TYPE, test_type) |
easoncylee | 9425870 | 2020-04-30 15:01:30 +0800 | [diff] [blame] | 88 | config = config.replace(PLACEHOLDER_EXTRA_CONFIGS, extra_configs) |
Dan Shi | efb892d | 2017-12-06 15:57:31 -0800 | [diff] [blame] | 89 | config = config.replace(PLACEHOLDER_RUNNER, runner) |
| 90 | with open(target_config, 'w') as config_file: |
| 91 | config_file.write(config) |
| 92 | return 0 |
| 93 | |
| 94 | if __name__ == '__main__': |
| 95 | sys.exit(main(sys.argv[1:])) |