| 1 |
From: Bas Couwenberg <[email protected]> |
| 2 |
Date: Mon, 27 Oct 2025 00:20:34 +0000 |
| 3 |
Subject: Vendor click-plugins, PyPI package no longer maintained. |
| 4 |
|
| 5 |
Bug: https://github.com/celery/celery/pull/9786 |
| 6 |
--- |
| 7 |
celery/bin/celery.py | 2 +- |
| 8 |
celery/click_plugins.py | 247 +++++++++++++++++++++++++++++++++++++++++++++++ |
| 9 |
requirements/default.txt | 1 - |
| 10 |
3 files changed, 248 insertions(+), 2 deletions(-) |
| 11 |
create mode 100644 celery/click_plugins.py |
| 12 |
|
| 13 |
diff --git a/celery/bin/celery.py b/celery/bin/celery.py |
| 14 |
index e1fae1a..41a8e3e 100644 |
| 15 |
--- a/celery/bin/celery.py |
| 16 |
+++ b/celery/bin/celery.py |
| 17 |
@@ -8,7 +8,6 @@ from importlib.metadata import entry_points |
| 18 |
import click |
| 19 |
import click.exceptions |
| 20 |
from click_didyoumean import DYMGroup |
| 21 |
-from click_plugins import with_plugins |
| 22 |
|
| 23 |
from celery import VERSION_BANNER |
| 24 |
from celery.app.utils import find_app |
| 25 |
@@ -28,6 +27,7 @@ from celery.bin.result import result |
| 26 |
from celery.bin.shell import shell |
| 27 |
from celery.bin.upgrade import upgrade |
| 28 |
from celery.bin.worker import worker |
| 29 |
+from celery.click_plugins import with_plugins |
| 30 |
|
| 31 |
UNABLE_TO_LOAD_APP_MODULE_NOT_FOUND = click.style(""" |
| 32 |
Unable to load celery application. |
| 33 |
diff --git a/celery/click_plugins.py b/celery/click_plugins.py |
| 34 |
new file mode 100644 |
| 35 |
index 0000000..ed9e5dc |
| 36 |
--- /dev/null |
| 37 |
+++ b/celery/click_plugins.py |
| 38 |
@@ -0,0 +1,247 @@ |
| 39 |
+# This file is part of 'click-plugins': https://github.com/click-contrib/click-plugins |
| 40 |
+# |
| 41 |
+# New BSD License |
| 42 |
+# |
| 43 |
+# Copyright (c) 2015-2025, Kevin D. Wurster, Sean C. Gillies |
| 44 |
+# All rights reserved. |
| 45 |
+# |
| 46 |
+# Redistribution and use in source and binary forms, with or without |
| 47 |
+# modification, are permitted provided that the following conditions are met: |
| 48 |
+# |
| 49 |
+# * Redistributions of source code must retain the above copyright notice, this |
| 50 |
+# list of conditions and the following disclaimer. |
| 51 |
+# |
| 52 |
+# * Redistributions in binary form must reproduce the above copyright notice, |
| 53 |
+# this list of conditions and the following disclaimer in the documentation |
| 54 |
+# and/or other materials provided with the distribution. |
| 55 |
+# |
| 56 |
+# * Neither click-plugins nor the names of its contributors may not be used to |
| 57 |
+# endorse or promote products derived from this software without specific prior |
| 58 |
+# written permission. |
| 59 |
+# |
| 60 |
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 61 |
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 62 |
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 63 |
+# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 64 |
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 65 |
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 66 |
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 67 |
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 68 |
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 69 |
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 70 |
+ |
| 71 |
+ |
| 72 |
+"""Support CLI plugins with click and entry points. |
| 73 |
+ |
| 74 |
+See :func:`with_plugins`. |
| 75 |
+""" |
| 76 |
+ |
| 77 |
+ |
| 78 |
+import importlib.metadata |
| 79 |
+import os |
| 80 |
+import sys |
| 81 |
+import traceback |
| 82 |
+ |
| 83 |
+import click |
| 84 |
+ |
| 85 |
+ |
| 86 |
+__version__ = '2.0' |
| 87 |
+ |
| 88 |
+ |
| 89 |
+def with_plugins(entry_points): |
| 90 |
+ |
| 91 |
+ """Decorator for loading and attaching plugins to a ``click.Group()``. |
| 92 |
+ |
| 93 |
+ Plugins are loaded from an ``importlib.metadata.EntryPoint()``. Each entry |
| 94 |
+ point must point to a ``click.Command()``. An entry point that fails to |
| 95 |
+ load will be wrapped in a ``BrokenCommand()`` to allow the CLI user to |
| 96 |
+ discover and potentially debug the problem. |
| 97 |
+ |
| 98 |
+ >>> from importlib.metadata import entry_points |
| 99 |
+ >>> |
| 100 |
+ >>> import click |
| 101 |
+ >>> from click_plugins import with_plugins |
| 102 |
+ >>> |
| 103 |
+ >>> @with_plugins('group_name') |
| 104 |
+ >>> @click.group() |
| 105 |
+ >>> def group(): |
| 106 |
+ ... '''Group''' |
| 107 |
+ >>> |
| 108 |
+ >>> @with_plugins(entry_points('group_name')) |
| 109 |
+ >>> @click.group() |
| 110 |
+ >>> def group(): |
| 111 |
+ ... '''Group''' |
| 112 |
+ >>> |
| 113 |
+ >>> @with_plugins(importlib.metadata.EntryPoint(...)) |
| 114 |
+ >>> @click.group() |
| 115 |
+ >>> def group(): |
| 116 |
+ ... '''Group''' |
| 117 |
+ >>> |
| 118 |
+ >>> @with_plugins("group1") |
| 119 |
+ >>> @with_plugins("group2") |
| 120 |
+ >>> def group(): |
| 121 |
+ ... '''Group''' |
| 122 |
+ |
| 123 |
+ :param str or EntryPoint or sequence[EntryPoint] entry_points: |
| 124 |
+ Entry point group name, a single ``importlib.metadata.EntryPoint()``, |
| 125 |
+ or a sequence of ``EntryPoint()``s. |
| 126 |
+ |
| 127 |
+ :rtype function: |
| 128 |
+ """ |
| 129 |
+ |
| 130 |
+ # Note that the explicit full path reference to: |
| 131 |
+ # |
| 132 |
+ # importlib.metadata.entry_points() |
| 133 |
+ # |
| 134 |
+ # in this function allows the call to be mocked in the tests. Replacing |
| 135 |
+ # with: |
| 136 |
+ # |
| 137 |
+ # from importlib.metadata import entry_points |
| 138 |
+ # |
| 139 |
+ # breaks this ability. |
| 140 |
+ |
| 141 |
+ def decorator(group): |
| 142 |
+ if not isinstance(group, click.Group): |
| 143 |
+ raise TypeError( |
| 144 |
+ f"plugins can only be attached to an instance of" |
| 145 |
+ f" 'click.Group()' not: {repr(group)}") |
| 146 |
+ |
| 147 |
+ # Load 'EntryPoint()' objects. |
| 148 |
+ if isinstance(entry_points, str): |
| 149 |
+ |
| 150 |
+ # Older versions of Python do not support filtering. |
| 151 |
+ if sys.version_info >= (3, 10): |
| 152 |
+ all_entry_points = importlib.metadata.entry_points( |
| 153 |
+ group=entry_points) |
| 154 |
+ |
| 155 |
+ else: |
| 156 |
+ all_entry_points = importlib.metadata.entry_points() |
| 157 |
+ all_entry_points = all_entry_points[entry_points] |
| 158 |
+ |
| 159 |
+ # A single 'importlib.metadata.EntryPoint()' |
| 160 |
+ elif isinstance(entry_points, importlib.metadata.EntryPoint): |
| 161 |
+ all_entry_points = [entry_points] |
| 162 |
+ |
| 163 |
+ # Sequence of 'EntryPoints()'. |
| 164 |
+ else: |
| 165 |
+ all_entry_points = entry_points |
| 166 |
+ |
| 167 |
+ for ep in all_entry_points: |
| 168 |
+ |
| 169 |
+ try: |
| 170 |
+ group.add_command(ep.load()) |
| 171 |
+ |
| 172 |
+ # Catch all exceptions (technically not 'BaseException') and |
| 173 |
+ # instead register a special 'BrokenCommand()'. Otherwise, a single |
| 174 |
+ # plugin that fails to load and/or register will make the CLI |
| 175 |
+ # inoperable. 'BrokenCommand()' explains the situation to users. |
| 176 |
+ except Exception as e: |
| 177 |
+ group.add_command(BrokenCommand(ep, e)) |
| 178 |
+ |
| 179 |
+ return group |
| 180 |
+ |
| 181 |
+ return decorator |
| 182 |
+ |
| 183 |
+ |
| 184 |
+class BrokenCommand(click.Command): |
| 185 |
+ |
| 186 |
+ """Represents a plugin ``click.Command()`` that failed to load. |
| 187 |
+ |
| 188 |
+ Can be executed just like a ``click.Command()``, but prints information |
| 189 |
+ for debugging and exits with an error code. |
| 190 |
+ """ |
| 191 |
+ |
| 192 |
+ def __init__(self, entry_point, exception): |
| 193 |
+ |
| 194 |
+ """ |
| 195 |
+ :param importlib.metadata.EntryPoint entry_point: |
| 196 |
+ Entry point that failed to load. |
| 197 |
+ :param Exception exception: |
| 198 |
+ Raised when attempting to load the entry point associated with |
| 199 |
+ this instance. |
| 200 |
+ """ |
| 201 |
+ |
| 202 |
+ super().__init__(entry_point.name) |
| 203 |
+ |
| 204 |
+ # There are several ways to get a traceback from an exception, but |
| 205 |
+ # 'TracebackException()' seems to be the most portable across actively |
| 206 |
+ # supported versions of Python. |
| 207 |
+ tbe = traceback.TracebackException.from_exception(exception) |
| 208 |
+ |
| 209 |
+ # A message for '$ cli command --help'. Contains full traceback and a |
| 210 |
+ # helpful note. The intention is to nudge users to figure out which |
| 211 |
+ # project should get a bug report since users are likely to report the |
| 212 |
+ # issue to the developers of the CLI utility they are directly |
| 213 |
+ # interacting with. These are not necessarily the right developers. |
| 214 |
+ self.help = ( |
| 215 |
+ "{ls}ERROR: entry point '{module}:{name}' could not be loaded." |
| 216 |
+ " Contact its author for help.{ls}{ls}{tb}").format( |
| 217 |
+ module=_module(entry_point), |
| 218 |
+ name=entry_point.name, |
| 219 |
+ ls=os.linesep, |
| 220 |
+ tb=''.join(tbe.format()) |
| 221 |
+ ) |
| 222 |
+ |
| 223 |
+ # Replace the broken command's summary with a warning about how it |
| 224 |
+ # was not loaded successfully. The idea is that '$ cli --help' should |
| 225 |
+ # include a clear indicator that a subcommand is not functional, and |
| 226 |
+ # a little hint for what to do about it. U+2020 is a "dagger", whose |
| 227 |
+ # modern use typically indicates a footnote. |
| 228 |
+ self.short_help = ( |
| 229 |
+ f"\u2020 Warning: could not load plugin. Invoke command with" |
| 230 |
+ f" '--help' for traceback." |
| 231 |
+ ) |
| 232 |
+ |
| 233 |
+ def invoke(self, ctx): |
| 234 |
+ |
| 235 |
+ """Print traceback and debugging message. |
| 236 |
+ |
| 237 |
+ :param click.Context ctx: |
| 238 |
+ Active context. |
| 239 |
+ """ |
| 240 |
+ |
| 241 |
+ click.echo(self.help, color=ctx.color, err=True) |
| 242 |
+ ctx.exit(1) |
| 243 |
+ |
| 244 |
+ def parse_args(self, ctx, args): |
| 245 |
+ |
| 246 |
+ """Pass arguments along without parsing. |
| 247 |
+ |
| 248 |
+ :param click.Context ctx: |
| 249 |
+ Active context. |
| 250 |
+ :param list args: |
| 251 |
+ List of command line arguments. |
| 252 |
+ """ |
| 253 |
+ |
| 254 |
+ # Do not attempt to parse these arguments. We do not know why the |
| 255 |
+ # entry point failed to load, but it is reasonable to assume that |
| 256 |
+ # argument parsing will not work. Ultimately the goal is to get the |
| 257 |
+ # 'Command.invoke()' method (overloaded in this class) to execute |
| 258 |
+ # and provide the user with a bit of debugging information. |
| 259 |
+ |
| 260 |
+ return args |
| 261 |
+ |
| 262 |
+ |
| 263 |
+def _module(ep): |
| 264 |
+ |
| 265 |
+ """Module name for a given entry point. |
| 266 |
+ |
| 267 |
+ Parameters |
| 268 |
+ ---------- |
| 269 |
+ ep : importlib.metadata.EntryPoint |
| 270 |
+ Determine parent module for this entry point. |
| 271 |
+ |
| 272 |
+ Returns |
| 273 |
+ ------- |
| 274 |
+ str |
| 275 |
+ """ |
| 276 |
+ |
| 277 |
+ if sys.version_info >= (3, 10): |
| 278 |
+ module = ep.module |
| 279 |
+ |
| 280 |
+ else: |
| 281 |
+ # From 'importlib.metadata.EntryPoint.module'. |
| 282 |
+ match = ep.pattern.match(ep.value) |
| 283 |
+ module = match.group('module') |
| 284 |
+ |
| 285 |
+ return module |
| 286 |
diff --git a/requirements/default.txt b/requirements/default.txt |
| 287 |
index eddb0ca..e9a4b47 100644 |
| 288 |
--- a/requirements/default.txt |
| 289 |
+++ b/requirements/default.txt |
| 290 |
@@ -4,7 +4,6 @@ vine>=5.1.0,<6.0 |
| 291 |
click>=8.1.2,<9.0 |
| 292 |
click-didyoumean>=0.3.0 |
| 293 |
click-repl>=0.2.0 |
| 294 |
-click-plugins>=1.1.1 |
| 295 |
python-dateutil>=2.8.2 |
| 296 |
exceptiongroup>=1.3.0; python_version < '3.11' |
| 297 |
tzlocal |