| 1 |
""" |
| 2 |
Extraction and file list generation for pip. |
| 3 |
""" |
| 4 |
|
| 5 |
__author__ = "Steve Dower <[email protected]>" |
| 6 |
__version__ = "3.8" |
| 7 |
|
| 8 |
|
| 9 |
import os |
| 10 |
import shutil |
| 11 |
import subprocess |
| 12 |
import sys |
| 13 |
|
| 14 |
from .filesets import * |
| 15 |
|
| 16 |
__all__ = ["extract_pip_files", "get_pip_layout"] |
| 17 |
|
| 18 |
|
| 19 |
def 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 |
|
| 28 |
def 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 |
|
| 44 |
def 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 |
"--upgrade", |
| 71 |
"--target", |
| 72 |
str(dest), |
| 73 |
"--no-index", |
| 74 |
"--no-compile", |
| 75 |
"--no-cache-dir", |
| 76 |
"-f", |
| 77 |
str(src), |
| 78 |
"--only-binary", |
| 79 |
":all:", |
| 80 |
], |
| 81 |
env=env, |
| 82 |
) |
| 83 |
|
| 84 |
try: |
| 85 |
shutil.rmtree(dest / "bin") |
| 86 |
except OSError: |
| 87 |
pass |
| 88 |
|
| 89 |
for file in wheels: |
| 90 |
try: |
| 91 |
os.remove(file) |
| 92 |
except OSError: |
| 93 |
pass |