Skip to content

Commit

Permalink
Prepare the codebase for hooking it up as a rez plugin
Browse files Browse the repository at this point in the history
Signed-off-by: Jean-Christophe Morin <[email protected]>
  • Loading branch information
JeanChristopheMorinPerso committed Feb 15, 2025
1 parent e039ce0 commit 56d7ae0
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 15 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions src/rez_pip/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# SPDX-FileCopyrightText: 2022 Contributors to the rez project
#
# SPDX-License-Identifier: Apache-2.0

"""rez-pip2"""

import argparse

import rez.command


command_behavior = {
"hidden": False, # optional: bool
"arg_mode": "grouped", # optional: None, "passthrough", "grouped"
}


def setup_parser(parser: argparse.ArgumentParser, completions=False):
import rez_pip.cli

rez_pip.cli._setupParser(parser, fromRez=True)


def command(opts, parser=None, extra_arg_groups=None) -> int:
import rez_pip.cli

return rez_pip.cli.run(args=opts, pipArgs=extra_arg_groups)


class RezPip(rez.command.Command):
"""asd"""


def register_plugin():
return RezPip
52 changes: 37 additions & 15 deletions src/rez_pip/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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__),
)
Expand Down Expand Up @@ -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]]:
Expand Down Expand Up @@ -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(), " "))
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 56d7ae0

Please sign in to comment.