1#!/usr/bin/env python
2# Copyright 2017 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
16import yaml
17import argparse
18import datetime
19import csv
20
21argp = argparse.ArgumentParser(description='Convert cloc yaml to bigquery csv')
22argp.add_argument('-i', '--input', type=str)
23argp.add_argument('-d',
24                  '--date',
25                  type=str,
26                  default=datetime.date.today().strftime('%Y-%m-%d'))
27argp.add_argument('-o', '--output', type=str, default='out.csv')
28args = argp.parse_args()
29
30data = yaml.load(open(args.input).read())
31with open(args.output, 'w') as outf:
32    writer = csv.DictWriter(
33        outf, ['date', 'name', 'language', 'code', 'comment', 'blank'])
34    for key, value in data.iteritems():
35        if key == 'header': continue
36        if key == 'SUM': continue
37        if key.startswith('third_party/'): continue
38        row = {'name': key, 'date': args.date}
39        row.update(value)
40        writer.writerow(row)
41