/usr/share/pyshared/celery/bin/camqadm.py is in python-celery 2.4.6-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 | # -*- coding: utf-8 -*-
"""camqadm
.. program:: camqadm
"""
from __future__ import absolute_import
import cmd
import sys
import shlex
import pprint
from itertools import count
from amqplib import client_0_8 as amqp
from celery.app import app_or_default
from celery.utils import padlist
from celery.bin.base import Command
# Valid string -> bool coercions.
BOOLS = {"1": True, "0": False,
"on": True, "off": False,
"yes": True, "no": False,
"true": True, "False": False}
# Map to coerce strings to other types.
COERCE = {bool: lambda value: BOOLS[value.lower()]}
HELP_HEADER = """
Commands
--------
""".rstrip()
EXAMPLE_TEXT = """
Example:
-> queue.delete myqueue yes no
"""
def say(m):
sys.stderr.write("%s\n" % (m, ))
class Spec(object):
"""AMQP Command specification.
Used to convert arguments to Python values and display various help
and tooltips.
:param args: see :attr:`args`.
:keyword returns: see :attr:`returns`.
.. attribute args::
List of arguments this command takes. Should
contain `(argument_name, argument_type)` tuples.
.. attribute returns:
Helpful human string representation of what this command returns.
May be :const:`None`, to signify the return type is unknown.
"""
def __init__(self, *args, **kwargs):
self.args = args
self.returns = kwargs.get("returns")
def coerce(self, index, value):
"""Coerce value for argument at index.
E.g. if :attr:`args` is `[("is_active", bool)]`:
>>> coerce(0, "False")
False
"""
arg_info = self.args[index]
arg_type = arg_info[1]
# Might be a custom way to coerce the string value,
# so look in the coercion map.
return COERCE.get(arg_type, arg_type)(value)
def str_args_to_python(self, arglist):
"""Process list of string arguments to values according to spec.
e.g:
>>> spec = Spec([("queue", str), ("if_unused", bool)])
>>> spec.str_args_to_python("pobox", "true")
("pobox", True)
"""
return tuple(self.coerce(index, value)
for index, value in enumerate(arglist))
def format_response(self, response):
"""Format the return value of this command in a human-friendly way."""
if not self.returns:
if response is None:
return "ok."
return response
if callable(self.returns):
return self.returns(response)
return self.returns % (response, )
def format_arg(self, name, type, default_value=None):
if default_value is not None:
return "%s:%s" % (name, default_value)
return name
def format_signature(self):
return " ".join(self.format_arg(*padlist(list(arg), 3))
for arg in self.args)
def dump_message(message):
if message is None:
return "No messages in queue. basic.publish something."
return {"body": message.body,
"properties": message.properties,
"delivery_info": message.delivery_info}
def format_declare_queue(ret):
return "ok. queue:%s messages:%s consumers:%s." % ret
class AMQShell(cmd.Cmd):
"""AMQP API Shell.
:keyword connect: Function used to connect to the server, must return
connection object.
:keyword silent: If :const:`True`, the commands won't have annoying
output not relevant when running in non-shell mode.
.. attribute: builtins
Mapping of built-in command names -> method names
.. attribute:: amqp
Mapping of AMQP API commands and their :class:`Spec`.
"""
conn = None
chan = None
prompt_fmt = "%d> "
identchars = cmd.IDENTCHARS = "."
needs_reconnect = False
counter = 1
inc_counter = count(2).next
builtins = {"EOF": "do_exit",
"exit": "do_exit",
"help": "do_help"}
amqp = {
"exchange.declare": Spec(("exchange", str),
("type", str),
("passive", bool, "no"),
("durable", bool, "no"),
("auto_delete", bool, "no"),
("internal", bool, "no")),
"exchange.delete": Spec(("exchange", str),
("if_unused", bool)),
"queue.bind": Spec(("queue", str),
("exchange", str),
("routing_key", str)),
"queue.declare": Spec(("queue", str),
("passive", bool, "no"),
("durable", bool, "no"),
("exclusive", bool, "no"),
("auto_delete", bool, "no"),
returns=format_declare_queue),
"queue.delete": Spec(("queue", str),
("if_unused", bool, "no"),
("if_empty", bool, "no"),
returns="ok. %d messages deleted."),
"queue.purge": Spec(("queue", str),
returns="ok. %d messages deleted."),
"basic.get": Spec(("queue", str),
("no_ack", bool, "off"),
returns=dump_message),
"basic.publish": Spec(("msg", amqp.Message),
("exchange", str),
("routing_key", str),
("mandatory", bool, "no"),
("immediate", bool, "no")),
"basic.ack": Spec(("delivery_tag", int)),
}
def __init__(self, *args, **kwargs):
self.connect = kwargs.pop("connect")
self.silent = kwargs.pop("silent", False)
cmd.Cmd.__init__(self, *args, **kwargs)
self._reconnect()
def say(self, m):
"""Say something to the user. Disabled if :attr:`silent`."""
if not self.silent:
say(m)
def get_amqp_api_command(self, cmd, arglist):
"""With a command name and a list of arguments, convert the arguments
to Python values and find the corresponding method on the AMQP channel
object.
:returns: tuple of `(method, processed_args)`.
Example:
>>> get_amqp_api_command("queue.delete", ["pobox", "yes", "no"])
(<bound method Channel.queue_delete of
<amqplib.client_0_8.channel.Channel object at 0x...>>,
('testfoo', True, False))
"""
spec = self.amqp[cmd]
args = spec.str_args_to_python(arglist)
attr_name = cmd.replace(".", "_")
if self.needs_reconnect:
self._reconnect()
return getattr(self.chan, attr_name), args, spec.format_response
def do_exit(self, *args):
"""The `"exit"` command."""
self.say("\n-> please, don't leave!")
sys.exit(0)
def display_command_help(self, cmd, short=False):
spec = self.amqp[cmd]
say("%s %s" % (cmd, spec.format_signature()))
def do_help(self, *args):
if not args:
say(HELP_HEADER)
for cmd_name in self.amqp.keys():
self.display_command_help(cmd_name, short=True)
say(EXAMPLE_TEXT)
else:
self.display_command_help(args[0])
def default(self, line):
say("unknown syntax: '%s'. how about some 'help'?" % line)
def get_names(self):
return set(self.builtins.keys() + self.amqp.keys())
def completenames(self, text, *ignored):
"""Return all commands starting with `text`, for tab-completion."""
names = self.get_names()
first = [cmd for cmd in names
if cmd.startswith(text.replace("_", "."))]
if first:
return first
return [cmd for cmd in names
if cmd.partition(".")[2].startswith(text)]
def dispatch(self, cmd, argline):
"""Dispatch and execute the command.
Lookup order is: :attr:`builtins` -> :attr:`amqp`.
"""
arglist = shlex.split(argline)
if cmd in self.builtins:
return getattr(self, self.builtins[cmd])(*arglist)
fun, args, formatter = self.get_amqp_api_command(cmd, arglist)
return formatter(fun(*args))
def parseline(self, line):
"""Parse input line.
:returns: tuple of three items:
`(command_name, arglist, original_line)`
E.g::
>>> parseline("queue.delete A 'B' C")
("queue.delete", "A 'B' C", "queue.delete A 'B' C")
"""
parts = line.split()
if parts:
return parts[0], " ".join(parts[1:]), line
return "", "", line
def onecmd(self, line):
"""Parse line and execute command."""
cmd, arg, line = self.parseline(line)
if not line:
return self.emptyline()
if cmd is None:
return self.default(line)
self.lastcmd = line
if cmd == '':
return self.default(line)
else:
self.counter = self.inc_counter()
try:
self.respond(self.dispatch(cmd, arg))
except (AttributeError, KeyError), exc:
self.default(line)
except Exception, exc:
say(exc)
self.needs_reconnect = True
def respond(self, retval):
"""What to do with the return value of a command."""
if retval is not None:
if isinstance(retval, basestring):
say(retval)
else:
pprint.pprint(retval)
def _reconnect(self):
"""Re-establish connection to the AMQP server."""
self.conn = self.connect(self.conn)
self.chan = self.conn.channel()
self.needs_reconnect = False
@property
def prompt(self):
return self.prompt_fmt % self.counter
class AMQPAdmin(object):
"""The celery :program:`camqadm` utility."""
def __init__(self, *args, **kwargs):
self.app = app_or_default(kwargs.get("app"))
self.silent = bool(args)
if "silent" in kwargs:
self.silent = kwargs["silent"]
self.args = args
def connect(self, conn=None):
if conn:
conn.close()
conn = self.app.broker_connection()
self.say("-> connecting to %s." % conn.as_uri())
conn.connect()
self.say("-> connected.")
return conn
def run(self):
shell = AMQShell(connect=self.connect)
if self.args:
return shell.onecmd(" ".join(self.args))
try:
return shell.cmdloop()
except KeyboardInterrupt:
self.say("(bibi)")
pass
def say(self, m):
if not self.silent:
say(m)
class AMQPAdminCommand(Command):
def run(self, *args, **options):
options["app"] = self.app
return AMQPAdmin(*args, **options).run()
def camqadm(*args, **options):
AMQPAdmin(*args, **options).run()
def main():
AMQPAdminCommand().execute_from_commandline()
if __name__ == "__main__": # pragma: no cover
main()
|