|
| 1 | +# Copyright 2024 Pex project contributors. |
| 2 | +# Licensed under the Apache License, Version 2.0 (see LICENSE). |
| 3 | + |
| 4 | +from __future__ import absolute_import |
| 5 | + |
| 6 | +from argparse import Namespace, _ActionsContainer |
| 7 | + |
| 8 | +from pex.fetcher import URLFetcher |
| 9 | +from pex.orderedset import OrderedSet |
| 10 | +from pex.pep_440 import Version |
| 11 | +from pex.scie import science |
| 12 | +from pex.scie.model import ( |
| 13 | + ScieConfiguration, |
| 14 | + ScieInfo, |
| 15 | + ScieOptions, |
| 16 | + SciePlatform, |
| 17 | + ScieStyle, |
| 18 | + ScieTarget, |
| 19 | +) |
| 20 | +from pex.typing import TYPE_CHECKING, cast |
| 21 | +from pex.variables import ENV, Variables |
| 22 | + |
| 23 | +if TYPE_CHECKING: |
| 24 | + from typing import Iterator, Optional, Tuple, Union |
| 25 | + |
| 26 | + |
| 27 | +__all__ = ( |
| 28 | + "ScieConfiguration", |
| 29 | + "ScieInfo", |
| 30 | + "SciePlatform", |
| 31 | + "ScieStyle", |
| 32 | + "ScieTarget", |
| 33 | + "build", |
| 34 | +) |
| 35 | + |
| 36 | + |
| 37 | +def register_options(parser): |
| 38 | + # type: (_ActionsContainer) -> None |
| 39 | + |
| 40 | + parser.add_argument( |
| 41 | + "--scie", |
| 42 | + dest="scie_style", |
| 43 | + default=None, |
| 44 | + type=ScieStyle.for_value, |
| 45 | + choices=ScieStyle.values(), |
| 46 | + help=( |
| 47 | + "Create one or more native executable scies from your PEX that include a portable " |
| 48 | + "CPython interpreter along with your PEX making for a truly hermetic PEX that can run " |
| 49 | + "on machines with no Python installed at all. If your PEX has multiple targets, " |
| 50 | + "whether `--platform`s, `--complete-platform`s or local interpreters in any " |
| 51 | + "combination, then one PEX scie will be made for each platform, selecting the latest " |
| 52 | + "compatible portable CPython interpreter. Note that only CPython>=3.8 is supported. If " |
| 53 | + "you'd like to explicitly control the target platforms or the exact portable CPython " |
| 54 | + "selected, see `--scie-platform`, `--scie-pbs-release` and `--scie-python-version`. " |
| 55 | + "Specifying `--scie {lazy}` will fetch the portable CPython interpreter just in time " |
| 56 | + "on first boot of the PEX scie on a given machine if needed. The URL(s) to fetch the " |
| 57 | + "portable CPython interpreter from can be customized by exporting the " |
| 58 | + "PEX_BOOTSTRAP_URLS environment variable pointing to a json file with the format: " |
| 59 | + '`{{"ptex": {{<file name 1>: <url>, ...}}}}` where the file names should match those ' |
| 60 | + "found via `SCIE=inspect <the PEX scie> | jq .ptex` with appropriate replacement URLs. " |
| 61 | + "Specifying `--scie {eager}` will embed the portable CPython interpreter in your PEX " |
| 62 | + "scie making for a larger file, but requiring no internet access to boot. If you have " |
| 63 | + "customization needs not addressed by the Pex `--scie*` options, consider using " |
| 64 | + "`science` to build your scies (which is what Pex uses behind the scenes); see: " |
| 65 | + "https://science.scie.app.".format(lazy=ScieStyle.LAZY, eager=ScieStyle.EAGER) |
| 66 | + ), |
| 67 | + ) |
| 68 | + parser.add_argument( |
| 69 | + "--scie-platform", |
| 70 | + dest="scie_platforms", |
| 71 | + default=[], |
| 72 | + action="append", |
| 73 | + type=SciePlatform.for_value, |
| 74 | + choices=SciePlatform.values(), |
| 75 | + help=( |
| 76 | + "The platform to produce the native PEX scie executable for. Can be specified multiple " |
| 77 | + "times." |
| 78 | + ), |
| 79 | + ) |
| 80 | + parser.add_argument( |
| 81 | + "--scie-pbs-release", |
| 82 | + dest="scie_pbs_release", |
| 83 | + default=None, |
| 84 | + type=str, |
| 85 | + help=( |
| 86 | + "The Python Standalone Builds release to use. Currently releases are dates of the form " |
| 87 | + "YYYYMMDD, e.g.: '20240713'. See their GitHub releases page at " |
| 88 | + "https://github.com/indygreg/python-build-standalone/releases to discover available " |
| 89 | + "releases. If left unspecified the latest release is used. N.B.: The latest lookup is " |
| 90 | + "cached for 5 days. To force a fresh lookup you can remove the cache at " |
| 91 | + "<USER CACHE DIR>/science/downloads." |
| 92 | + ), |
| 93 | + ) |
| 94 | + parser.add_argument( |
| 95 | + "--scie-python-version", |
| 96 | + dest="scie_python_version", |
| 97 | + default=None, |
| 98 | + type=Version, |
| 99 | + help=( |
| 100 | + "The portable CPython version to select. Can be either in `<major>.<minor>` form; " |
| 101 | + "e.g.: '3.11', or else fully specified as `<major>.<minor>.<patch>`; e.g.: '3.11.3'. " |
| 102 | + "If you don't specify this option, Pex will do its best to guess appropriate portable " |
| 103 | + "CPython versions. N.B.: Python Standalone Builds does not provide all patch versions; " |
| 104 | + "so you should check their releases at " |
| 105 | + "https://github.com/indygreg/python-build-standalone/releases if you wish to pin down " |
| 106 | + "to the patch level." |
| 107 | + ), |
| 108 | + ) |
| 109 | + |
| 110 | + |
| 111 | +def render_options(options): |
| 112 | + # type: (ScieOptions) -> str |
| 113 | + |
| 114 | + args = ["--scie", str(options.style)] |
| 115 | + for platform in options.platforms: |
| 116 | + args.append("--scie-platform") |
| 117 | + args.append(str(platform)) |
| 118 | + if options.pbs_release: |
| 119 | + args.append("--scie-pbs-release") |
| 120 | + args.append(options.pbs_release) |
| 121 | + if options.python_version: |
| 122 | + args.append("--scie-python-version") |
| 123 | + args.append(".".join(map(str, options.python_version))) |
| 124 | + return " ".join(args) |
| 125 | + |
| 126 | + |
| 127 | +def extract_options(options): |
| 128 | + # type: (Namespace) -> Optional[ScieOptions] |
| 129 | + |
| 130 | + if not options.scie_style: |
| 131 | + return None |
| 132 | + |
| 133 | + python_version = None # type: Optional[Union[Tuple[int, int], Tuple[int, int, int]]] |
| 134 | + if options.scie_python_version: |
| 135 | + if ( |
| 136 | + not options.scie_python_version.parsed_version.release |
| 137 | + or len(options.scie_python_version.parsed_version.release) < 2 |
| 138 | + ): |
| 139 | + raise ValueError( |
| 140 | + "Invalid Python version: '{python_version}'.\n" |
| 141 | + "Must be in the form `<major>.<minor>` or `<major>.<minor>.<release>`".format( |
| 142 | + python_version=options.scie_python_version |
| 143 | + ) |
| 144 | + ) |
| 145 | + python_version = cast( |
| 146 | + "Union[Tuple[int, int], Tuple[int, int, int]]", |
| 147 | + options.scie_python_version.parsed_version.release, |
| 148 | + ) |
| 149 | + if python_version < (3, 8): |
| 150 | + raise ValueError( |
| 151 | + "Invalid Python version: '{python_version}'.\n" |
| 152 | + "Scies are built using Python Standalone Builds which only supports Python >=3.8.\n" |
| 153 | + "To find supported Python versions, you can browse the releases here:\n" |
| 154 | + " https://github.com/indygreg/python-build-standalone/releases".format( |
| 155 | + python_version=options.scie_python_version |
| 156 | + ) |
| 157 | + ) |
| 158 | + |
| 159 | + return ScieOptions( |
| 160 | + style=options.scie_style, |
| 161 | + platforms=tuple(OrderedSet(options.scie_platforms)), |
| 162 | + pbs_release=options.scie_pbs_release, |
| 163 | + python_version=python_version, |
| 164 | + ) |
| 165 | + |
| 166 | + |
| 167 | +def build( |
| 168 | + configuration, # type: ScieConfiguration |
| 169 | + pex_file, # type: str |
| 170 | + url_fetcher=None, # type: Optional[URLFetcher] |
| 171 | + env=ENV, # type: Variables |
| 172 | +): |
| 173 | + # type: (...) -> Iterator[ScieInfo] |
| 174 | + |
| 175 | + return science.build(configuration, pex_file, url_fetcher=url_fetcher, env=env) |
0 commit comments