20
20
Union ,
21
21
)
22
22
from unittest .mock import patch
23
+ from zipfile import ZipFile
23
24
24
25
import pytest
25
26
32
33
from _pytest .config .argparsing import Parser
33
34
from setuptools .wheel import Wheel
34
35
36
+ from pip import __file__ as pip_location
35
37
from pip ._internal .cli .main import main as pip_entry_point
36
38
from pip ._internal .locations import _USE_SYSCONFIG
37
39
from pip ._internal .utils .temp_dir import global_tempdir_manager
@@ -529,6 +531,35 @@ def factory(
529
531
return factory
530
532
531
533
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
+
532
563
@pytest .fixture (scope = "session" )
533
564
def zipapp (request : pytest .FixtureRequest , tmpdir_factory : pytest .TempPathFactory ) -> Optional [str ]:
534
565
"""
@@ -542,13 +573,7 @@ def zipapp(request: pytest.FixtureRequest, tmpdir_factory: pytest.TempPathFactor
542
573
543
574
temp_location = tmpdir_factory .mktemp ("zipapp" )
544
575
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 )
552
577
return str (pyz_file )
553
578
554
579
0 commit comments