OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Scans the Chromium source of UseCounter, formats the Feature enum for | 6 """Scans the Chromium source of UseCounter, formats the Feature enum for |
7 histograms.xml and merges it. This script can also generate a python code | 7 histograms.xml and merges it. This script can also generate a python code |
8 snippet to put in uma.py of Chromium Dashboard. Make sure that you review the | 8 snippet to put in uma.py of Chromium Dashboard. Make sure that you review the |
9 output for correctness. | 9 output for correctness. |
10 """ | 10 """ |
11 | 11 |
12 import optparse | 12 import optparse |
13 import os | 13 import os |
14 import re | |
14 import sys | 15 import sys |
15 | 16 |
16 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common')) | 17 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common')) |
17 from update_histogram_enum import ReadHistogramValues | 18 from update_histogram_enum import ReadHistogramValues |
18 from update_histogram_enum import UpdateHistogramEnum | 19 from update_histogram_enum import UpdateHistogramFromDict |
20 | |
21 | |
22 USE_COUNTER_CPP_PATH = \ | |
23 '../../../third_party/WebKit/Source/core/frame/UseCounter.cpp' | |
19 | 24 |
20 | 25 |
21 def print_enum_for_dashboard(enum_dict): | 26 def print_enum_for_dashboard(enum_dict): |
Mike Lawther (Google)
2014/05/08 07:29:50
I wasn't sure where to factor this function to. Su
Mike Lawther (Google)
2014/05/09 01:11:03
I ended up just pulling it from the original locat
| |
22 """Prints enum_items formatted for use in uma.py of Chromium dashboard.""" | 27 """Prints enum_items formatted for use in uma.py of Chromium dashboard.""" |
23 for key in sorted(enum_dict.iterkeys()): | 28 for key in sorted(enum_dict.iterkeys()): |
24 print ' %d: \'%s\',' % (key, enum_dict[key]) | 29 print ' %d: \'%s\',' % (key, enum_dict[key]) |
25 | 30 |
tyoshino (SeeGerritForStatus)
2014/05/08 08:06:53
two vertical spaces plz
http://google-styleguide.
Mike Lawther (Google)
2014/05/09 01:11:03
Done.
It looks like the presubmit picks up some
| |
31 def enum_to_css(enum_name): | |
tyoshino (SeeGerritForStatus)
2014/05/08 08:06:53
enum_to_css_property
Mike Lawther (Google)
2014/05/09 01:11:03
Done.
| |
32 """Converts a camel cased enum name to the lower case CSS property""" | |
tyoshino (SeeGerritForStatus)
2014/05/08 08:06:53
period plz
Mike Lawther (Google)
2014/05/09 01:11:03
Done.
| |
33 return re.sub(r'([a-zA-Z])([A-Z])', r'\1-\2', enum_name).lower() | |
34 | |
tyoshino (SeeGerritForStatus)
2014/05/08 08:06:53
ditto
Mike Lawther (Google)
2014/05/09 01:11:03
Done.
| |
35 def read_css_properties(filename): | |
36 # Read the file as a list of lines | |
37 with open(filename) as f: | |
38 content = f.readlines() | |
39 | |
40 # Looking for a line like "case CSSPropertyGrid: return 453;" | |
41 bucket_finder = re.compile(r'.*CSSProperty(.*):\s*return\s*([0-9]+).*') | |
42 | |
43 bucket_dict = {} | |
44 for line in content: | |
45 bucket_match = bucket_finder.match(line) | |
46 if bucket_match: | |
47 css_property = bucket_match.group(1) | |
48 bucket = int(bucket_match.group(2)) | |
49 bucket_dict[bucket] = enum_to_css(css_property) | |
tyoshino (SeeGerritForStatus)
2014/05/08 08:06:53
this call looks weird.
how about s/css_property/e
Mike Lawther (Google)
2014/05/09 01:11:03
Done.
| |
50 | |
51 return bucket_dict | |
26 | 52 |
tyoshino (SeeGerritForStatus)
2014/05/08 08:06:53
ditto
Mike Lawther (Google)
2014/05/09 01:11:03
Done.
| |
27 if __name__ == '__main__': | 53 if __name__ == '__main__': |
28 parser = optparse.OptionParser() | 54 parser = optparse.OptionParser() |
Mike Lawther (Google)
2014/05/08 07:29:50
Is it worth factoring these lines out somewhere?
| |
29 parser.add_option('--for-dashboard', action='store_true', dest='dashboard', | 55 parser.add_option('--for-dashboard', action='store_true', dest='dashboard', |
30 default=False, | 56 default=False, |
31 help='Print enum definition formatted for use in uma.py of ' | 57 help='Print enum definition formatted for use in uma.py of ' |
32 'Chromium dashboard developed at ' | 58 'Chromium dashboard developed at ' |
33 'https://github.com/GoogleChrome/chromium-dashboard') | 59 'https://github.com/GoogleChrome/chromium-dashboard') |
34 options, args = parser.parse_args() | 60 options, args = parser.parse_args() |
35 | 61 |
36 source_path = os.path.join( | |
37 '..', '..', '..', | |
38 'third_party', 'WebKit', 'Source', 'core', 'frame', 'UseCounter.h') | |
39 | |
40 START_MARKER = '^enum Feature {' | |
41 END_MARKER = '^NumberOfFeatures' | |
42 | |
43 if options.dashboard: | 62 if options.dashboard: |
44 enum_dict = ReadHistogramValues(source_path, START_MARKER, END_MARKER) | 63 enum_dict = read_css_properties(USE_COUNTER_CPP_PATH) |
45 print_enum_for_dashboard(enum_dict) | 64 print_enum_for_dashboard(enum_dict) |
46 else: | 65 else: |
47 UpdateHistogramEnum( | 66 UpdateHistogramFromDict( |
48 histogram_enum_name='FeatureObserver', | 67 'MappedCSSProperties', read_css_properties(USE_COUNTER_CPP_PATH), |
tyoshino (SeeGerritForStatus)
2014/05/08 08:06:53
4 space indentation
Mike Lawther (Google)
2014/05/09 01:11:03
Done.
| |
49 source_enum_path=source_path, | 68 USE_COUNTER_CPP_PATH) |
50 start_marker=START_MARKER, | |
51 end_marker=END_MARKER) | |
OLD | NEW |