loadtest_concat_yaml.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python3
  2. # Copyright 2021 The gRPC Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # Helper script to concatenate YAML files.
  16. #
  17. # This script concatenates multiple YAML files into a single multipart file.
  18. # Input files are not parsed but processed as strings. This is a convenience
  19. # script to concatenate output files generated by multiple runs of
  20. # loadtest_config.py.
  21. import argparse
  22. import sys
  23. from typing import Iterable
  24. def gen_content_strings(input_files: Iterable[str]) -> Iterable[str]:
  25. if not input_files:
  26. return
  27. with open(input_files[0]) as f:
  28. content = f.read()
  29. yield content
  30. for input_file in input_files[1:]:
  31. with open(input_file) as f:
  32. content = f.read()
  33. yield '---\n'
  34. yield content
  35. def main() -> None:
  36. argp = argparse.ArgumentParser(description='Concatenates YAML files.')
  37. argp.add_argument('-i',
  38. '--inputs',
  39. action='extend',
  40. nargs='+',
  41. type=str,
  42. required=True,
  43. help='Input files.')
  44. argp.add_argument(
  45. '-o',
  46. '--output',
  47. type=str,
  48. help='Concatenated output file. Output to stdout if not set.')
  49. args = argp.parse_args()
  50. with open(args.output, 'w') if args.output else sys.stdout as f:
  51. for content in gen_content_strings(args.inputs):
  52. print(content, file=f, sep='', end='')
  53. if __name__ == '__main__':
  54. main()