Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: tools/metrics/rappor/pretty_print.py

Issue 925753002: Add pretty printing for rappor.xml (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 # Copyright 2015 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.
5
6 import logging
7 import sys
8 import os
9
10 # Import the metrics/common module for pretty print xml.
11 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common'))
12 import models
13 import presubmit_util
14
15
16 # Model definitions for rappor.xml content
17 _SUMMARY_TYPE = models.TextNodeType('summary')
18
19 _PARAMETERS_TYPE = models.ObjectNodeType('parameters',
20 int_attributes=[
21 'num-cohorts',
22 'bytes',
23 'hash-functions',
24 ],
25 float_attributes=[
26 'fake-prob',
27 'fake-one-prob',
28 'one-coin-prob',
29 'zero-coin-prob',
30 ],
31 string_attributes=[
32 'reporting-level'
33 ])
34
35 _RAPPOR_PARAMETERS_TYPE = models.ObjectNodeType('rappor-parameters',
36 extra_newlines=(1, 1, 1),
37 string_attributes=['name'],
38 children=[
39 models.ChildType('summary', _SUMMARY_TYPE, False),
40 models.ChildType('parameters', _PARAMETERS_TYPE, False),
41 ])
42
43 _RAPPOR_PARAMETERS_TYPES_TYPE = models.ObjectNodeType('rappor-parameter-types',
44 extra_newlines=(1, 1, 1),
45 dont_indent=True,
46 children=[
47 models.ChildType('types', _RAPPOR_PARAMETERS_TYPE, True),
48 ])
49
50 _OWNER_TYPE = models.TextNodeType('owner', single_line=True)
51
52 _RAPPOR_METRIC_TYPE = models.ObjectNodeType('rappor-metric',
53 extra_newlines=(1, 1, 1),
54 string_attributes=['name', 'type'],
55 children=[
56 models.ChildType('owners', _OWNER_TYPE, True),
57 models.ChildType('summary', _SUMMARY_TYPE, False),
58 ])
59
60 _RAPPOR_METRICS_TYPE = models.ObjectNodeType('rappor-metrics',
61 extra_newlines=(1, 1, 1),
62 dont_indent=True,
63 children=[
64 models.ChildType('metrics', _RAPPOR_METRIC_TYPE, True),
65 ])
66
67 _RAPPOR_CONFIGURATION_TYPE = models.ObjectNodeType('rappor-configuration',
68 dont_indent=True,
69 children=[
70 models.ChildType('parameterTypes', _RAPPOR_PARAMETERS_TYPES_TYPE, False),
71 models.ChildType('metrics', _RAPPOR_METRICS_TYPE, False),
72 ])
73
74 RAPPOR_XML_TYPE = models.DocumentType(_RAPPOR_CONFIGURATION_TYPE)
75
76
77 def GetTypeNames(config):
78 return set(p['name'] for p in config['parameterTypes']['types'])
79
80
81 def HasMissingOwners(metrics):
82 """Check that all of the metrics have owners.
83
84 Args:
85 metrics: A list of rappor metric description objects.
86
87 Returns:
88 True iff some metrics are missing owners.
89 """
90 missing_owners = [m for m in metrics if not m['owners']]
91 for metric in missing_owners:
92 logging.error('Rappor metric "%s" is missing an owner.', metric['name'])
93 print metric
94 return bool(missing_owners)
95
96
97 def HasInvalidTypes(type_names, metrics):
98 """Check that all of the metrics have valid types.
99
100 Args:
101 type_names: The set of valid type names.
102 metrics: A list of rappor metric description objects.
103
104 Returns:
105 True iff some metrics have invalid types.
106 """
107 invalid_types = [m for m in metrics if m['type'] not in type_names]
108 for metric in invalid_types:
109 logging.error('Rappor metric "%s" has invalid type "%s"',
110 metric['name'], metric['type'])
111 return bool(invalid_types)
112
113
114 def HasErrors(config):
115 """Check that rappor.xml passes some basic validation checks.
116
117 Args:
118 config: The parsed rappor.xml contents.
119
120 Returns:
121 True iff there are validation errors.
122 """
123 metrics = config['metrics']['metrics']
124 type_names = GetTypeNames(config)
125 return (HasMissingOwners(metrics) or
126 HasInvalidTypes(type_names, metrics))
127
128
129 def Cleanup(config):
130 """Preform cleanup on description contents, such as sorting metrics.
131
132 Args:
133 config: The parsed rappor.xml contents.
134 """
135 types = config['parameterTypes']['types']
136 types.sort(key=lambda x: x['name'])
137 metrics = config['metrics']['metrics']
138 metrics.sort(key=lambda x: x['name'])
139
140
141 def UpdateXML(original_xml):
142 """Parse the original xml and return a pretty printed version.
143
144 Args:
145 original_xml: A string containing the original xml file contents.
146
147 Returns:
148 A Pretty printed xml string.
149 """
150 comments, config = RAPPOR_XML_TYPE.Parse(original_xml)
151
152 if HasErrors(config):
153 return None
154
155 Cleanup(config)
156
157 return RAPPOR_XML_TYPE.PrettyPrint(comments, config)
158
159
160 def main(argv):
161 presubmit_util.DoPresubmitMain(argv, 'rappor.xml', 'rappor.old.xml',
162 'pretty_print.py', UpdateXML)
163
164
165 if '__main__' == __name__:
166 sys.exit(main(sys.argv))
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698