• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
25
26from typing import Iterable
27
28
29def gen_content_strings(input_files: Iterable[str]) -> Iterable[str]:
30    if not input_files:
31        return
32
33    with open(input_files[0]) as f:
34        content = f.read()
35    yield content
36
37    for input_file in input_files[1:]:
38        with open(input_file) as f:
39            content = f.read()
40        yield '---\n'
41        yield content
42
43
44def main() -> None:
45    argp = argparse.ArgumentParser(description='Concatenates YAML files.')
46    argp.add_argument('-i',
47                      '--inputs',
48                      action='extend',
49                      nargs='+',
50                      type=str,
51                      required=True,
52                      help='Input files.')
53    argp.add_argument(
54        '-o',
55        '--output',
56        type=str,
57        help='Concatenated output file. Output to stdout if not set.')
58    args = argp.parse_args()
59
60    with open(args.output, 'w') if args.output else sys.stdout as f:
61        for content in gen_content_strings(args.inputs):
62            print(content, file=f, sep='', end='')
63
64
65if __name__ == '__main__':
66    main()
67