Imported Upstream version 1.36.0
[platform/upstream/grpc.git] / tools / line_count / yaml2csv.py
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
16 import yaml
17 import argparse
18 import datetime
19 import csv
20
21 argp = argparse.ArgumentParser(description='Convert cloc yaml to bigquery csv')
22 argp.add_argument('-i', '--input', type=str)
23 argp.add_argument('-d',
24                   '--date',
25                   type=str,
26                   default=datetime.date.today().strftime('%Y-%m-%d'))
27 argp.add_argument('-o', '--output', type=str, default='out.csv')
28 args = argp.parse_args()
29
30 data = yaml.load(open(args.input).read())
31 with 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':
36             continue
37         if key == 'SUM':
38             continue
39         if key.startswith('third_party/'):
40             continue
41         row = {'name': key, 'date': args.date}
42         row.update(value)
43         writer.writerow(row)