From 8437376c53f1396c7a7bfdd4736f161c61f3b2a1 Mon Sep 17 00:00:00 2001 From: Matthias Dellweg Date: Wed, 13 Mar 2024 16:34:11 +0100 Subject: [PATCH] Add debug ipython command This can help developers to drop quite fast into a interactive python shell with a usable pulp_ctx preconfigured. [noissue] --- CHANGES/+ipython.feature | 1 + pulpcore/cli/common/debug.py | 36 ++++++++++++++++++++++++++++++++++++ pyproject.toml | 1 + 3 files changed, 38 insertions(+) create mode 100644 CHANGES/+ipython.feature diff --git a/CHANGES/+ipython.feature b/CHANGES/+ipython.feature new file mode 100644 index 00000000..6202a6aa --- /dev/null +++ b/CHANGES/+ipython.feature @@ -0,0 +1 @@ +Added `pulp debug ipython` command to drop into a python shell. diff --git a/pulpcore/cli/common/debug.py b/pulpcore/cli/common/debug.py index 2e12426a..9f152a65 100644 --- a/pulpcore/cli/common/debug.py +++ b/pulpcore/cli/common/debug.py @@ -12,6 +12,13 @@ pulp_group, ) +try: + import IPython +except ImportError: + IPYTHON_AVAILABLE = False +else: + IPYTHON_AVAILABLE = True + translation = get_translation(__package__) _ = translation.gettext @@ -23,6 +30,35 @@ def debug() -> None: """ +if IPYTHON_AVAILABLE: + + @debug.command() + @pass_pulp_context + @click.pass_context + def ipython( + ctx: click.Context, + pulp_ctx: PulpCLIContext, + ) -> None: + """ + Drop into an interactive python shell. + Prepopulated symbols: + - click + - PluginRequirement + - ctx: click.Context + - pulp_ctx: PulpContext + """ + user_ns: t.Dict[str, t.Any] = { + # modules + "click": click, + # Classes + "PluginRequirement": PluginRequirement, + # Objects + "ctx": ctx, + "pulp_ctx": pulp_ctx, + } + IPython.start_ipython(argv=[], user_ns=user_ns) + + @debug.command() @click.option("--name", required=True) @click.option("--min-version", help=_("Succeed only if the installed version is not smaller.")) diff --git a/pyproject.toml b/pyproject.toml index 6b908f74..f7525043 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -180,6 +180,7 @@ explicit_package_bases = true module = [ "click_shell.*", "gnupg.*", + "IPython.*", "schema.*", ] ignore_missing_imports = true