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
|
# 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 asyncio
import logging
import os
import re
from shutil import which
from mach.decorators import (
Command,
CommandArgument,
)
_log = logging.getLogger(__name__)
NO_RIPGREP_MSG = """
Could not find ripgrep. Perhaps it needs installing via your package manager
or `cargo install`.
""".strip()
RIPGREP_ERROR_MSG = """
An error occurred running ripgrep. Please check the following error messages:
{}
""".strip()
NO_MODULES_TO_REWRITE_MSG = """
Found no EXTRA_JS_MODULES we could convert in the moz.build file(s) passed.
""".strip()
CANNOT_CONVERT_ERROR_MSG = """
Unfortunately {} cannot be automatically converted.
The script isn't clever enough to convert everything else while leaving that
module alone. Consider (temporarily) removing this module from the moz.build
file, then running the script, and then re-adding the module into the
moz.build file in a copy of the original EXTRA_JS_MODULES instruction.
The list of excluded module patterns is in tools/use-moz-src/mach_commands.py
and should have a comment explaining why it cannot be automatically moved to
moz-src:///.
""".strip()
excluded_from_convert_re = list(
map(
re.compile,
[
"BackgroundTask_.*\\.sys\\.mjs", # Depend on this resource: path.
"BrowserUsageTelemetry\\.sys\\.mjs", # Browser + Android duplicates.
"BuiltInThemes\\.sys\\.mjs", # Thunderbird override
"CrashManager\\.sys.\\.mjs", # Preprocessed.
"ExtensionBrowsingData\\.sys\\.mjs", # Browser + Android duplicates.
"policies/schema\\.sys\\.mjs", # Thunderbird override
"Policies\\.sys\\.mjs", # Thunderbird override
"PromiseWorker\\.m?js", # Preprocessed.
"RFPTargetConstants\\.sys\\.mjs", # Preprocessed.
"ThemeVariableMap\\.sys\\.mjs", # Thunderbird override
],
)
)
def is_excluded_from_convert(path):
"""Returns true if the JSM file shouldn't be converted to ESM."""
path_str = str(path)
for expr in excluded_from_convert_re:
if expr.search(path_str):
return True
return False
def extract_info_from_mozbuild(command_context, paths):
mozbuilds_for_fixing = set()
urlmap = dict()
reader = command_context.mozbuild_reader(config_mode="empty")
for mozbuild_path in paths:
is_browser = mozbuild_path.startswith("browser") or (
mozbuild_path.startswith("devtools")
and not mozbuild_path.startswith("devtools/platform")
)
assignments = reader.find_variables_from_ast(
variables="EXTRA_JS_MODULES", path=mozbuild_path, all_relevant_files=False
)
for path, _variable, key, value in assignments:
module_path = path.replace("moz.build", "") + value
if is_excluded_from_convert(module_path):
_log.log(logging.ERROR, CANNOT_CONVERT_ERROR_MSG.format(value))
return [], dict()
newurl = "moz-src:///" + module_path
module_name = os.path.basename(module_path)
keystr = "/".join(key.split(".")) + "/" if key else ""
resource_suffix = "modules/" + keystr + module_name
if is_browser:
urlmap["resource:///" + resource_suffix] = newurl
else:
urlmap["resource://gre/" + resource_suffix] = newurl
mozbuilds_for_fixing.add(mozbuild_path)
return mozbuilds_for_fixing, urlmap
extra_js_modules_re = re.compile('EXTRA_JS_MODULES(["\\.\\w/\\[\\]]*) [+=]+')
def rewrite_mozbuilds(mozbuilds):
for path in mozbuilds:
with open(path, "r+", encoding="utf-8", newline="\n") as f:
contents = f.read()
contents = re.sub(extra_js_modules_re, "MOZ_SRC_FILES +=", contents)
f.seek(0)
f.write(contents)
f.truncate()
def _get_re_for_urls(urlmap):
old_urls = list(urlmap.keys())
prefix = os.path.commonprefix(old_urls)
prefixlen = len(prefix)
old_urls_without_prefix = map(lambda u: u[prefixlen:], old_urls)
escaped_prefix = re.escape(prefix)
# Put the "app" option in if needed:
escaped_prefix = re.sub("resource:///", "resource://(app)?/", escaped_prefix)
suffixes = "|".join(map(re.escape, old_urls_without_prefix))
return escaped_prefix + "(" + suffixes + ")"
def handle_matching_file(file, urlmap_re, replace_fn):
with open(file, "r+", encoding="utf-8", newline="\n") as f:
contents = f.read()
f.seek(0)
contents = re.sub(urlmap_re, replace_fn, contents)
f.write(contents)
f.truncate()
async def find_and_replace_refs(urlmap):
urlmap_re = _get_re_for_urls(urlmap)
encoding = "utf-8"
cmd_args = [which("rg"), "-l", "-e", urlmap_re]
proc = await asyncio.subprocess.create_subprocess_exec(
*cmd_args, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
)
pending_file_writes = set()
affected_files = set()
def replace_fn(matchobj):
old_url = matchobj.group(0)
# Replace with the mapped URL or with itself if we can't find it.
return urlmap.get(old_url, old_url)
async with asyncio.TaskGroup() as tg:
async def handle_matching_files() -> None:
assert proc.stdout is not None
while matching_file := await proc.stdout.readline():
matching_file = matching_file.decode(encoding, "replace")
if not matching_file.endswith("\n"):
continue
matching_file = matching_file.strip()
if not matching_file:
continue
_log.log(logging.INFO, f"Rewriting {matching_file}")
affected_files.add(matching_file)
task = tg.create_task(
asyncio.to_thread(
handle_matching_file, matching_file, urlmap_re, replace_fn
)
)
pending_file_writes.add(task)
task.add_done_callback(pending_file_writes.discard)
async def handle_errors() -> None:
assert proc.stderr is not None
while chunk := await proc.stderr.read(10000):
_log.log(
logging.ERROR,
RIPGREP_ERROR_MSG.format(chunk.decode("utf-8", "replace")),
)
# We assume that if anything gets written to stderr by ripgrep,
# it'll also exit shortly and we don't need to deal with that
# separately by raising exceptions or whatever.
await asyncio.gather(handle_matching_files(), handle_errors(), proc.wait())
return affected_files
@Command(
"use-moz-src",
category="misc",
description="Convert use of EXTRA_JS_MODULES to MOZ_SRC_FILES",
)
@CommandArgument(
"paths",
nargs="+",
help="Path to the moz.build file(s) to convert.",
)
def use_moz_src(command_context, paths):
"""
This command does two things:
1. replace use of EXTRA_JS_MODULES in the moz.build file passed with
MOZ_SRC_FILES.
2. fix up consumers across the tree that rely on any files in
EXTRA_JS_MODULES using `resource` URLs to use `moz-src` ones
instead.
Note that this only uses the moz.build file in question; it will not
recurse into `DIRS`. If you want to convert subdirectories, use
e.g. `find` to collect the moz.build files and pass all of them on
the commandline.
Example:
./mach use-moz-src browser/components/moz.build
Recursive example:
find browser/components -iname 'moz.build' | xargs ./mach use-moz-src
"""
if not which("rg"):
_log.log(logging.ERROR, NO_RIPGREP_MSG)
return
mozbuilds_for_fixing, urlmap = extract_info_from_mozbuild(command_context, paths)
if not mozbuilds_for_fixing:
_log.log(logging.INFO, NO_MODULES_TO_REWRITE_MSG)
return
_log.log(
logging.INFO,
"Searching the tree and updating all references to:\n {}".format(
"\n ".join(urlmap.keys())
),
)
updated_files = asyncio.run(find_and_replace_refs(urlmap))
_log.log(
logging.INFO,
"Updating build files:\n {}".format("\n ".join(mozbuilds_for_fixing)),
)
rewrite_mozbuilds(mozbuilds_for_fixing)
if len(updated_files) > 0:
_log.log(logging.INFO, f"Formatting {len(updated_files)} affected files...")
command_context._mach_context.commands.dispatch(
"lint",
command_context._mach_context,
linters=["eslint"],
paths=updated_files,
fix=True,
)
_log.log(
logging.INFO, "Done. Make sure to test the result before submitting patches."
)
|