File: mach_commands.py

package info (click to toggle)
firefox-esr 140.4.0esr-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 4,539,276 kB
  • sloc: cpp: 7,381,286; javascript: 6,388,710; ansic: 3,710,139; python: 1,393,780; xml: 628,165; asm: 426,918; java: 184,004; sh: 65,742; makefile: 19,302; objc: 13,059; perl: 12,912; yacc: 4,583; cs: 3,846; pascal: 3,352; lex: 1,720; ruby: 1,226; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (393 lines) | stat: -rw-r--r-- 12,366 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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# 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/.

import re
from pathlib import Path

from mach.decorators import Command, CommandArgument, SubCommand

LICENSE_HEADER = """# 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/.
"""

GENERATED_HEADER = """
### This file was AUTOMATICALLY GENERATED by `./mach update-glean-tags`
### DO NOT edit it by hand.
"""

# A list of bug components only present in certain build configurations.
# Include any valid BMO bug component that is missed when you run
# `./mach update-glean-tags` on certain platforms.
PLATFORM_SPECIFIC_COMPONENTS = [
    "Toolkit :: Default Browser Agent",  # Windows-only
]

DEFAULT_TAG_CONTENT = {
    "description": "The Bugzilla component which applies to this object."
}

DATA_REVIEW_HELP = """
Beginning 2024-05-07[1], data reviews for projects in mozilla-central are now
conducted on Phabricator. Simply duplicate your bug URL from the `bugs` list to
the `data_reviews` list in your metrics and pings definitions, and push for code
review in the normal way[2].

More details about this process can be found in the in-tree docs[3] and wiki[4].

If you'd like to generate a Data Review Request template anyway (if, for
instance, you can't use Phabricator for your data review or you need a Data
Review Request to aid in a Sensitive Data Review process. Or you're just
curious), you can invoke glean_parser directly:

./mach python -m glean_parser data-review

[1]: https://groups.google.com/a/mozilla.org/g/firefox-dev/c/7z-i6UhPoKY
[2]: https://firefox-source-docs.mozilla.org/contributing/index.html
[3]: https://firefox-source-docs.mozilla.org/contributing/data-review.html
[4]: https://wiki.mozilla.org/Data_Collection
"""


@Command(
    "data-review",
    category="misc",
    description="Describe how Data Review works in mozilla-central",
)
def data_review(command_context):
    # Data Review happens in Phabricator now
    # (https://groups.google.com/a/mozilla.org/g/firefox-dev/c/7z-i6UhPoKY)
    # so explain how to do it.

    print(DATA_REVIEW_HELP)


@Command(
    "update-glean-tags",
    category="misc",
    description=(
        "Creates a list of valid glean tags based on in-tree bugzilla component definitions"
    ),
)
def update_glean_tags(command_context):
    import yaml
    from mozbuild.backend.configenvironment import ConfigEnvironment
    from mozbuild.frontend.reader import BuildReader

    config = ConfigEnvironment(
        command_context.topsrcdir,
        command_context.topobjdir,
        defines=command_context.defines,
        substs=command_context.substs,
    )

    reader = BuildReader(config)
    bug_components = set()
    for p in reader.read_topsrcdir():
        if p.get("BUG_COMPONENT"):
            bug_components.add(p["BUG_COMPONENT"])

    tags_filename = (Path(__file__).parent / "../tags.yaml").resolve()

    tags = {"$schema": "moz://mozilla.org/schemas/glean/tags/1-0-0"}
    for bug_component in bug_components:
        product = bug_component.product.strip()
        component = bug_component.component.strip()
        tags[f"{product} :: {component}"] = DEFAULT_TAG_CONTENT

    for bug_component in PLATFORM_SPECIFIC_COMPONENTS:
        tags[bug_component] = DEFAULT_TAG_CONTENT

    # pyyaml will anchor+alias DEFAULT_TAG_CONTENT which would normally be fine,
    # but I don't want the whole file to change all at once right now.
    yaml.Dumper.ignore_aliases = lambda self, data: True

    open(tags_filename, "w").write(
        f"{LICENSE_HEADER}\n{GENERATED_HEADER}\n\n"
        + yaml.dump(tags, width=78, explicit_start=True, line_break="\n")
    )


