Skip to content

Commit a71ffcc

Browse files
Port "Update pyodide py-compile command to accept list of files to exclude" (#9)
- pyodide/pyodide#4911 --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 0e4498a commit a71ffcc

File tree

4 files changed

+65
-21
lines changed

4 files changed

+65
-21
lines changed

pyodide_build/_py_compile.py

+17-18
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
from packaging.tags import Tag
1212
from packaging.utils import parse_wheel_filename
1313

14-
from pyodide_build.common import _get_sha256_checksum
15-
14+
from .common import _get_sha256_checksum
1615
from .logger import logger, set_log_level
1716

1817

@@ -236,11 +235,7 @@ def _get_py_compiled_archive_name(path: Path) -> str | None:
236235
>>> import re
237236
>>> re.sub("cp[0-9]*", "cpxxx", _get_py_compiled_archive_name(Path("snowballstemmer-2.2.0-py2.py3-none-any.whl")))
238237
'snowballstemmer-2.2.0-cpxxx-none-any.whl'
239-
>>> _get_py_compiled_archive_name(Path("test-1.0.0.zip"))
240238
"""
241-
# TODO: fix py-compilation of the following packages
242-
if path.name.startswith(("RobotRaconteur", "astropy-", "opencv_python-")):
243-
return None
244239

245240
if path.suffix == ".whl":
246241
try:
@@ -249,9 +244,6 @@ def _get_py_compiled_archive_name(path: Path) -> str | None:
249244
except Exception as e:
250245
print(e)
251246
return None
252-
elif path.name == "test-1.0.0.zip":
253-
# We don't want to py-compile the test package
254-
return None
255247
elif path.suffix == ".zip":
256248
# If it's a zip file with .py files, keep the same name
257249
with zipfile.ZipFile(path, "r") as zip_ref:
@@ -281,6 +273,7 @@ def _py_compile_archive_dir(
281273
keep: bool = True,
282274
verbose: bool = True,
283275
compression_level: int = 6,
276+
excludes: list[str] | None = None,
284277
) -> dict[str, str]:
285278
"""Py-compile all wheels or zip files in a directory.
286279
@@ -315,15 +308,21 @@ def _py_compile_archive_dir(
315308
for file_path in itertools.chain(
316309
*[input_dir.glob(ext) for ext in ["*.zip", "*.whl"]]
317310
):
318-
if (output_name := _get_py_compiled_archive_name(file_path)) is not None:
319-
_compile(
320-
file_path,
321-
file_path.parent / output_name,
322-
keep=keep,
323-
verbose=verbose,
324-
compression_level=compression_level,
325-
)
326-
name_mapping[file_path.name] = output_name
311+
if excludes and any(file_path.name.startswith(exclude) for exclude in excludes):
312+
continue
313+
314+
output_name = _get_py_compiled_archive_name(file_path)
315+
if output_name is None:
316+
continue
317+
318+
_compile(
319+
file_path,
320+
file_path.parent / output_name,
321+
keep=keep,
322+
verbose=verbose,
323+
compression_level=compression_level,
324+
)
325+
name_mapping[file_path.name] = output_name
327326

328327
lockfile_path = input_dir / "pyodide-lock.json"
329328
if name_mapping and lockfile_path.exists():

pyodide_build/cli/py_compile.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import re
12
from pathlib import Path
23

34
import typer
45

5-
from pyodide_build._py_compile import _py_compile_archive, _py_compile_archive_dir
6+
from .._py_compile import _py_compile_archive, _py_compile_archive_dir
67

78

89
def main(
@@ -14,6 +15,10 @@ def main(
1415
compression_level: int = typer.Option(
1516
6, help="Compression level to use for the created zip file"
1617
),
18+
exclude: str = typer.Option(
19+
"",
20+
help="List of files to exclude from compilation, works only for directories. Defaults to no files.",
21+
),
1722
) -> None:
1823
"""Compile .py files to .pyc in a wheel, a zip file, or a folder with wheels or zip files.
1924
@@ -24,6 +29,11 @@ def main(
2429
typer.echo(f"Error: {path} does not exist")
2530
raise typer.Exit(1)
2631

32+
# Convert the comma / space separated strings to lists
33+
excludes = [
34+
item.strip() for item in re.split(r",|\s", exclude) if item.strip() != ""
35+
]
36+
2737
if path.is_file():
2838
if path.suffix not in [".whl", ".zip"]:
2939
typer.echo(
@@ -36,7 +46,11 @@ def main(
3646
)
3747
elif path.is_dir():
3848
_py_compile_archive_dir(
39-
path, verbose=not silent, keep=keep, compression_level=compression_level
49+
path,
50+
verbose=not silent,
51+
keep=keep,
52+
compression_level=compression_level,
53+
excludes=excludes,
4054
)
4155
else:
4256
typer.echo(f"{path=} is not a file or a directory")

pyodide_build/tests/test_cli.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,11 @@ def test_py_compile(tmp_path, target, compression_level):
333333
target_path = wheel_path
334334

335335
py_compile.main(
336-
path=target_path, silent=False, keep=False, compression_level=compression_level
336+
path=target_path,
337+
silent=False,
338+
keep=False,
339+
compression_level=compression_level,
340+
exclude="",
337341
)
338342
with zipfile.ZipFile(tmp_path / "python.zip", "r") as fh:
339343
if compression_level > 0:

pyodide_build/tests/test_py_compile.py

+27
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,30 @@ def test_py_compile_archive_dir(tmp_path, with_lockfile):
248248
"file_name": "some-path.tar",
249249
"checksum": "123",
250250
}
251+
252+
253+
@pytest.mark.parametrize("with_lockfile", [True, False])
254+
def test_py_compile_archive_dir_excludes(tmp_path, with_lockfile):
255+
wheel_data = {
256+
"a.so": "abc",
257+
"b.txt": "123",
258+
"METADATA": "a",
259+
"packageB/a.py": "1+1",
260+
}
261+
262+
_create_tmp_wheel(
263+
"packageB", base_dir=tmp_path, data=wheel_data, tag="py3-none-any"
264+
)
265+
266+
excludes = ["packageB-"]
267+
mapping = _py_compile_archive_dir(tmp_path, keep=True, excludes=excludes)
268+
269+
assert mapping == {}
270+
271+
mapping2 = _py_compile_archive_dir(tmp_path, keep=False)
272+
273+
ver = sys.version_info
274+
cpver = f"cp{ver.major}{ver.minor}"
275+
assert mapping2 == {
276+
"packageB-0.1.0-py3-none-any.whl": f"packageb-0.1.0-{cpver}-none-any.whl",
277+
}

0 commit comments

Comments
 (0)