test_config_fixer.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/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. from __future__ import print_function
  19. import argparse
  20. import sys
  21. from xml.dom import minidom
  22. from manifest import get_children_with_tag
  23. from manifest import parse_manifest
  24. from manifest import parse_test_config
  25. from manifest import write_xml
  26. KNOWN_PREPARERS = ['com.android.tradefed.targetprep.TestAppInstallSetup',
  27. 'com.android.tradefed.targetprep.suite.SuiteApkInstaller']
  28. MAINLINE_CONTROLLER = 'com.android.tradefed.testtype.suite.module.MainlineTestModuleController'
  29. def parse_args():
  30. """Parse commandline arguments."""
  31. parser = argparse.ArgumentParser()
  32. parser.add_argument('--manifest', default='', dest='manifest',
  33. help=('AndroidManifest.xml that contains the original package name'))
  34. parser.add_argument('--package-name', default='', dest='package_name',
  35. help=('overwrite package fields in the test config'))
  36. parser.add_argument('--test-file-name', default='', dest='test_file_name',
  37. help=('overwrite test file name in the test config'))
  38. parser.add_argument('--mainline-package-name', default='', dest='mainline_package_name',
  39. help=('overwrite mainline module package name 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. def overwrite_package_name(test_config_doc, manifest_doc, package_name):
  44. manifest = parse_manifest(manifest_doc)
  45. original_package = manifest.getAttribute('package')
  46. test_config = parse_test_config(test_config_doc)
  47. tests = get_children_with_tag(test_config, 'test')
  48. for test in tests:
  49. options = get_children_with_tag(test, 'option')
  50. for option in options:
  51. if option.getAttribute('name') == "package" and option.getAttribute('value') == original_package:
  52. option.setAttribute('value', package_name)
  53. def overwrite_test_file_name(test_config_doc, test_file_name):
  54. test_config = parse_test_config(test_config_doc)
  55. tests = get_children_with_tag(test_config, 'target_preparer')
  56. for test in tests:
  57. if test.getAttribute('class') in KNOWN_PREPARERS:
  58. options = get_children_with_tag(test, 'option')
  59. for option in options:
  60. if option.getAttribute('name') == "test-file-name":
  61. option.setAttribute('value', test_file_name)
  62. def overwrite_mainline_module_package_name(test_config_doc, mainline_package_name):
  63. test_config = parse_test_config(test_config_doc)
  64. for obj in get_children_with_tag(test_config, 'object'):
  65. if obj.getAttribute('class') == MAINLINE_CONTROLLER:
  66. for option in get_children_with_tag(obj, 'option'):
  67. if option.getAttribute('name') == "mainline-module-package-name":
  68. option.setAttribute('value', mainline_package_name)
  69. def main():
  70. """Program entry point."""
  71. try:
  72. args = parse_args()
  73. doc = minidom.parse(args.input)
  74. if args.package_name:
  75. if not args.manifest:
  76. raise RuntimeError('--manifest flag required for --package-name')
  77. manifest_doc = minidom.parse(args.manifest)
  78. overwrite_package_name(doc, manifest_doc, args.package_name)
  79. if args.test_file_name:
  80. overwrite_test_file_name(doc, args.test_file_name)
  81. if args.mainline_package_name:
  82. overwrite_mainline_module_package_name(doc, args.mainline_package_name)
  83. with open(args.output, 'w') as f:
  84. write_xml(f, doc)
  85. # pylint: disable=broad-except
  86. except Exception as err:
  87. print('error: ' + str(err), file=sys.stderr)
  88. sys.exit(-1)
  89. if __name__ == '__main__':
  90. main()