File: model.py

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (376 lines) | stat: -rw-r--r-- 11,225 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# Copyright 2021 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Model of a structured metrics description xml file.

This marshals an XML string into a Model, and validates that the XML is
semantically correct. The model can also be used to create a canonically
formatted version XML.
"""

import textwrap as tw
import xml.etree.ElementTree as ET
import re

import sync.model_util as util


# Default key rotation period if not explicitly specified in the XML.
DEFAULT_KEY_ROTATION_PERIOD = 90

# Default scope if not explicitly specified in the XML.
DEFAULT_PROJECT_SCOPE = "device"


def wrap(text: str, indent: str) -> str:
  wrapper = tw.TextWrapper(width=80,
                           initial_indent=indent,
                           subsequent_indent=indent)
  return wrapper.fill(tw.dedent(text))


class Model:
  """Represents all projects in the structured.xml file.

    A Model is initialized with an XML string representing the top-level of
    the structured.xml file. This file is built from three building blocks:
    projects, events, and metrics. These have the following attributes.

      PROJECT
      - summary
      - id specifier
      - (optional) one or more targets. If undefined, defaults to 'chromium'
      - one or more owners
      - one or more events

      EVENT
      - summary
      - one or more metrics

      METRIC
      - summary
      - data type

    The following is an example input XML.

      <structured-metrics>
      <project name="MyProject" targets="chromium">
        <owner>owner@chromium.org</owner>
        <id>none</id>
        <scope>profile</scope>
        <summary> My project. </summary>

        <event name="MyEvent">
          <summary> My event. </summary>
          <metric name="MyMetric" type="int">
            <summary> My metric. </summary>
          </metric>
        </event>
      </project>
      </structured-metrics>

    Calling str(model) will return a canonically formatted XML string.
    """

  OWNER_REGEX = r"^.+@(chromium\.org|google\.com)$"
  NAME_REGEX = r"^[A-Za-z0-9_.]+$"
  VARIANT_NAME_REGEX = r"^[A-Z0-9_.]+$"
  TYPE_REGEX = r"^(hmac-string|raw-string|int|double|int-array)$"
  ID_REGEX = r"^(none|per-project|uma)$"
  SCOPE_REGEX = r"^(profile|device)$"
  KEY_REGEX = r"^[0-9]+$"
  MAX_REGEX = r"^[0-9]+$"
  TARGET_REGEX = r"^(chromium|webui)$"

  def __init__(self, xml_string: str, platform: str):
    elem = ET.fromstring(xml_string)
    util.check_attributes(elem, set())
    util.check_children(elem, {"project"})
    util.check_child_names_unique(elem, "project")
    projects = util.get_compound_children(elem, "project")
    self.projects = [Project(p, platform) for p in projects]

  def __repr__(self):
    projects = "\n\n".join(str(p) for p in self.projects)

    return f"""\
<structured-metrics>

{projects}

</structured-metrics>"""


def merge_models(primary: Model, other: Model) -> Model:
  """Merges two models into one."""
  primary.projects += [
      p for p in other.projects if not re.match("Test", p.name)
  ]
  return primary


class Project:
  """Represents a single structured metrics project.

    A Project is initialized with an XML node representing one project, eg:

      <project name="MyProject" cros_events="true" targets="webui,chromium">
        <owner>owner@chromium.org</owner>
        <id>none</id>
        <scope>project</scope>
        <key-rotation>60</key-rotation>
        <summary> My project. </summary>

        <enum name="Enum1">
          <variant value="1">VARIANT1</variant>
          <variant value="2">VARIANT2</variant>
          <variant value="5">VARIANT3</variant>
        </enum>

        <event name="MyEvent">
          <summary> My event. </summary>
          <metric name="MyMetric" type="int">
            <summary> My metric. </summary>
          </metric>
        </event>
      </project>

    Calling str(project) will return a canonically formatted XML string.
    """

  def __init__(self, elem: ET.Element, platform: str):
    util.check_attributes(elem, {"name"}, {"cros_events", "targets"})
    util.check_children(elem, {"id", "summary", "owner", "event"}, {"enum"})
    util.check_child_names_unique(elem, "event")

    self.name = util.get_attr(elem, "name", Model.NAME_REGEX)
    self.id = util.get_text_child(elem, "id", Model.ID_REGEX)
    self.summary = util.get_text_child(elem, "summary")
    self.owners = util.get_text_children(elem, "owner", Model.OWNER_REGEX)
    self.platform = platform

    self.key_rotation_period = DEFAULT_KEY_ROTATION_PERIOD
    self.scope = DEFAULT_PROJECT_SCOPE
    self.is_event_sequence_project = util.get_boolean_attr(elem, "cros_events")

    # Check if key-rotation is specified. If so, then change the
    # key_rotation_period.
    if elem.find("key-rotation") is not None:
      self.key_rotation_period = util.get_text_child(elem, "key-rotation",
                                                     Model.KEY_REGEX)

    # enums need to be populated first because they are used for validation
    util.check_child_names_unique(elem, "enum")
    self.enums = [
        Enum(e, self) for e in util.get_compound_children(elem, "enum", True)
    ]

    if "targets" in elem.attrib:
      self.targets = set(
          util.get_optional_attr_list(elem, "targets", Model.TARGET_REGEX))
    else:
      self.targets = set()

    # Check if scope is specified. If so, then change the scope.
    if elem.find("scope") is not None:
      self.scope = util.get_text_child(elem, "scope", Model.SCOPE_REGEX)

    self.events = [
        Event(e, self) for e in util.get_compound_children(elem, "event")
    ]

  def has_enum(self, enum_name: str) -> bool:
    enum_names = [e.name for e in self.enums]
    return enum_name in enum_names

  def __repr__(self):
    events = "\n\n".join(str(e) for e in self.events)
    events = tw.indent(events, "  ")
    summary = wrap(self.summary, indent="    ")
    owners = "\n".join(f"  <owner>{o}</owner>" for o in self.owners)
    if self.is_event_sequence_project:
      cros_events_attr = ' cros_events="true"'
    else:
      cros_events_attr = ""
    if self.targets:
      targets = ' targets="' + ",".join(self.targets) + '"'
    else:
      targets = ""

    enums = "\n\n".join(str(v) for v in self.enums)
    enums = tw.indent(enums, "  ")

    return f"""\
