Debian Codegraph @

src/pbs_installer/__main__.py

raw
1 from __future__ import annotations
2
3 import logging
4 from argparse import SUPPRESS, Action, ArgumentParser, Namespace
5 from collections.abc import Sequence
6 from typing import Any
7
8 from ._install import install
9 from ._utils import get_available_arch_platforms
10
11
12 def _setup_logger(verbose: bool) -> None:
13 logger = logging.getLogger("pbs_installer")
14 handler = logging.StreamHandler()
15 handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
16 logger.addHandler(handler)
17 logger.setLevel(logging.DEBUG if verbose else logging.WARNING)
18
19
20 class ListAction(Action):
21 def __init__(
22 self,
23 option_strings: Sequence[str],
24 dest: str = SUPPRESS,
25 default: Any = SUPPRESS,
26 help: str | None = None,
27 ) -> None:
28 super().__init__(
29 option_strings=option_strings, dest=dest, nargs=0, default=default, help=help
30 )
31
32 def __call__(
33 self,
34 parser: ArgumentParser,
35 namespace: Namespace,
36 values: str | Sequence[Any] | None,
37 option_string: str | None = None,
38 ) -> None:
39 self.list_versions()
40 parser.exit()
41
42 def list_versions(self) -> None:
43 from ._versions import PYTHON_VERSIONS
44
45 for version in PYTHON_VERSIONS:
46 print(f"- {version}")
47
48
49 def main() -> None:
50 archs, platforms = get_available_arch_platforms()
51 parser = ArgumentParser("pbs-install", description="Installer for Python Build Standalone")
52 install_group = parser.add_argument_group("Install Arguments")
53 install_group.add_argument(
54 "version", help="The version of Python to install, e.g. 3.14, 3.10.4, [email protected]"
55 )
56 install_group.add_argument(
57 "--version-dir", help="Install to a subdirectory named by the version", action="store_true"
58 )
59 install_group.add_argument(
60 "--build-dir", help="Include the build directory", action="store_true"
61 )
62 install_group.add_argument(
63 "-d", "--destination", help="The directory to install to", required=True
64 )
65 install_group.add_argument("--arch", choices=archs, help="Override the architecture to install")
66 install_group.add_argument(
67 "--platform", choices=platforms, help="Override the platform to install"
68 )
69 parser.add_argument("-v", "--verbose", help="Enable verbose logging", action="store_true")
70 parser.add_argument("-l", "--list", action=ListAction, help="List installable versions")
71
72 args = parser.parse_args()
73 _setup_logger(args.verbose)
74 impl, has_amp, version = args.version.rpartition("@")
75 if not has_amp:
76 impl = "cpython"
77 install(
78 version,
79 args.destination,
80 version_dir=args.version_dir,
81 arch=args.arch,
82 platform=args.platform,
83 implementation=impl,
84 build_dir=args.build_dir,
85 )
86 print("Done!")
87
88
89 if __name__ == "__main__":
90 main()