Skip to content

Commit b84e5f3

Browse files
committed
Actually build the zipapp
1 parent ccb2566 commit b84e5f3

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

tests/conftest.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
Union,
2121
)
2222
from unittest.mock import patch
23+
from zipfile import ZipFile
2324

2425
import pytest
2526

@@ -32,6 +33,7 @@
3233
from _pytest.config.argparsing import Parser
3334
from setuptools.wheel import Wheel
3435

36+
from pip import __file__ as pip_location
3537
from pip._internal.cli.main import main as pip_entry_point
3638
from pip._internal.locations import _USE_SYSCONFIG
3739
from pip._internal.utils.temp_dir import global_tempdir_manager
@@ -529,6 +531,35 @@ def factory(
529531
return factory
530532

531533

534+
ZIPAPP_MAIN = """\
535+
#!/usr/bin/env python
536+
537+
import os
538+
import runpy
539+
import sys
540+
541+
lib = os.path.join(os.path.dirname(__file__), "lib")
542+
sys.path.insert(0, lib)
543+
544+
runpy.run_module("pip", run_name="__main__")
545+
"""
546+
547+
def make_zipapp_from_pip(zipapp_name: Path) -> None:
548+
pip_dir = Path(pip_location).parent
549+
with zipapp_name.open("wb") as zipapp_file:
550+
zipapp_file.write(b"#!/usr/bin/env python\n")
551+
with ZipFile(zipapp_file, "w") as zipapp:
552+
for pip_file in pip_dir.rglob("*"):
553+
if pip_file.suffix == ".pyc":
554+
continue
555+
if pip_file.name == "__pycache__":
556+
continue
557+
rel_name = pip_file.relative_to(pip_dir.parent)
558+
zipapp.write(pip_file, arcname=f"lib/{rel_name}")
559+
zipapp.writestr("__main__.py", ZIPAPP_MAIN)
560+
561+
562+
532563
@pytest.fixture(scope="session")
533564
def zipapp(request: pytest.FixtureRequest, tmpdir_factory: pytest.TempPathFactory) -> Optional[str]:
534565
"""
@@ -542,13 +573,7 @@ def zipapp(request: pytest.FixtureRequest, tmpdir_factory: pytest.TempPathFactor
542573

543574
temp_location = tmpdir_factory.mktemp("zipapp")
544575
pyz_file = temp_location / "pip.pyz"
545-
# What we want to do here is `pip wheel --wheel-dir temp_location <source_dir>`
546-
# and then build a zipapp from that wheel.
547-
# TODO: Remove hard coded file
548-
za = "pip-22.2.dev0.pyz"
549-
import warnings
550-
warnings.warn(f"Copying {za} to {pyz_file}")
551-
shutil.copyfile(za, pyz_file)
576+
make_zipapp_from_pip(pyz_file)
552577
return str(pyz_file)
553578

554579

0 commit comments

Comments
 (0)