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 16# Helper script to concatenate YAML files. 17# 18# This script concatenates multiple YAML files into a single multipart file. 19# Input files are not parsed but processed as strings. This is a convenience 20# script to concatenate output files generated by multiple runs of 21# loadtest_config.py. 22 23import argparse 24import sys 25from typing import Iterable 26 27 28def gen_content_strings(input_files: Iterable[str]) -> Iterable[str]: 29 if not input_files: 30 return 31 32 with open(input_files[0]) as f: 33 content = f.read() 34 yield content 35 36 for input_file in input_files[1:]: 37 with open(input_file) as f: 38 content = f.read() 39 yield "---\n" 40 yield content 41 42 43def main() -> None: 44 argp = argparse.ArgumentParser(description="Concatenates YAML files.") 45 argp.add_argument( 46 "-i", 47 "--inputs", 48 action="extend", 49 nargs="+", 50 type=str, 51 required=True, 52 help="Input files.", 53 ) 54 argp.add_argument( 55 "-o", 56 "--output", 57 type=str, 58 help="Concatenated output file. Output to stdout if not set.", 59 ) 60 args = argp.parse_args() 61 62 with open(args.output, "w") if args.output else sys.stdout as f: 63 for content in gen_content_strings(args.inputs): 64 print(content, file=f, sep="", end="") 65 66 67if __name__ == "__main__": 68 main() 69