2017-12-07 07:57:31 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright (C) 2017 The Android Open Source Project
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
"""A tool to generate TradeFed test config file.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import sys
|
|
|
|
from xml.dom.minidom import parse
|
|
|
|
|
|
|
|
ATTRIBUTE_LABEL = 'android:label'
|
|
|
|
ATTRIBUTE_RUNNER = 'android:name'
|
2017-12-23 05:32:48 +08:00
|
|
|
ATTRIBUTE_PACKAGE = 'package'
|
2017-12-07 07:57:31 +08:00
|
|
|
|
|
|
|
PLACEHOLDER_LABEL = '{LABEL}'
|
2020-04-30 15:01:30 +08:00
|
|
|
PLACEHOLDER_EXTRA_CONFIGS = '{EXTRA_CONFIGS}'
|
2017-12-07 07:57:31 +08:00
|
|
|
PLACEHOLDER_MODULE = '{MODULE}'
|
|
|
|
PLACEHOLDER_PACKAGE = '{PACKAGE}'
|
|
|
|
PLACEHOLDER_RUNNER = '{RUNNER}'
|
|
|
|
PLACEHOLDER_TEST_TYPE = '{TEST_TYPE}'
|
|
|
|
|
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
"""Entry point of auto_gen_test_config.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
argv: A list of arguments.
|
|
|
|
Returns:
|
|
|
|
0 if no error, otherwise 1.
|
|
|
|
"""
|
2020-04-30 15:01:30 +08:00
|
|
|
if len(argv) != 4 and len(argv) != 6:
|
2017-12-07 07:57:31 +08:00
|
|
|
sys.stderr.write(
|
2020-04-30 15:01:30 +08:00
|
|
|
'Invalid arguments. The script requires 4 arguments for file paths: '
|
2017-12-07 07:57:31 +08:00
|
|
|
'target_config android_manifest empty_config '
|
2020-04-30 15:01:30 +08:00
|
|
|
'instrumentation_test_config_template '
|
|
|
|
'and 2 optional arguments for extra configs: '
|
|
|
|
'--extra-configs \'EXTRA_CONFIGS\'.\n')
|
2017-12-07 07:57:31 +08:00
|
|
|
return 1
|
2020-04-30 15:01:30 +08:00
|
|
|
|
2017-12-07 07:57:31 +08:00
|
|
|
target_config = argv[0]
|
|
|
|
android_manifest = argv[1]
|
|
|
|
empty_config = argv[2]
|
|
|
|
instrumentation_test_config_template = argv[3]
|
2020-04-30 15:01:30 +08:00
|
|
|
extra_configs = '\n'.join(argv[5].split('\\n')) if len(argv) == 6 else ''
|
2017-12-07 07:57:31 +08:00
|
|
|
|
|
|
|
manifest = parse(android_manifest)
|
|
|
|
instrumentation_elements = manifest.getElementsByTagName('instrumentation')
|
2017-12-23 05:32:48 +08:00
|
|
|
manifest_elements = manifest.getElementsByTagName('manifest')
|
|
|
|
if len(instrumentation_elements) != 1 or len(manifest_elements) != 1:
|
|
|
|
# Failed to locate instrumentation or manifest element in AndroidManifest.
|
|
|
|
# file. Empty test config file will be created.
|
2017-12-07 07:57:31 +08:00
|
|
|
shutil.copyfile(empty_config, target_config)
|
|
|
|
return 0
|
|
|
|
|
|
|
|
module = os.path.splitext(os.path.basename(target_config))[0]
|
|
|
|
instrumentation = instrumentation_elements[0]
|
2017-12-23 05:32:48 +08:00
|
|
|
manifest = manifest_elements[0]
|
2017-12-07 07:57:31 +08:00
|
|
|
if instrumentation.attributes.has_key(ATTRIBUTE_LABEL):
|
|
|
|
label = instrumentation.attributes[ATTRIBUTE_LABEL].value
|
|
|
|
else:
|
|
|
|
label = module
|
|
|
|
runner = instrumentation.attributes[ATTRIBUTE_RUNNER].value
|
2017-12-23 05:32:48 +08:00
|
|
|
package = manifest.attributes[ATTRIBUTE_PACKAGE].value
|
2018-02-22 03:31:06 +08:00
|
|
|
test_type = ('InstrumentationTest'
|
|
|
|
if runner.endswith('.InstrumentationTestRunner')
|
|
|
|
else 'AndroidJUnitTest')
|
2017-12-07 07:57:31 +08:00
|
|
|
|
|
|
|
with open(instrumentation_test_config_template) as template:
|
|
|
|
config = template.read()
|
|
|
|
config = config.replace(PLACEHOLDER_LABEL, label)
|
|
|
|
config = config.replace(PLACEHOLDER_MODULE, module)
|
|
|
|
config = config.replace(PLACEHOLDER_PACKAGE, package)
|
|
|
|
config = config.replace(PLACEHOLDER_TEST_TYPE, test_type)
|
2020-04-30 15:01:30 +08:00
|
|
|
config = config.replace(PLACEHOLDER_EXTRA_CONFIGS, extra_configs)
|
2017-12-07 07:57:31 +08:00
|
|
|
config = config.replace(PLACEHOLDER_RUNNER, runner)
|
|
|
|
with open(target_config, 'w') as config_file:
|
|
|
|
config_file.write(config)
|
|
|
|
return 0
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main(sys.argv[1:]))
|