File: mach_commands.py

package info (click to toggle)
firefox-esr 128.13.0esr-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,230,012 kB
  • sloc: cpp: 7,103,971; javascript: 6,088,450; ansic: 3,653,980; python: 1,212,330; xml: 594,604; asm: 420,652; java: 182,969; sh: 71,124; makefile: 20,747; perl: 13,449; objc: 12,399; yacc: 4,583; cs: 3,846; pascal: 2,973; lex: 1,720; ruby: 1,194; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (169 lines) | stat: -rw-r--r-- 6,733 bytes parent folder | download | duplicates (6)
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from mach.decorators import Command, CommandArgument

TODO_FILL_DESCRIPTION = "TODO: Fill in this description. You _can_ use **markdown**."
GENERATED_BY_DESCRIPTION = (
    "This event was generated to correspond to the Legacy Telemetry event {}."
)
DESCRIPTION_INDENT = "      "
LIST_INDENT = "      - "
BUG_URL_TEMPLATE = "https://bugzil.la/{}"

GLEAN_EVENT_TEMPLATE = """
{category}:
  {name}:
    type: event
    description: >
{multiline_description}
    bugs: {bugs_alias}{bugs_list}
    data_reviews: {data_alias}{bugs_list}
    notification_emails:
{emails_list}
    expires: {expiry}
    extra_keys: {extra_alias}{extra_keys}
    telemetry_mirror: {legacy_enum}
""".strip(
    "\n"
)

EXTRA_KEY_DESCRIPTION_INDENT = DESCRIPTION_INDENT + "    "
VALUE_EXTRA_DESCRIPTION = "The `value` of the event. Mirrors to the Legacy Telemetry event's `value` parameter."
EXTRA_KEY_TEMPLATE = """
      {name}:
        description: >
{description}
        type: string
""".strip(
    "\n"
)


@Command(
    "gifft",
    category="misc",
    description="Generate a Glean metric definition for a given Legacy Telemetry probe. Only supports Events.",
)
@CommandArgument(
    "telemetry_probe_name", help="Telemetry probe name (e.g. readermode.view)"
)
def mach_gifft(command_context, telemetry_probe_name):
    from os import path

    telemetrydir = path.join(
        command_context.topsrcdir, "toolkit", "components", "telemetry"
    )

    import itertools
    import sys

    sys.path.append(path.join(telemetrydir, "build_scripts"))
    import textwrap

    from mozparsers import parse_events

    events = parse_events.load_events(path.join(telemetrydir, "Events.yaml"), True)
    for e in events:
        if e.category + "." + e.name == telemetry_probe_name:
            # There are four levels of identification in Legacy Telemetry event
            # definitions: category, name/family, method, and object.
            # If not present, the `method` property will supply the name/family.
            # GIFFT will mirror to a specific identified C++ enum, which means
            # we need to generate Glean events for every combination of method
            # and object.
            category = e.category
            bugs_alias = data_alias = extra_alias = ""
            for m, o in itertools.product(e.methods, e.objects):
                legacy_name = category + "." + m + "#" + o
                name = m + "_" + o
                description = e._definition.get("description", TODO_FILL_DESCRIPTION)
                multiline_description = textwrap.fill(
                    description,
                    width=80 - len(DESCRIPTION_INDENT),
                    initial_indent=DESCRIPTION_INDENT,
                    subsequent_indent=DESCRIPTION_INDENT,
                )
                multiline_description += "\n"
                multiline_description += textwrap.fill(
                    GENERATED_BY_DESCRIPTION.format(legacy_name),
                    width=80 - len(DESCRIPTION_INDENT),
                    initial_indent=DESCRIPTION_INDENT,
                    subsequent_indent=DESCRIPTION_INDENT,
                )

                if bugs_alias:
                    bugs_list = ""
                else:
                    bugs_alias = f"{category}_{m}_bugs"
                    data_alias = f"{category}_{m}_data_reviews"
                    bugs_list = "\n" + textwrap.indent(
                        "\n".join(
                            map(
                                lambda b: BUG_URL_TEMPLATE.format(b),
                                e._definition.get("bug_numbers", []),
                            )
                        ),
                        LIST_INDENT,
                    )
                emails_list = textwrap.indent(
                    "\n".join(e._definition.get("notification_emails", [])), LIST_INDENT
                )

                # expiry_version is a string like `"123.0a1"` or `"never"`,
                # but Glean wants a number like `123` or `never`.
                expiry = e.expiry_version.strip('"').split(".")[0]

                if extra_alias:
                    extra_keys = ""
                else:
                    extra_alias = f"{category}_{m}_extra"
                    multiline_extra_description = textwrap.fill(
                        VALUE_EXTRA_DESCRIPTION,
                        width=80 - len(EXTRA_KEY_DESCRIPTION_INDENT),
                        initial_indent=EXTRA_KEY_DESCRIPTION_INDENT,
                        subsequent_indent=EXTRA_KEY_DESCRIPTION_INDENT,
                    )
                    extra_keys = "\n" + EXTRA_KEY_TEMPLATE.format(
                        name="value", description=multiline_extra_description
                    )
                    for key_name, key_description in e._definition.get(
                        "extra_keys", {}
                    ).items():
                        extra_keys += "\n"
                        extra_keys += EXTRA_KEY_TEMPLATE.format(
                            name=key_name,
                            description=textwrap.indent(
                                key_description, EXTRA_KEY_DESCRIPTION_INDENT
                            ),
                        )

                legacy_enum = (
                    parse_events.convert_to_cpp_identifier(category, ".") + "_"
                )
                legacy_enum += parse_events.convert_to_cpp_identifier(m, "_") + "_"
                legacy_enum += parse_events.convert_to_cpp_identifier(o, "_")

                print(
                    GLEAN_EVENT_TEMPLATE.format(
                        category=category,
                        name=name,
                        multiline_description=multiline_description,
                        bugs_alias=(
                            f"&{bugs_alias}" if bugs_list else f"*{bugs_alias}"
                        ),
                        bugs_list=bugs_list,
                        data_alias=(
                            f"&{data_alias}" if bugs_list else f"*{data_alias}"
                        ),
                        emails_list=emails_list,
                        expiry=expiry,
                        extra_alias=(
                            f"&{extra_alias}" if extra_keys else f"*{extra_alias}"
                        ),
                        extra_keys=extra_keys,
                        legacy_enum=legacy_enum,
                    )
                )
                print()  # We want a newline between event definitions.