Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare the codebase for hooking it up as a rez plugin #136

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
42 changes: 42 additions & 0 deletions src/rez_pip/__init__.py
Original file line number Diff line number Diff line change
@@ -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
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
Loading