1""" 2Extraction and file list generation for pip. 3""" 4 5__author__ = "Steve Dower <steve.dower@python.org>" 6__version__ = "3.8" 7 8 9import os 10import shutil 11import subprocess 12import sys 13 14from .filesets import * 15 16__all__ = ["extract_pip_files", "get_pip_layout"] 17 18 19def get_pip_dir(ns): 20 if ns.copy: 21 if ns.zip_lib: 22 return ns.copy / "packages" 23 return ns.copy / "Lib" / "site-packages" 24 else: 25 return ns.temp / "packages" 26 27 28def get_pip_layout(ns): 29 pip_dir = get_pip_dir(ns) 30 if not pip_dir.is_dir(): 31 log_warning("Failed to find {} - pip will not be included", pip_dir) 32 else: 33 pkg_root = "packages/{}" if ns.zip_lib else "Lib/site-packages/{}" 34 for dest, src in rglob(pip_dir, "**/*"): 35 yield pkg_root.format(dest), src 36 if ns.include_pip_user: 37 content = "\n".join( 38 "[{}]\nuser=yes".format(n) 39 for n in ["install", "uninstall", "freeze", "list"] 40 ) 41 yield "pip.ini", ("pip.ini", content.encode()) 42 43 44def extract_pip_files(ns): 45 dest = get_pip_dir(ns) 46 try: 47 dest.mkdir(parents=True, exist_ok=False) 48 except IOError: 49 return 50 51 src = ns.source / "Lib" / "ensurepip" / "_bundled" 52 53 ns.temp.mkdir(parents=True, exist_ok=True) 54 wheels = [shutil.copy(whl, ns.temp) for whl in src.glob("*.whl")] 55 search_path = os.pathsep.join(wheels) 56 if os.environ.get("PYTHONPATH"): 57 search_path += ";" + os.environ["PYTHONPATH"] 58 59 env = os.environ.copy() 60 env["PYTHONPATH"] = search_path 61 62 output = subprocess.check_output( 63 [ 64 sys.executable, 65 "-m", 66 "pip", 67 "--no-color", 68 "install", 69 "pip", 70 "setuptools", 71 "--upgrade", 72 "--target", 73 str(dest), 74 "--no-index", 75 "--no-compile", 76 "--no-cache-dir", 77 "-f", 78 str(src), 79 "--only-binary", 80 ":all:", 81 ], 82 env=env, 83 ) 84 85 try: 86 shutil.rmtree(dest / "bin") 87 except OSError: 88 pass 89 90 for file in wheels: 91 try: 92 os.remove(file) 93 except OSError: 94 pass 95