<project name="{self.name}"{cros_events_attr}{targets}>
{owners}
  <id>{self.id}</id>
  <scope>{self.scope}</scope>
  <key-rotation>{self.key_rotation_period}</key-rotation>
  <summary>
{summary}
  </summary>
{enums}
{events}
</project>"""


class Enum:
  """Represents an enum value for a project.

    An Enum is initialized with an XML node representing one enum, eg:

    <enum name="EnumName">
      <variant value="1">Name1</variant>
      <variant value="2">Name2</variant>
      <variant value="5">Name3</variant>
    </enum>

    Calling str(enum) will return a canonically formatted XML string.
    """

  def __init__(self, elem: ET.Element, project: Project):
    self.project = project
    util.check_attributes(elem, {"name"})

    util.check_children(elem, {"variant"})

    self.name = util.get_attr(elem, "name", Model.NAME_REGEX)
    self.variants = [
        Variant(e, self)
        for e in util.get_compound_children(elem, "variant", allow_text=True)
    ]
    variant_names = [v.name for v in self.variants]
    util.check_names_unique(elem, variant_names, "variant")

  def __repr__(self):
    variants = '\n'.join(str(v) for v in self.variants)
    variants = tw.indent(variants, "  ")
    return f"""\
<enum name="{self.name}">
{variants}
</enum>"""


class Variant:
  """Represents an element of an Enum.

    <variant value="1">Name1</variant>

    Calling str(variant) will return a canonically formatted XML string.
    """

  def __init__(self, elem: ET.Element, enum: Enum):
    util.check_attributes(elem, {"value"})
    self.name = util.get_text(elem, Model.VARIANT_NAME_REGEX)
    self.value = util.get_attr(elem, "value")
    self.enum = enum

  def __repr__(self):
    return f'<variant value="{self.value}">{self.name}</variant>'


class Event:
  """Represents a single structured metrics event.

    An Event is initialized with an XML node representing one event, eg:

      <event name="MyEvent">
        <summary> My event. </summary>
        <metric name="MyMetric" type="int">
          <summary> My metric. </summary>
        </metric>
      </event>

    Calling str(event) will return a canonically formatted XML string.
    """

  def __init__(self, elem: ET.Element, project: Project):
    util.check_attributes(elem, {"name"}, {"force_record"})

    if project.is_event_sequence_project:
      expected_children = {"summary"}
    else:
      expected_children = {"summary", "metric"}

    util.check_children(elem, expected_children)

    util.check_child_names_unique(elem, "metric")

    self.name = util.get_attr(elem, "name", Model.NAME_REGEX)
    self.force_record = util.get_boolean_attr(elem, "force_record")
    self.summary = util.get_text_child(elem, "summary")
    self.metrics = [
        Metric(m, project) for m in util.get_compound_children(
            elem, "metric", project.is_event_sequence_project)
    ]

  def __repr__(self):
    metrics = "\n".join(str(m) for m in self.metrics)
    metrics = tw.indent(metrics, "  ")
    summary = wrap(self.summary, indent="    ")
    if self.force_record:
      force_record = ' force_record="true"'
    else:
      force_record = ""

    return f"""\
<event name="{self.name}"{force_record}>
  <summary>
{summary}
  </summary>
{metrics}
</event>"""


class Metric:
  """Represents a single metric.

    A Metric is initialized with an XML node representing one metric, eg:

      <metric name="MyMetric" type="int">
        <summary> My metric. </summary>
      </metric>

    Calling str(metric) will return a canonically formatted XML string.
    """

  def __init__(self, elem: ET.Element, project: Project):
    util.check_attributes(elem, {"name", "type"}, {"max"})
    util.check_children(elem, {"summary"})

    self.name = util.get_attr(elem, "name", Model.NAME_REGEX)

    self.type = util.get_attr(elem, "type")
    # If the type isn't an enum then check it it must be a builtin type.
    if project.has_enum(self.type):
      self.is_enum = True
    else:
      self.is_enum = False
      self.type = util.get_attr(elem, "type", Model.TYPE_REGEX)

    self.summary = util.get_text_child(elem, "summary")

    if self.type == "int-array":
      self.max_size = int(util.get_attr(elem, "max", Model.MAX_REGEX))

    if self.type == "raw-string" and (project.id != "none" and
                                      not project.is_event_sequence_project):
      util.error(
          elem,
          "raw-string metrics must be in a project with id type "
          f"'none' or sequenced project, but {project.name} has "
          f"id type '{project.id}'",
      )

  def is_array(self) -> bool:
    return "array" in self.type

  def __repr__(self):
    summary = wrap(self.summary, indent="    ")
    return f"""\
<metric name="{self.name}" type="{self.type}">
  <summary>
{summary}
  </summary>
</metric>"""