def replace_in_file(path, pattern, replace):
    """
    Replace `pattern` with `replace` in the file `path`.
    The file is modified on disk.

    Returns `True` if exactly one replacement happened.
    `False` otherwise.
    """

    import re

    with open(path, "r+") as file:
        data = file.read()
        data, subs_made = re.subn(pattern, replace, data, flags=re.MULTILINE)

        file.seek(0)
        file.write(data)
        file.truncate()

        if subs_made != 1:
            return False

    return True


def replace_in_file_or_die(path, pattern, replace):
    """
    Replace `pattern` with `replace` in the file `path`.
    The file is modified on disk.

    If not exactly one occurrence of `pattern` was replaced it will exit with exit code 1.
    """

    import sys

    success = replace_in_file(path, pattern, replace)
    if not success:
        print(f"ERROR: Failed to replace one occurrence in {path}")
        print(f"  Pattern: {pattern}")
        print(f"  Replace: {replace}")
        print("File was modified. Check the diff.")
        sys.exit(1)


@Command(
    "update-glean",
    category="misc",
    description="Update Glean to the given version",
)
@CommandArgument("version", help="Glean version to upgrade to")
def update_glean(command_context, version):
    import textwrap

    topsrcdir = Path(command_context.topsrcdir)

    replace_in_file_or_die(
        topsrcdir / "gradle" / "libs.versions.toml",
        r'mozilla-glean = "[0-9.]+"',
        f'mozilla-glean = "{version}"',
    )
    replace_in_file_or_die(
        topsrcdir / "Cargo.toml",
        r'^glean = "=[0-9.]+"',
        f'glean = "={version}"',
    )
    replace_in_file_or_die(
        topsrcdir / "gfx" / "wr" / "Cargo.toml",
        r'^glean = "=[0-9.]+"',
        f'glean = "={version}"',
    )
    replace_in_file_or_die(
        topsrcdir / "python" / "sites" / "mach.txt",
        r"glean-sdk==[0-9.]+",
        f"glean-sdk=={version}",
    )

    instructions = f"""
    We've edited the necessary files to require Glean SDK {version}.

    To ensure Glean and Firefox's other Rust dependencies are appropriately vendored,
    please run the following commands:

        cargo update -p glean
        ./mach vendor rust --ignore-modified

    `./mach vendor rust` may identify version mismatches.
    Please consult the Updating the Glean SDK docs for assistance:
    https://firefox-source-docs.mozilla.org/toolkit/components/glean/dev/updating_sdk.html

    The Glean SDK is already vetted and no additional vetting for it is necessary.
    To prune the configuration file after vendoring run:

        ./mach cargo vet prune

    Then, to update webrender which independently relies on the Glean SDK, run:

        cd gfx/wr
        cargo update -p glean

    Then, to ensure all is well, build Firefox and run the FOG tests.
    Instructions can be found here:
    https://firefox-source-docs.mozilla.org/toolkit/components/glean/dev/testing.html
    """

    print(textwrap.dedent(instructions))


@Command(
    "event-into-legacy",
    category="misc",
    description="Create a Legacy Telemetry compatible event definition from an existing Glean Event metric.",
)
@CommandArgument(
    "--append",
    "-a",
    action="store_true",
    help="Append to toolkit/components/telemetry/Events.yaml (note: verify and make any necessary modifications before landing).",
)
@CommandArgument("event", default=None, nargs="?", type=str, help="Event name.")
def event_into_legacy(command_context, event=None, append=False):
    # Get the metrics_index's list of metrics indices
    # by loading the index as a module.
    import sys
    from os import path

    sys.path.append(path.join(path.dirname(__file__), path.pardir))

    from metrics_index import metrics_yamls

    sys.path.append(path.dirname(__file__))

    from translate_events import translate_event

    legacy_yaml_path = path.join(
        Path(command_context.topsrcdir),
        "toolkit",
        "components",
        "telemetry",
        "Events.yaml",
    )

    return translate_event(
        event,
        append,
        [Path(command_context.topsrcdir) / x for x in metrics_yamls],
        legacy_yaml_path,
    )


