1# Copyright (c) 2014 The WebM project authors. All Rights Reserved.
2#
3# Use of this source code is governed by a BSD-style license
4# that can be found in the LICENSE file in the root of the source
5# tree. An additional intellectual property rights grant can be found
6# in the file PATENTS.  All contributing project authors may
7# be found in the AUTHORS file in the root of the source tree.
8
9"""Standalone script which parses a gtest log for json.
10
11Json is returned returns as an array.  This script is used by the libvpx
12waterfall to gather json results mixed in with gtest logs.  This is
13dubious software engineering.
14"""
15
16import getopt
17import json
18import os
19import re
20import sys
21
22
23def main():
24  if len(sys.argv) != 3:
25    print "Expects a file to write json to!"
26    exit(1)
27
28  try:
29    opts, _ = \
30        getopt.getopt(sys.argv[1:], \
31                      'o:', ['output-json='])
32  except getopt.GetOptError:
33    print 'scrape_gtest_log.py -o <output_json>'
34    sys.exit(2)
35
36  output_json = ''
37  for opt, arg in opts:
38    if opt in ('-o', '--output-json'):
39      output_json = os.path.join(arg)
40
41  blob = sys.stdin.read()
42  json_string = '[' + ','.join('{' + x + '}' for x in
43                               re.findall(r'{([^}]*.?)}', blob)) + ']'
44  print blob
45
46  output = json.dumps(json.loads(json_string), indent=4, sort_keys=True)
47  print output
48
49  path = os.path.dirname(output_json)
50  if path and not os.path.exists(path):
51    os.makedirs(path)
52
53  outfile = open(output_json, 'w')
54  outfile.write(output)
55
56if __name__ == '__main__':
57  sys.exit(main())
58