diff --git a/pyproject.toml b/pyproject.toml index 10ddee1..b395c51 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -53,6 +53,9 @@ Documentation = "https://rez-pip.readthedocs.io" [project.scripts] rez-pip2 = "rez_pip.cli:run" +[project.entry-points."rez.plugins.command"] +pip2 = "rez_pip" + [tool.hatch.build.targets.sdist] # Ensure the sdist includes a setup.py for older pip versions # support-legacy = true diff --git a/src/rez_pip/__init__.py b/src/rez_pip/__init__.py index e69de29..dee710f 100644 --- a/src/rez_pip/__init__.py +++ b/src/rez_pip/__init__.py @@ -0,0 +1,42 @@ +# SPDX-FileCopyrightText: 2022 Contributors to the rez project +# +# SPDX-License-Identifier: Apache-2.0 + +"""rez-pip2""" + +from __future__ import annotations + +import argparse + + +command_behavior = { + "hidden": False, # optional: bool + "arg_mode": "grouped", # optional: None, "passthrough", "grouped" +} + + +def setup_parser(parser: argparse.ArgumentParser) -> None: + import rez_pip.cli + + rez_pip.cli._setupParser(parser, fromRez=True) + + +def command( + opts: argparse.Namespace, + _: argparse.ArgumentParser, + extra_arg_groups: list[list[str]], +) -> int: + import rez_pip.cli + + pipArgs = [arg for group in extra_arg_groups for arg in group] + return rez_pip.cli.run(args=opts, pipArgs=pipArgs) + + +def register_plugin(): # type: ignore + # Defined here to avoid cirlular imports + import rez.command + + class RezPip(rez.command.Command): # type: ignore + """asd""" + + return RezPip diff --git a/src/rez_pip/cli.py b/src/rez_pip/cli.py index 766b778..476da54 100644 --- a/src/rez_pip/cli.py +++ b/src/rez_pip/cli.py @@ -42,12 +42,9 @@ def __dir__() -> list[str]: return __all__ -def _createParser() -> argparse.ArgumentParser: - parser = argparse.ArgumentParser( - description="Ingest and convert python packages to rez packages.", - prog=__package__.replace("_", "-"), - add_help=False, - ) +def _setupParser( + parser: argparse.ArgumentParser, fromRez: bool = False +) -> argparse.ArgumentParser: parser.add_argument("packages", nargs="*", help="Packages to install.") generalGroup = parser.add_argument_group(title="general options") @@ -91,14 +88,16 @@ def _createParser() -> argparse.ArgumentParser: help="Standalone pip (https://pip.pypa.io/en/stable/installation/#standalone-zip-application) (default: bundled).", ) - # Manually define just to keep the style consistent (capital letters, dot, etc.) - generalGroup.add_argument( - "-h", "--help", action="help", help="Show this help message and exit." - ) + # Rez injects --version and --help... So we have to + # avoid setting them when run from rez. + if not fromRez: + # Manually define just to keep the style consistent (capital letters, dot, etc.) + generalGroup.add_argument( + "-h", "--help", action="help", help="Show this help message and exit." + ) generalGroup.add_argument( - "-v", - "--version", + "--plugin-version", action="version", version=importlib_metadata.version(__package__), ) @@ -136,6 +135,15 @@ def _createParser() -> argparse.ArgumentParser: return parser +def _createParser() -> argparse.ArgumentParser: + parser = argparse.ArgumentParser( + description="Ingest and convert python packages to rez packages.", + prog=__package__.replace("_", "-"), + add_help=False, + ) + return _setupParser(parser) + + def _parseArgs( args: list[str], ) -> tuple[argparse.Namespace, list[str]]: @@ -313,8 +321,15 @@ def _debug( text=True, ) + opts = vars(args) + + # Get rid of non serializable values injected by rez. + opts.pop("parser", None) + opts.pop("func", None) + opts.pop("formatter_class", None) + console.print("[bold]rez-pip provided arguments[/]:") - print(textwrap.indent(json.dumps(vars(args), indent=4), prefix)) + print(textwrap.indent(json.dumps(opts, indent=4), prefix)) console.print(f"[bold]pip config debug[/]:", highlight=False) print(textwrap.indent(completedProcess.stdout.strip(), " ")) @@ -361,9 +376,16 @@ def _printPlugins() -> None: rez_pip.utils.CONSOLE.print(table) -def run() -> int: +def run( + args: argparse.Namespace | None = None, pipArgs: list[str] | None = None +) -> int: pipWorkArea = tempfile.mkdtemp(prefix="rez-pip-target") - args, pipArgs = _parseArgs(sys.argv[1:]) + + if args is None: + args, pipArgs = _parseArgs(sys.argv[1:]) + + assert args is not None + assert pipArgs is not None # Initialize the plugin system rez_pip.plugins.getManager()