Skip to content

Commit 8072f28

Browse files
Prepare the codebase for hooking it up as a rez plugin
Signed-off-by: Jean-Christophe Morin <jean_christophe_morin@hotmail.com>
1 parent e039ce0 commit 8072f28

3 files changed

Lines changed: 72 additions & 16 deletions

File tree

‎pyproject.toml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ Documentation = "https://rez-pip.readthedocs.io"
5353
[project.scripts]
5454
rez-pip2 = "rez_pip.cli:run"
5555

56+
[project.entry-points."rez.plugins.command"]
57+
pip2 = "rez_pip"
58+
5659
[tool.hatch.build.targets.sdist]
5760
# Ensure the sdist includes a setup.py for older pip versions
5861
# support-legacy = true

‎src/rez_pip/__init__.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# SPDX-FileCopyrightText: 2022 Contributors to the rez project
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
"""rez-pip2"""
6+
7+
import argparse
8+
9+
import rez.command
10+
11+
12+
command_behavior = {
13+
"hidden": False, # optional: bool
14+
"arg_mode": "grouped", # optional: None, "passthrough", "grouped"
15+
}
16+
17+
18+
def setup_parser(parser: argparse.ArgumentParser, completions=False):
19+
import rez_pip.cli
20+
21+
rez_pip.cli._setupParser(parser, fromRez=True)
22+
23+
24+
def command(opts, parser=None, extra_arg_groups=None) -> int:
25+
import rez_pip.cli
26+
27+
return rez_pip.cli.run(args=opts, pipArgs=extra_arg_groups)
28+
29+
30+
class RezPip(rez.command.Command):
31+
"""asd"""
32+
33+
34+
def register_plugin():
35+
return RezPip

‎src/rez_pip/cli.py‎

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,9 @@ def __dir__() -> list[str]:
4242
return __all__
4343

4444

45-
def _createParser() -> argparse.ArgumentParser:
46-
parser = argparse.ArgumentParser(
47-
description="Ingest and convert python packages to rez packages.",
48-
prog=__package__.replace("_", "-"),
49-
add_help=False,
50-
)
45+
def _setupParser(
46+
parser: argparse.ArgumentParser, fromRez: bool = False
47+
) -> argparse.ArgumentParser:
5148
parser.add_argument("packages", nargs="*", help="Packages to install.")
5249

5350
generalGroup = parser.add_argument_group(title="general options")
@@ -91,14 +88,16 @@ def _createParser() -> argparse.ArgumentParser:
9188
help="Standalone pip (https://pip.pypa.io/en/stable/installation/#standalone-zip-application) (default: bundled).",
9289
)
9390

94-
# Manually define just to keep the style consistent (capital letters, dot, etc.)
95-
generalGroup.add_argument(
96-
"-h", "--help", action="help", help="Show this help message and exit."
97-
)
91+
# Rez injects --version and --help... So we have to
92+
# avoid setting them when run from rez.
93+
if not fromRez:
94+
# Manually define just to keep the style consistent (capital letters, dot, etc.)
95+
generalGroup.add_argument(
96+
"-h", "--help", action="help", help="Show this help message and exit."
97+
)
9898

9999
generalGroup.add_argument(
100-
"-v",
101-
"--version",
100+
"--plugin-version",
102101
action="version",
103102
version=importlib_metadata.version(__package__),
104103
)
@@ -139,7 +138,12 @@ def _createParser() -> argparse.ArgumentParser:
139138
def _parseArgs(
140139
args: list[str],
141140
) -> tuple[argparse.Namespace, list[str]]:
142-
parser = _createParser()
141+
parser = argparse.ArgumentParser(
142+
description="Ingest and convert python packages to rez packages.",
143+
prog=__package__.replace("_", "-"),
144+
add_help=False,
145+
)
146+
parser = _setupParser(parser)
143147

144148
knownArgs = []
145149
pipArgs = []
@@ -313,8 +317,15 @@ def _debug(
313317
text=True,
314318
)
315319

320+
opts = vars(args)
321+
322+
# Get rid of non serializable values injected by rez.
323+
opts.pop("parser", None)
324+
opts.pop("func", None)
325+
opts.pop("formatter_class", None)
326+
316327
console.print("[bold]rez-pip provided arguments[/]:")
317-
print(textwrap.indent(json.dumps(vars(args), indent=4), prefix))
328+
print(textwrap.indent(json.dumps(opts, indent=4), prefix))
318329

319330
console.print(f"[bold]pip config debug[/]:", highlight=False)
320331
print(textwrap.indent(completedProcess.stdout.strip(), " "))
@@ -361,9 +372,16 @@ def _printPlugins() -> None:
361372
rez_pip.utils.CONSOLE.print(table)
362373

363374

364-
def run() -> int:
375+
def run(
376+
args: argparse.Namespace | None = None, pipArgs: list[str] | None = None
377+
) -> int:
365378
pipWorkArea = tempfile.mkdtemp(prefix="rez-pip-target")
366-
args, pipArgs = _parseArgs(sys.argv[1:])
379+
380+
if args is None:
381+
args, pipArgs = _parseArgs(sys.argv[1:])
382+
383+
assert args is not None
384+
assert pipArgs is not None
367385

368386
# Initialize the plugin system
369387
rez_pip.plugins.getManager()

0 commit comments

Comments
 (0)