From 97d2e4d7849be5a2ed1caae896c80d64592ea0f1 Mon Sep 17 00:00:00 2001 From: Wichert Akkerman Date: Fri, 4 May 2012 09:49:18 +0200 Subject: [PATCH 1/4] Use console_scripts instead of scripts. Unfortunately zc.buildout does not support distribute/setuptools script option (see https://bugs.launchpad.net/zc.buildout/+bug/422724), but it does support console_scripts. This also makes the scripts importable, allowing for unittest and code reuse. --- rq/scripts/__init__.py | 0 bin/rqgenload => rq/scripts/rqgenload.py | 1 + bin/rqinfo => rq/scripts/rqingo.py | 3 --- bin/rqworker => rq/scripts/rqworker.py | 4 ---- setup.py | 6 +++++- 5 files changed, 6 insertions(+), 8 deletions(-) create mode 100644 rq/scripts/__init__.py rename bin/rqgenload => rq/scripts/rqgenload.py (99%) rename bin/rqinfo => rq/scripts/rqingo.py (99%) rename bin/rqworker => rq/scripts/rqworker.py (98%) diff --git a/rq/scripts/__init__.py b/rq/scripts/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/bin/rqgenload b/rq/scripts/rqgenload.py similarity index 99% rename from bin/rqgenload rename to rq/scripts/rqgenload.py index a6e4cfd..3a10b7e 100755 --- a/bin/rqgenload +++ b/rq/scripts/rqgenload.py @@ -60,3 +60,4 @@ def main(): if __name__ == '__main__': main() + diff --git a/bin/rqinfo b/rq/scripts/rqingo.py similarity index 99% rename from bin/rqinfo rename to rq/scripts/rqingo.py index 851b4ec..c695d95 100755 --- a/bin/rqinfo +++ b/rq/scripts/rqingo.py @@ -177,6 +177,3 @@ def main(): interval(args.interval, func, args) except ConnectionError as e: print(e) - -if __name__ == '__main__': - main() diff --git a/bin/rqworker b/rq/scripts/rqworker.py similarity index 98% rename from bin/rqworker rename to rq/scripts/rqworker.py index e6a72e2..12ceb05 100755 --- a/bin/rqworker +++ b/rq/scripts/rqworker.py @@ -71,7 +71,3 @@ def main(): w.work(burst=args.burst) except ConnectionError as e: print(e) - - -if __name__ == '__main__': - main() diff --git a/setup.py b/setup.py index 97a3189..3adac2e 100644 --- a/setup.py +++ b/setup.py @@ -42,7 +42,11 @@ setup( zip_safe=False, platforms='any', install_requires=get_dependencies(), - scripts=['bin/rqinfo', 'bin/rqworker'], + entry_points='''\ + [console_scripts] + rqworker = rq.scripts.rqworker:main + rqinfo = rq.scripts.rqinfo:main + ''', classifiers=[ # As from http://pypi.python.org/pypi?%3Aaction=list_classifiers #'Development Status :: 1 - Planning', From 2d4a58631d4d4b97afc527acbea224437b773d23 Mon Sep 17 00:00:00 2001 From: Wichert Akkerman Date: Fri, 4 May 2012 09:57:44 +0200 Subject: [PATCH 2/4] PyFlakes --- rq/scripts/rqgenload.py | 2 +- rq/scripts/rqworker.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/rq/scripts/rqgenload.py b/rq/scripts/rqgenload.py index 3a10b7e..b643c49 100755 --- a/rq/scripts/rqgenload.py +++ b/rq/scripts/rqgenload.py @@ -1,6 +1,6 @@ #!/usr/bin/env python import optparse -from rq import use_connection, Queue, Worker +from rq import use_connection, Queue from rq import dummy diff --git a/rq/scripts/rqworker.py b/rq/scripts/rqworker.py index 12ceb05..1a4bc76 100755 --- a/rq/scripts/rqworker.py +++ b/rq/scripts/rqworker.py @@ -27,7 +27,6 @@ def setup_loghandlers(args): loglevel = logbook.INFO formatter = format_colors - import sys handlers.NullHandler(bubble=False).push_application() handler = handlers.StreamHandler(sys.stdout, level=loglevel, bubble=False) if formatter: From 9f7d608da870183ecd5f4c8f956a9db8e3a20730 Mon Sep 17 00:00:00 2001 From: Wichert Akkerman Date: Fri, 4 May 2012 10:00:31 +0200 Subject: [PATCH 3/4] Renaming typo.. --- rq/scripts/{rqingo.py => rqinfo.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename rq/scripts/{rqingo.py => rqinfo.py} (100%) diff --git a/rq/scripts/rqingo.py b/rq/scripts/rqinfo.py similarity index 100% rename from rq/scripts/rqingo.py rename to rq/scripts/rqinfo.py From ec0e4776b4034cce22983240f991678fa052654e Mon Sep 17 00:00:00 2001 From: Wichert Akkerman Date: Fri, 4 May 2012 10:04:37 +0200 Subject: [PATCH 4/4] Move common code out. --- rq/scripts/__init__.py | 16 ++++++++++++++++ rq/scripts/rqinfo.py | 13 +++++-------- rq/scripts/rqworker.py | 14 +++++--------- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/rq/scripts/__init__.py b/rq/scripts/__init__.py index e69de29..a616912 100644 --- a/rq/scripts/__init__.py +++ b/rq/scripts/__init__.py @@ -0,0 +1,16 @@ +import redis +from rq import use_connection + + +def add_standard_arguments(parser): + parser.add_argument('--host', '-H', default='localhost', + help='The Redis hostname (default: localhost)') + parser.add_argument('--port', '-p', type=int, default=6379, + help='The Redis portnumber (default: 6379)') + parser.add_argument('--db', '-d', type=int, default=0, + help='The Redis database (default: 0)') + + +def setup_redis(args): + redis_conn = redis.Redis(host=args.host, port=args.port, db=args.db) + use_connection(redis_conn) diff --git a/rq/scripts/rqinfo.py b/rq/scripts/rqinfo.py index c695d95..71c302c 100755 --- a/rq/scripts/rqinfo.py +++ b/rq/scripts/rqinfo.py @@ -4,10 +4,11 @@ import sys import os import time import argparse -import redis from redis.exceptions import ConnectionError -from rq import use_connection, Queue, Worker +from rq import Queue, Worker from rq.utils import gettermsize, make_colorizer +from rq.scripts import add_standard_arguments +from rq.scripts import setup_redis red = make_colorizer('darkred') green = make_colorizer('darkgreen') @@ -133,9 +134,7 @@ def show_both(args): def parse_args(): parser = argparse.ArgumentParser(description='RQ command-line monitor.') - parser.add_argument('--host', '-H', default='localhost', help='The Redis hostname (default: localhost)') - parser.add_argument('--port', '-p', type=int, default=6379, help='The Redis portnumber (default: 6379)') - parser.add_argument('--db', '-d', type=int, default=0, help='The Redis database (default: 0)') + add_standard_arguments(parser) parser.add_argument('--path', '-P', default='.', help='Specify the import path.') parser.add_argument('--interval', '-i', metavar='N', type=float, default=2.5, help='Updates stats every N seconds (default: don\'t poll)') parser.add_argument('--raw', '-r', action='store_true', default=False, help='Print only the raw numbers, no bar charts') @@ -163,9 +162,7 @@ def main(): if args.path: sys.path = args.path.split(':') + sys.path - # Setup connection to Redis - redis_conn = redis.Redis(host=args.host, port=args.port, db=args.db) - use_connection(redis_conn) + setup_redis(args) try: if args.only_queues: func = show_queues diff --git a/rq/scripts/rqworker.py b/rq/scripts/rqworker.py index 1a4bc76..06c3a7c 100755 --- a/rq/scripts/rqworker.py +++ b/rq/scripts/rqworker.py @@ -2,10 +2,11 @@ import sys import argparse import logbook -import redis from logbook import handlers -from rq import use_connection, Queue, Worker +from rq import Queue, Worker from redis.exceptions import ConnectionError +from rq.scripts import add_standard_arguments +from rq.scripts import setup_redis def format_colors(record, handler): @@ -40,9 +41,7 @@ def setup_loghandlers(args): def parse_args(): parser = argparse.ArgumentParser(description='Starts an RQ worker.') - parser.add_argument('--host', '-H', default='localhost', help='The Redis hostname (default: localhost)') - parser.add_argument('--port', '-p', type=int, default=6379, help='The Redis portnumber (default: 6379)') - parser.add_argument('--db', '-d', type=int, default=0, help='The Redis database (default: 0)') + add_standard_arguments(parser) parser.add_argument('--burst', '-b', action='store_true', default=False, help='Run in burst mode (quit after all work is done)') parser.add_argument('--name', '-n', default=None, help='Specify a different name') @@ -60,10 +59,7 @@ def main(): sys.path = args.path.split(':') + sys.path setup_loghandlers(args) - - # Setup connection to Redis - redis_conn = redis.Redis(host=args.host, port=args.port, db=args.db) - use_connection(redis_conn) + setup_redis(args) try: queues = map(Queue, args.queues) w = Worker(queues, name=args.name)