Skip to content

Commit 1a35a38

Browse files
hanzala-sohrabahochheiden
authored andcommitted
Bug 2068111 - Fix ruff UP031 and PLW2901 warnings in python/mozbuild/mozbuild/action r=firefox-build-system-reviewers,sergesanspaille,ahochheiden
Replace percent-style string formatting with f-strings for modern Python style and fix loop variable overwriting warnings in python/mozbuild/mozbuild/action/. Differential Revision: https://phabricator.services.mozilla.com/D322635
1 parent de4a229 commit 1a35a38

12 files changed

Lines changed: 60 additions & 85 deletions

‎python/mozbuild/mozbuild/action/fat_aar.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,8 @@ def fat_aar(
7777
# Collect multi-architecture inputs to the fat AAR.
7878
copier = FileCopier()
7979

80-
for arch, zip_path in zip_paths.items():
81-
if not zip_path:
82-
zip_path = _download_zip(distdir, arch)
80+
for arch, zip_path_opt in zip_paths.items():
81+
zip_path = zip_path_opt or _download_zip(distdir, arch)
8382
if verbose:
8483
print(f"Processing '{zip_path}' for architecture {arch}")
8584
# Map old non-architecture-specific path to new architecture-specific path.

‎python/mozbuild/mozbuild/action/file_generate_wrapper.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ def make_absolute(base_path, p):
3232
except Exception:
3333
relative = os.path.relpath(__file__, topsrcdir)
3434
print(
35-
"%s:action caught exception. params=%s\n"
36-
% (relative, json.dumps([script, target_dir] + args, indent=2))
35+
f"{relative}:action caught exception. params={json.dumps([script, target_dir] + args, indent=2)}\n"
3736
)
3837
raise

‎python/mozbuild/mozbuild/action/generate_symbols_file.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def generate_symbols_file(output, *args):
6969
# is, in fact, part of the symbol name as far as the symbols variable
7070
# is concerned.
7171
assert ext == ".def"
72-
output.write("LIBRARY %s\nEXPORTS\n %s\n" % (libname, "\n ".join(symbols)))
72+
output.write(f"LIBRARY {libname}\nEXPORTS\n " + "\n ".join(symbols) + "\n")
7373
elif (
7474
buildconfig.substs.get("GCC_USE_GNU_LD")
7575
or buildconfig.substs["OS_TARGET"] == "SunOS"
@@ -85,11 +85,11 @@ def generate_symbols_file(output, *args):
8585
# *;
8686
# };
8787
output.write(
88-
"%s {\nglobal:\n %s;\nlocal:\n *;\n};" % (libname, ";\n ".join(symbols))
88+
f"{libname} {{\nglobal:\n " + ";\n ".join(symbols) + ";\nlocal:\n *;\n};"
8989
)
9090
elif buildconfig.substs["OS_TARGET"] == "Darwin":
9191
# A list of symbols is generated for Apple ld that simply lists all
9292
# symbols, with an underscore prefix.
93-
output.write("".join("_%s\n" % s for s in symbols))
93+
output.write("".join(f"_{s}\n" for s in symbols))
9494

9595
return set(pp.includes)

‎python/mozbuild/mozbuild/action/langpack_manifest.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ def _extract_numeric_part(part):
391391

392392
buildid = os.environ.get("MOZ_BUILD_DATE")
393393
if buildid and len(buildid) != 14:
394-
print("Ignoring invalid MOZ_BUILD_DATE: %s" % buildid, file=sys.stderr)
394+
print(f"Ignoring invalid MOZ_BUILD_DATE: {buildid}", file=sys.stderr)
395395
buildid = None
396396

397397
if buildid:

‎python/mozbuild/mozbuild/action/node.py‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ def execute_node_cmd(node_cmd_list):
7070
# XXX Starting with an empty list means that node scripts can
7171
# (intentionally or inadvertently) remove deps. Do we want this?
7272
deps = []
73-
for line in stdout.splitlines():
74-
line = line.decode()
73+
for raw_line in stdout.splitlines():
74+
line = raw_line.decode()
7575
if "dep:" in line:
7676
deps.append(line.replace("dep:", ""))
7777
else:
@@ -86,10 +86,9 @@ def execute_node_cmd(node_cmd_list):
8686
# disambiguate this from real "Permission denied" errors so that we
8787
# can log such problems more clearly?
8888
print(
89-
"""Failed with %s. Be sure to check that your mozconfig doesn't
89+
f"""Failed with {err}. Be sure to check that your mozconfig doesn't
9090
have --disable-nodejs in it. If it does, try removing that line and
91-
building again."""
92-
% str(err),
91+
building again.""",
9392
file=sys.stderr,
9493
)
9594
sys.exit(1)

‎python/mozbuild/mozbuild/action/package_generated_sources.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def is_valid_entry(entry):
2828
entry_abspath = mozpath.abspath(entry[1])
2929
if not entry_abspath.startswith(objdir_abspath):
3030
print(
31-
"Warning: omitting generated source [%s] from archive" % entry_abspath,
31+
f"Warning: omitting generated source [{entry_abspath}] from archive",
3232
file=sys.stderr,
3333
)
3434
return False
@@ -40,7 +40,7 @@ def is_valid_entry(entry):
4040
and not os.path.exists(entry_abspath)
4141
):
4242
print(
43-
"Warning: omitting non-existing file [%s] from archive" % entry_abspath,
43+
f"Warning: omitting non-existing file [{entry_abspath}] from archive",
4444
file=sys.stderr,
4545
)
4646
return False

‎python/mozbuild/mozbuild/action/process_define_files.py‎

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ def process_define_file(output, input):
4141
r = re.compile(
4242
r"^\s*#\s*(?P<cmd>[a-z]+)(?:\s+(?P<name>\S+)(?:\s+(?P<value>\S+))?)?", re.U
4343
)
44-
for l in input_file:
45-
m = r.match(l)
46-
if m:
44+
for raw_line in input_file:
45+
line = raw_line
46+
if m := r.match(line):
4747
cmd = m.group("cmd")
4848
name = m.group("name")
4949
value = m.group("value")
@@ -71,28 +71,35 @@ def define_for_name(name, val):
7171
for name, val in config.defines["ALLDEFINES"].items()
7272
)
7373
)
74-
l = l[: m.start("cmd") - 1] + defines + l[m.end("name") :]
74+
line = (
75+
line[: m.start("cmd") - 1] + defines + line[m.end("name") :]
76+
)
7577
elif cmd == "define":
7678
if value and name in config.defines:
77-
l = (
78-
l[: m.start("value")]
79+
line = (
80+
line[: m.start("value")]
7981
+ str(config.defines[name])
80-
+ l[m.end("value") :]
82+
+ line[m.end("value") :]
8183
)
8284
elif cmd == "undef":
8385
if name in config.defines:
84-
l = (
85-
l[: m.start("cmd")]
86+
line = (
87+
line[: m.start("cmd")]
8688
+ "define"
87-
+ l[m.end("cmd") : m.end("name")]
89+
+ line[m.end("cmd") : m.end("name")]
8890
+ " "
8991
+ str(config.defines[name])
90-
+ l[m.end("name") :]
92+
+ line[m.end("name") :]
9193
)
9294
else:
93-
l = "/* " + l[: m.end("name")] + " */" + l[m.end("name") :]
95+
line = (
96+
"/* "
97+
+ line[: m.end("name")]
98+
+ " */"
99+
+ line[m.end("name") :]
100+
)
94101

95-
output.write(l)
102+
output.write(line)
96103

97104
deps = {path}
98105
deps.update(config.get_dependencies())

‎python/mozbuild/mozbuild/action/symbols_archive.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def make_archive(archive_name, base, exclude, include):
2020
def fill_archive(add_file):
2121
for pat in include:
2222
for p, f in finder.find(pat):
23-
print(' Adding to "%s":\n\t"%s"' % (archive_basename, p))
23+
print(f' Adding to "{archive_basename}":\n\t"{p}"')
2424
add_file(p, f)
2525

2626
with open(archive_name, "wb") as fh:

‎python/mozbuild/mozbuild/action/test_archive.py‎

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -192,16 +192,10 @@
192192
"source": buildconfig.topobjdir,
193193
"base": "dist/bin",
194194
"patterns": [
195-
"%s%s" % (f, buildconfig.substs["BIN_SUFFIX"])
196-
for f in TEST_HARNESS_BINS
195+
f"{f}{buildconfig.substs['BIN_SUFFIX']}" for f in TEST_HARNESS_BINS
197196
]
198197
+ [
199-
"%s%s%s"
200-
% (
201-
buildconfig.substs["DLL_PREFIX"],
202-
f,
203-
buildconfig.substs["DLL_SUFFIX"],
204-
)
198+
f"{buildconfig.substs['DLL_PREFIX']}{f}{buildconfig.substs['DLL_SUFFIX']}"
205199
for f in TEST_HARNESS_DLLS
206200
],
207201
"dest": "bin",
@@ -636,7 +630,7 @@
636630
{
637631
"source": buildconfig.topobjdir,
638632
"base": "dist/bin",
639-
"pattern": "http3server%s" % buildconfig.substs["BIN_SUFFIX"],
633+
"pattern": f"http3server{buildconfig.substs['BIN_SUFFIX']}",
640634
"dest": "xpcshell/http3server",
641635
},
642636
{
@@ -708,16 +702,10 @@
708702
"source": buildconfig.topobjdir,
709703
"base": "dist/bin",
710704
"patterns": [
711-
"%s%s" % (f, buildconfig.substs["BIN_SUFFIX"])
712-
for f in TEST_HARNESS_BINS
705+
f"{f}{buildconfig.substs['BIN_SUFFIX']}" for f in TEST_HARNESS_BINS
713706
]
714707
+ [
715-
"%s%s%s"
716-
% (
717-
buildconfig.substs["DLL_PREFIX"],
718-
f,
719-
buildconfig.substs["DLL_SUFFIX"],
720-
)
708+
f"{buildconfig.substs['DLL_PREFIX']}{f}{buildconfig.substs['DLL_SUFFIX']}"
721709
for f in TRAIN_HOP_DLLS
722710
],
723711
"dest": "bin",
@@ -782,8 +770,8 @@
782770
itertools.chain(*(e.get("ignore", []) for e in ARCHIVE_FILES["common"]))
783771
)
784772

785-
if not any(p.startswith("%s/" % k) for p in ignores):
786-
raise Exception('"common" ignore list probably should contain %s' % k)
773+
if not any(p.startswith(f"{k}/") for p in ignores):
774+
raise Exception(f'"common" ignore list probably should contain {k}')
787775

788776

789777
def find_generated_harness_files():
@@ -860,9 +848,8 @@ def find_files(archive):
860848
finder = FileFinder(os.path.join(source, base), **common_kwargs)
861849

862850
for pattern in patterns:
863-
for p, f in finder.find(pattern):
864-
if dest:
865-
p = mozpath.join(dest, p)
851+
for raw_p, f in finder.find(pattern):
852+
p = mozpath.join(dest, raw_p) if dest else raw_p
866853
yield p, f
867854

868855

@@ -873,22 +860,22 @@ def find_manifest_dirs(topsrcdir, manifests):
873860
"""
874861
dirs = set()
875862

876-
for p in manifests:
877-
p = os.path.join(topsrcdir, p)
863+
for manifest_path in manifests:
864+
abs_manifest_path = os.path.join(topsrcdir, manifest_path)
878865

879-
if p.endswith(".ini") or p.endswith(".toml"):
866+
if abs_manifest_path.endswith(".ini") or abs_manifest_path.endswith(".toml"):
880867
test_manifest = TestManifest()
881-
test_manifest.read(p)
868+
test_manifest.read(abs_manifest_path)
882869
dirs |= set([os.path.dirname(m) for m in test_manifest.manifests()])
883870

884-
elif p.endswith(".list"):
871+
elif abs_manifest_path.endswith(".list"):
885872
m = ReftestManifest()
886-
m.load(p)
873+
m.load(abs_manifest_path)
887874
dirs |= m.dirs
888875

889876
else:
890877
raise Exception(
891-
f'"{os.path.splitext(p)[1]}" is not a supported manifest format.'
878+
f'"{os.path.splitext(abs_manifest_path)[1]}" is not a supported manifest format.'
892879
)
893880

894881
dirs = {mozpath.normpath(d[len(topsrcdir) :]).lstrip("/") for d in dirs}
@@ -946,14 +933,13 @@ def main(argv):
946933
)
947934
file_count += 1
948935
else:
949-
raise Exception("unhandled file extension: %s" % out_file)
936+
raise Exception(f"unhandled file extension: {out_file}")
950937

951938
duration = time.monotonic() - t_start
952939
zip_size = os.path.getsize(args.outputfile)
953940
basename = os.path.basename(args.outputfile)
954941
print(
955-
"Wrote %d files in %d bytes to %s in %.2fs"
956-
% (file_count, zip_size, basename, duration)
942+
f"Wrote {file_count} files in {zip_size} bytes to {basename} in {duration:.2f}s"
957943
)
958944

959945

‎python/mozbuild/mozbuild/action/wrap_rustc.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ def parse_file(f):
3636
args += ["-L", val]
3737
else:
3838
raise Exception(
39-
"Unknown flag passed through "
40-
'"cargo:rustc-flags": "%s"' % flag
39+
f'Unknown flag passed through "cargo:rustc-flags": "{flag}"'
4140
)
4241
elif key == "rustc-link-lib" and f == crate_output:
4342
args += ["-l", value]

0 commit comments

Comments
 (0)