Skip to content

Commit a15aeec

Browse files
GH-139590: Run ruff format on pre-commit for Tools/wasm (#139591)
Co-authored-by: Hugo van Kemenade <[email protected]>
1 parent c53c55b commit a15aeec

File tree

8 files changed

+306
-149
lines changed

8 files changed

+306
-149
lines changed

.pre-commit-config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ repos:
3434
name: Run Ruff (format) on Tools/build/check_warnings.py
3535
args: [--check, --config=Tools/build/.ruff.toml]
3636
files: ^Tools/build/check_warnings.py
37+
- id: ruff-format
38+
name: Run Ruff (format) on Tools/wasm/
39+
args: [--check, --config=Tools/wasm/.ruff.toml]
40+
files: ^Tools/wasm/
3741

3842
- repo: https://github.com/psf/black-pre-commit-mirror
3943
rev: 25.9.0

Tools/wasm/.ruff.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
extend = "../../.ruff.toml" # Inherit the project-wide settings
2+
3+
[format]
4+
preview = true
5+
docstring-code-format = true
6+
7+
[lint]
8+
select = [
9+
"C4", # flake8-comprehensions
10+
"E", # pycodestyle
11+
"F", # pyflakes
12+
"I", # isort
13+
"ISC", # flake8-implicit-str-concat
14+
"LOG", # flake8-logging
15+
"PGH", # pygrep-hooks
16+
"PT", # flake8-pytest-style
17+
"PYI", # flake8-pyi
18+
"RUF100", # Ban unused `# noqa` comments
19+
"UP", # pyupgrade
20+
"W", # pycodestyle
21+
"YTT", # flake8-2020
22+
]
23+
ignore = [
24+
"E501", # Line too long
25+
"F541", # f-string without any placeholders
26+
"PYI024", # Use `typing.NamedTuple` instead of `collections.namedtuple`
27+
"PYI025", # Use `from collections.abc import Set as AbstractSet`
28+
]

Tools/wasm/emscripten/__main__.py

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
PREFIX_DIR = CROSS_BUILD_DIR / HOST_TRIPLE / "prefix"
3434

3535
LOCAL_SETUP = CHECKOUT / "Modules" / "Setup.local"
36-
LOCAL_SETUP_MARKER = "# Generated by Tools/wasm/emscripten.py\n".encode("utf-8")
36+
LOCAL_SETUP_MARKER = "# Generated by Tools/wasm/emscripten.py\n".encode(
37+
"utf-8"
38+
)
3739

3840

3941
def updated_env(updates={}):
@@ -45,7 +47,9 @@ def updated_env(updates={}):
4547
# https://reproducible-builds.org/docs/source-date-epoch/
4648
git_epoch_cmd = ["git", "log", "-1", "--pretty=%ct"]
4749
try:
48-
epoch = subprocess.check_output(git_epoch_cmd, encoding="utf-8").strip()
50+
epoch = subprocess.check_output(
51+
git_epoch_cmd, encoding="utf-8"
52+
).strip()
4953
env_defaults["SOURCE_DATE_EPOCH"] = epoch
5054
except subprocess.CalledProcessError:
5155
pass # Might be building from a tarball.
@@ -79,7 +83,11 @@ def wrapper(context):
7983
terminal_width = 80
8084
print("⎯" * terminal_width)
8185
print("📁", working_dir)
82-
if clean_ok and getattr(context, "clean", False) and working_dir.exists():
86+
if (
87+
clean_ok
88+
and getattr(context, "clean", False)
89+
and working_dir.exists()
90+
):
8391
print("🚮 Deleting directory (--clean)...")
8492
shutil.rmtree(working_dir)
8593

@@ -128,7 +136,9 @@ def build_python_path():
128136
if not binary.is_file():
129137
binary = binary.with_suffix(".exe")
130138
if not binary.is_file():
131-
raise FileNotFoundError("Unable to find `python(.exe)` in " f"{NATIVE_BUILD_DIR}")
139+
raise FileNotFoundError(
140+
f"Unable to find `python(.exe)` in {NATIVE_BUILD_DIR}"
141+
)
132142

133143
return binary
134144

@@ -158,7 +168,8 @@ def make_build_python(context, working_dir):
158168
cmd = [
159169
binary,
160170
"-c",
161-
"import sys; " "print(f'{sys.version_info.major}.{sys.version_info.minor}')",
171+
"import sys; "
172+
"print(f'{sys.version_info.major}.{sys.version_info.minor}')",
162173
]
163174
version = subprocess.check_output(cmd, encoding="utf-8").strip()
164175

@@ -173,7 +184,9 @@ def check_shasum(file: str, expected_shasum: str):
173184

174185

175186
def download_and_unpack(working_dir: Path, url: str, expected_shasum: str):
176-
with tempfile.NamedTemporaryFile(suffix=".tar.gz", delete_on_close=False) as tmp_file:
187+
with tempfile.NamedTemporaryFile(
188+
suffix=".tar.gz", delete_on_close=False
189+
) as tmp_file:
177190
with urlopen(url) as response:
178191
shutil.copyfileobj(response, tmp_file)
179192
tmp_file.close()
@@ -186,7 +199,11 @@ def make_emscripten_libffi(context, working_dir):
186199
ver = "3.4.6"
187200
libffi_dir = working_dir / f"libffi-{ver}"
188201
shutil.rmtree(libffi_dir, ignore_errors=True)
189-
download_and_unpack(working_dir, f"https://github.com/libffi/libffi/releases/download/v{ver}/libffi-{ver}.tar.gz", "b0dea9df23c863a7a50e825440f3ebffabd65df1497108e5d437747843895a4e")
202+
download_and_unpack(
203+
working_dir,
204+
f"https://github.com/libffi/libffi/releases/download/v{ver}/libffi-{ver}.tar.gz",
205+
"b0dea9df23c863a7a50e825440f3ebffabd65df1497108e5d437747843895a4e",
206+
)
190207
call(
191208
[EMSCRIPTEN_DIR / "make_libffi.sh"],
192209
env=updated_env({"PREFIX": PREFIX_DIR}),
@@ -200,7 +217,11 @@ def make_mpdec(context, working_dir):
200217
ver = "4.0.1"
201218
mpdec_dir = working_dir / f"mpdecimal-{ver}"
202219
shutil.rmtree(mpdec_dir, ignore_errors=True)
203-
download_and_unpack(working_dir, f"https://www.bytereef.org/software/mpdecimal/releases/mpdecimal-{ver}.tar.gz", "96d33abb4bb0070c7be0fed4246cd38416188325f820468214471938545b1ac8")
220+
download_and_unpack(
221+
working_dir,
222+
f"https://www.bytereef.org/software/mpdecimal/releases/mpdecimal-{ver}.tar.gz",
223+
"96d33abb4bb0070c7be0fed4246cd38416188325f820468214471938545b1ac8",
224+
)
204225
call(
205226
[
206227
"emconfigure",
@@ -214,10 +235,7 @@ def make_mpdec(context, working_dir):
214235
quiet=context.quiet,
215236
)
216237
call(
217-
[
218-
"make",
219-
"install"
220-
],
238+
["make", "install"],
221239
cwd=mpdec_dir,
222240
quiet=context.quiet,
223241
)
@@ -226,17 +244,15 @@ def make_mpdec(context, working_dir):
226244
@subdir(HOST_DIR, clean_ok=True)
227245
def configure_emscripten_python(context, working_dir):
228246
"""Configure the emscripten/host build."""
229-
config_site = os.fsdecode(
230-
EMSCRIPTEN_DIR / "config.site-wasm32-emscripten"
231-
)
247+
config_site = os.fsdecode(EMSCRIPTEN_DIR / "config.site-wasm32-emscripten")
232248

233249
emscripten_build_dir = working_dir.relative_to(CHECKOUT)
234250

235251
python_build_dir = NATIVE_BUILD_DIR / "build"
236252
lib_dirs = list(python_build_dir.glob("lib.*"))
237-
assert (
238-
len(lib_dirs) == 1
239-
), f"Expected a single lib.* directory in {python_build_dir}"
253+
assert len(lib_dirs) == 1, (
254+
f"Expected a single lib.* directory in {python_build_dir}"
255+
)
240256
lib_dir = os.fsdecode(lib_dirs[0])
241257
pydebug = lib_dir.endswith("-pydebug")
242258
python_version = lib_dir.removesuffix("-pydebug").rpartition("-")[-1]
@@ -290,7 +306,9 @@ def configure_emscripten_python(context, working_dir):
290306
quiet=context.quiet,
291307
)
292308

293-
shutil.copy(EMSCRIPTEN_DIR / "node_entry.mjs", working_dir / "node_entry.mjs")
309+
shutil.copy(
310+
EMSCRIPTEN_DIR / "node_entry.mjs", working_dir / "node_entry.mjs"
311+
)
294312

295313
node_entry = working_dir / "node_entry.mjs"
296314
exec_script = working_dir / "python.sh"
@@ -383,13 +401,15 @@ def main():
383401
subcommands = parser.add_subparsers(dest="subcommand")
384402
build = subcommands.add_parser("build", help="Build everything")
385403
configure_build = subcommands.add_parser(
386-
"configure-build-python", help="Run `configure` for the " "build Python"
404+
"configure-build-python", help="Run `configure` for the build Python"
387405
)
388406
make_mpdec_cmd = subcommands.add_parser(
389-
"make-mpdec", help="Clone mpdec repo, configure and build it for emscripten"
407+
"make-mpdec",
408+
help="Clone mpdec repo, configure and build it for emscripten",
390409
)
391410
make_libffi_cmd = subcommands.add_parser(
392-
"make-libffi", help="Clone libffi repo, configure and build it for emscripten"
411+
"make-libffi",
412+
help="Clone libffi repo, configure and build it for emscripten",
393413
)
394414
make_build = subcommands.add_parser(
395415
"make-build-python", help="Run `make` for the build Python"
@@ -457,7 +477,11 @@ def main():
457477

458478
if not context.subcommand:
459479
# No command provided, display help and exit
460-
print("Expected one of", ", ".join(sorted(dispatch.keys())), file=sys.stderr)
480+
print(
481+
"Expected one of",
482+
", ".join(sorted(dispatch.keys())),
483+
file=sys.stderr,
484+
)
461485
parser.print_help(sys.stderr)
462486
sys.exit(1)
463487
dispatch[context.subcommand](context)

Tools/wasm/emscripten/prepare_external_wasm.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
}});
2020
"""
2121

22+
2223
def prepare_wasm(input_file, output_file, function_name):
2324
# Read the compiled WASM as binary and convert to hex
2425
wasm_bytes = Path(input_file).read_bytes()
@@ -31,9 +32,7 @@ def prepare_wasm(input_file, output_file, function_name):
3132
)
3233
Path(output_file).write_text(js_content)
3334

34-
print(
35-
f"Successfully compiled {input_file} and generated {output_file}"
36-
)
35+
print(f"Successfully compiled {input_file} and generated {output_file}")
3736
return 0
3837

3938

Tools/wasm/emscripten/wasm_assets.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
WASM_STDLIB_ZIP = (
2828
WASM_LIB / f"python{sys.version_info.major}{sys.version_info.minor}.zip"
2929
)
30-
WASM_STDLIB = WASM_LIB / f"python{sys.version_info.major}.{sys.version_info.minor}"
30+
WASM_STDLIB = (
31+
WASM_LIB / f"python{sys.version_info.major}.{sys.version_info.minor}"
32+
)
3133
WASM_DYNLOAD = WASM_STDLIB / "lib-dynload"
3234

3335

Tools/wasm/emscripten/web_example/server.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@
66
description="Start a local webserver with a Python terminal."
77
)
88
parser.add_argument(
9-
"--port", type=int, default=8000, help="port for the http server to listen on"
9+
"--port",
10+
type=int,
11+
default=8000,
12+
help="port for the http server to listen on",
1013
)
1114
parser.add_argument(
12-
"--bind", type=str, default="127.0.0.1", help="Bind address (empty for all)"
15+
"--bind",
16+
type=str,
17+
default="127.0.0.1",
18+
help="Bind address (empty for all)",
1319
)
1420

1521

Tools/wasm/wasi.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
if __name__ == "__main__":
1+
if __name__ == "__main__":
22
import pathlib
33
import runpy
44
import sys
55

6-
print("⚠️ WARNING: This script is deprecated and slated for removal in Python 3.20; "
7-
"execute the `wasi/` directory instead (i.e. `python Tools/wasm/wasi`)\n",
8-
file=sys.stderr)
6+
print(
7+
"⚠️ WARNING: This script is deprecated and slated for removal in Python 3.20; "
8+
"execute the `wasi/` directory instead (i.e. `python Tools/wasm/wasi`)\n",
9+
file=sys.stderr,
10+
)
911

1012
runpy.run_path(pathlib.Path(__file__).parent / "wasi", run_name="__main__")

0 commit comments

Comments
 (0)