aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/collect_yaml.py
blob: 3147a0ca8d0c52d1faf076f8c2201f92e6840982 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
#
# Copyright (c) 2017 Weitian LI <liweitianux@live.com>
# MIT license
#
# Weitian LI
# 2017-02-12

"""
Collect YAML manifest files, and convert collected results to CSV
format for later use.
"""

import sys
import argparse
import csv

from _context import acispy
from acispy.manifest import Manifest


def main():
    parser = argparse.ArgumentParser(description="Collect YAML manifest files")
    parser.add_argument("-k", "--keys", dest="keys", required=True,
                        help="YAML keys to be collected (in order); " +
                        "can be comma-separated string, or a file " +
                        "containing the keys one-per-line")
    parser.add_argument("-b", "--brief", dest="brief",
                        action="store_true",
                        help="be brief and do not print header")
    parser.add_argument("-v", "--verbose", dest="verbose",
                        action="store_true",
                        help="show verbose information")
    parser.add_argument("-o", "--outfile", dest="outfile", default=sys.stdout,
                        help="output CSV file to save collected data")
    parser.add_argument("-i", "--infile", dest="infile",
                        nargs="+", required=True,
                        help="list of input YAML manifest files")
    args = parser.parse_args()

    try:
        keys = [k.strip() for k in open(args.keys).readlines()]
    except FileNotFoundError:
        keys = [k.strip() for k in args.keys.split(",")]

    if args.verbose:
        print("keys:", keys, file=sys.stderr)
        print("infile:", args.infile, file=sys.stderr)
        print("outfile:", args.outfile, file=sys.stderr)

    results = []
    for fp in args.infile:
        manifest = Manifest(fp)
        res = manifest.gets(keys, splitlist=True)
        if args.verbose:
            print("FILE:{0}: {1}".format(fp, list(res.values())),
                  file=sys.stderr)
        results.append(res)

    try:
        of = open(args.outfile, "w")
    except TypeError:
        of = args.outfile
    writer = csv.writer(of)
    if not args.brief:
        writer.writerow(results[0].keys())
    for res in results:
        writer.writerow(res.values())
    if of is not sys.stdout:
        of.close()


if __name__ == "__main__":
    main()