Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / tools / metrics / histograms / update_net_error_codes.py
blob37e532375975d644b15fa7f20e98c89589c365a2
1 #!/usr/bin/env python
2 # Copyright 2014 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Updates NetErrorCodes enum in histograms.xml file with values read
7 from net_error_list.h.
9 If the file was pretty-printed, the updated version is pretty-printed too.
10 """
12 import os.path
13 import re
14 import sys
16 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common'))
17 import path_util
19 import update_histogram_enum
21 NET_ERROR_LIST_PATH = 'net/base/net_error_list.h'
23 POSITIVE_ERROR_REGEX = re.compile(r'^NET_ERROR\(([\w]+), -([0-9]+)\)')
24 NEGATIVE_ERROR_REGEX = re.compile(r'^NET_ERROR\(([\w]+), (-[0-9]+)\)')
26 def ReadNetErrorCodes(filename, error_regex):
27 """Reads in values from net_error_list.h, returning a dictionary mapping
28 error code to error name.
29 """
30 # Read the file as a list of lines
31 with open(path_util.GetInputFile(filename)) as f:
32 content = f.readlines()
34 # Parse out lines that are net errors.
35 errors = {}
36 for line in content:
37 m = error_regex.match(line)
38 if m:
39 errors[int(m.group(2))] = m.group(1)
40 return errors
43 def main():
44 if len(sys.argv) > 1:
45 print >>sys.stderr, 'No arguments expected!'
46 sys.stderr.write(__doc__)
47 sys.exit(1)
49 update_histogram_enum.UpdateHistogramFromDict(
50 'NetErrorCodes',
51 ReadNetErrorCodes(NET_ERROR_LIST_PATH, POSITIVE_ERROR_REGEX),
52 NET_ERROR_LIST_PATH)
54 update_histogram_enum.UpdateHistogramFromDict(
55 'CombinedHttpResponseAndNetErrorCode',
56 ReadNetErrorCodes(NET_ERROR_LIST_PATH, NEGATIVE_ERROR_REGEX),
57 NET_ERROR_LIST_PATH)
59 if __name__ == '__main__':
60 main()