def is_glean_path(glean_path):
    """
    Check if the given path could be a Glean repository
    by checking for the existence of the right manifest files.
    """
    main_cargo = Path(glean_path) / "Cargo.toml"
    core_cargo = Path(glean_path) / "glean-core" / "Cargo.toml"
    rlb_cargo = Path(glean_path) / "glean-core" / "rlb" / "Cargo.toml"

    return main_cargo.is_file() and core_cargo.is_file() and rlb_cargo.is_file()


GLEAN_PATCH_PATH = """
glean-core = {{ path = "{core_path}" }}
glean = {{ path = "{rlb_path}" }}
""".strip()

GLEAN_PATCH_GIT = """
glean-core = {{ git = "{repo}", branch = "{branch}" }}
glean = {{ git = "{repo}", branch = "{branch}" }}
""".strip()

TOML_GLEAN_PATCH_RE = re.compile(r"^glean(-core)? = {.+}\n?", re.MULTILINE)


def patch_section(glean_path, branch):
    """
    Return the right patch section for a `Cargo.toml` based on the given Glean path.

    A `https` URI will be used as a git location and the given branch inserted.
    Otherwise it will be treated as a path dependency.
    """
    import urllib.parse

    uri = urllib.parse.urlparse(glean_path)

    if uri.scheme == "https":
        section = GLEAN_PATCH_GIT.format(repo=glean_path, branch=branch)
        return section
    elif uri.scheme == "":
        if not is_glean_path(uri.path):
            raise Exception(
                f"given path '{uri.path}' does not point to a Glean checkout"
            )

        core_path = Path(glean_path) / "glean-core"
        rlb_path = Path(glean_path) / "glean-core" / "rlb"
        section = GLEAN_PATCH_PATH.format(core_path=core_path, rlb_path=rlb_path)
        return section
    else:
        raise Exception("unsupported format. Use a path or a https: URL")


@Command(
    "glean",
    category="misc",
    description="Glean utilities",
)
def glean(command_context):
    print("Usage:")
    print("  mach glean dev <path>  Use the given URL or path as the Glean source")
    print("")
    print("  mach glean prod        Switch back to the normal production use of Glean.")
    print("                         Removes any patch overrides.")


@SubCommand(
    "glean",
    "dev",
    description="Use the given URL or path as the Glean source",
)
@CommandArgument(
    "glean_path",
    help="Path or URL to a Glean repository. A patch can be absolute or relative to the mozilla-central root.",
)
@CommandArgument(
    "branch", default="main", nargs="?", help="branch to use for Glean repository"
)
def glean_dev(command_context, glean_path, branch):
    cargo_toml = Path(command_context.topsrcdir) / "Cargo.toml"
    content = open(cargo_toml).read()

    if re.search(TOML_GLEAN_PATCH_RE, content):
        content = re.sub(TOML_GLEAN_PATCH_RE, "", content)

    patch = patch_section(glean_path, branch)
    glean_source(command_context, cargo_toml, content, patch)


@SubCommand(
    "glean",
    "prod",
    description="Switch back to the normal production use of Glean. Removes any patch overrides.",
)
def glean_prod(command_context):
    cargo_toml = Path(command_context.topsrcdir) / "Cargo.toml"
    content = open(cargo_toml).read()

    if re.search(TOML_GLEAN_PATCH_RE, content):
        content = re.sub(TOML_GLEAN_PATCH_RE, "", content)

    glean_source(command_context, cargo_toml, content)


def glean_source(command_context, cargo_toml, content, patch=None):
    if patch:
        print(f"Adding Cargo patch for Glean in {cargo_toml}")
    else:
        print(f"Removing Cargo patch for Glean in {cargo_toml}")

    with open(cargo_toml, "w") as fp:
        fp.write(content)
        if patch:
            print("Adding these lines:")
            print(patch)
            print(patch, file=fp)

    print("Vendoring it for you:\n")
    print("  mach vendor rust --force --ignore-modified")
    print("\nYou can commit the local changes afterwards:\n")
    print("  git add Cargo.toml Cargo.lock third_party/rust")
    print("  git commit -m 'Local Glean development'")
    print("")

    run_mach(
        command_context, "vendor", subcommand="rust", force=True, ignore_modified=True
    )


def run_mach(command_context, cmd, **kwargs):
    return command_context._mach_context.commands.dispatch(
        cmd, command_context._mach_context, **kwargs
    )