| 1 |
"""Installer CLI.""" |
| 2 |
|
| 3 |
import argparse |
| 4 |
import os.path |
| 5 |
import sys |
| 6 |
import sysconfig |
| 7 |
from collections.abc import Sequence |
| 8 |
|
| 9 |
import installer |
| 10 |
from installer.destinations import SchemeDictionaryDestination |
| 11 |
from installer.sources import WheelFile |
| 12 |
from installer.utils import get_launcher_kind |
| 13 |
|
| 14 |
|
| 15 |
def _get_main_parser() -> argparse.ArgumentParser: |
| 16 |
"""Construct the main parser.""" |
| 17 |
parser = argparse.ArgumentParser() |
| 18 |
parser.add_argument("wheel", type=str, nargs="+", help="wheel file to install") |
| 19 |
parser.add_argument( |
| 20 |
"--destdir", |
| 21 |
"-d", |
| 22 |
metavar="path", |
| 23 |
type=str, |
| 24 |
help="destination directory (prefix to prepend to each file)", |
| 25 |
) |
| 26 |
parser.add_argument( |
| 27 |
"--prefix", |
| 28 |
"-p", |
| 29 |
metavar="path", |
| 30 |
type=str, |
| 31 |
help="override prefix to install packages to", |
| 32 |
) |
| 33 |
parser.add_argument( |
| 34 |
"--compile-bytecode", |
| 35 |
action="append", |
| 36 |
metavar="level", |
| 37 |
type=int, |
| 38 |
choices=[0, 1, 2], |
| 39 |
help="generate bytecode for the specified optimization level(s) (default=0, 1)", |
| 40 |
) |
| 41 |
parser.add_argument( |
| 42 |
"--no-compile-bytecode", |
| 43 |
action="store_true", |
| 44 |
help="don't generate bytecode for installed modules", |
| 45 |
) |
| 46 |
parser.add_argument( |
| 47 |
"--validate-record", |
| 48 |
metavar="part", |
| 49 |
default="none", |
| 50 |
type=str, |
| 51 |
choices=["all", "entries", "none"], |
| 52 |
help="validate the wheel against certain part of its record (default=none)", |
| 53 |
) |
| 54 |
parser.add_argument( |
| 55 |
"--overwrite-existing", |
| 56 |
action="store_true", |
| 57 |
help="silently overwrite existing files", |
| 58 |
) |
| 59 |
return parser |
| 60 |
|
| 61 |
|
| 62 |
def _get_scheme_dict( |
| 63 |
distribution_name: str, prefix: str | None = None |
| 64 |
) -> dict[str, str]: |
| 65 |
"""Calculate the scheme dictionary for the current Python environment.""" |
| 66 |
vars = {} |
| 67 |
if prefix is None: |
| 68 |
installed_base = sysconfig.get_config_var("base") |
| 69 |
assert installed_base |
| 70 |
else: |
| 71 |
vars["base"] = vars["platbase"] = installed_base = prefix |
| 72 |
|
| 73 |
scheme_dict = sysconfig.get_paths(vars=vars) |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
scheme_dict["headers"] = os.path.join( |
| 79 |
sysconfig.get_path("include", vars={"installed_base": installed_base}), |
| 80 |
distribution_name, |
| 81 |
) |
| 82 |
|
| 83 |
return scheme_dict |
| 84 |
|
| 85 |
|
| 86 |
def _main(cli_args: Sequence[str], program: str | None = None) -> None: |
| 87 |
"""Process arguments and perform the install.""" |
| 88 |
parser = _get_main_parser() |
| 89 |
if program: |
| 90 |
parser.prog = program |
| 91 |
args = parser.parse_args(cli_args) |
| 92 |
|
| 93 |
bytecode_levels = args.compile_bytecode |
| 94 |
if args.no_compile_bytecode: |
| 95 |
bytecode_levels = [] |
| 96 |
elif not bytecode_levels: |
| 97 |
bytecode_levels = [0, 1] |
| 98 |
|
| 99 |
for wheel in args.wheel: |
| 100 |
with WheelFile.open(wheel) as source: |
| 101 |
if args.validate_record != "none": |
| 102 |
source.validate_record(validate_contents=args.validate_record == "all") |
| 103 |
destination = SchemeDictionaryDestination( |
| 104 |
scheme_dict=_get_scheme_dict(source.distribution, prefix=args.prefix), |
| 105 |
interpreter=sys.executable, |
| 106 |
script_kind=get_launcher_kind(), |
| 107 |
bytecode_optimization_levels=bytecode_levels, |
| 108 |
destdir=args.destdir, |
| 109 |
overwrite_existing=args.overwrite_existing, |
| 110 |
) |
| 111 |
installer.install(source, destination, {}) |
| 112 |
|
| 113 |
|
| 114 |
if __name__ == "__main__": |
| 115 |
_main(sys.argv[1:], "python -m installer") |