From 9e826a590f45588f6663f78a23bbb7d6db3d0280 Mon Sep 17 00:00:00 2001 From: Kostas Papadimitriou Date: Tue, 17 Jan 2012 19:21:55 +0200 Subject: [PATCH] Improved snf-pithos-tools packages - refactored tool scripts to python modules - added console_scripts entry points in setup.py - minor fix in dispatcher.py --- snf-pithos-tools/pithos/tools/README | 4 --- snf-pithos-tools/pithos/tools/dispatcher.py | 27 +++++++++++++------- snf-pithos-tools/pithos/tools/{pithos-fs => fs.py} | 7 ++++- snf-pithos-tools/pithos/tools/{pithos-sh => sh.py} | 2 ++ .../pithos/tools/{pithos-sync => sync.py} | 0 .../pithos/tools/{pithos-test => test.py} | 8 +++++- snf-pithos-tools/setup.py | 9 ++++--- 7 files changed, 38 insertions(+), 19 deletions(-) delete mode 100644 snf-pithos-tools/pithos/tools/README create mode 100644 snf-pithos-tools/pithos/tools/__init__.py rename snf-pithos-tools/pithos/tools/{pithos-fs => fs.py} (99%) rename snf-pithos-tools/pithos/tools/{pithos-sh => sh.py} (99%) rename snf-pithos-tools/pithos/tools/{pithos-sync => sync.py} (100%) rename snf-pithos-tools/pithos/tools/{pithos-test => test.py} (99%) diff --git a/snf-pithos-tools/pithos/tools/README b/snf-pithos-tools/pithos/tools/README deleted file mode 100644 index 878727a..0000000 --- a/snf-pithos-tools/pithos/tools/README +++ /dev/null @@ -1,4 +0,0 @@ -Tools in this dir depend on being able to import pithos. -You need to adjust PYTHONPATH accordingly for this to work. -e.g. - export PYTHONPATH=$HOME/pithos diff --git a/snf-pithos-tools/pithos/tools/__init__.py b/snf-pithos-tools/pithos/tools/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/snf-pithos-tools/pithos/tools/dispatcher.py b/snf-pithos-tools/pithos/tools/dispatcher.py index e66b629..8625390 100755 --- a/snf-pithos-tools/pithos/tools/dispatcher.py +++ b/snf-pithos-tools/pithos/tools/dispatcher.py @@ -38,9 +38,15 @@ import logging from optparse import OptionParser -from carrot.connection import BrokerConnection -from carrot.messaging import Consumer -from carrot.messaging import Publisher +try: + from carrot.connection import BrokerConnection + from carrot.messaging import Consumer + from carrot.messaging import Publisher +except ImportError: + sys.stderr.write("Dispatcher requires 'carrot' python library to " \ + "be installed\n") + sys.exit(1) + BROKER_HOST = 'localhost' @@ -56,7 +62,7 @@ CONSUMER_KEY = '#' DEBUG = False -if __name__ == '__main__': +def main(): parser = OptionParser() parser.add_option('-v', '--verbose', action='store_true', default=False, dest='verbose', help='Enable verbose logging') @@ -81,14 +87,14 @@ if __name__ == '__main__': parser.add_option('--test', action='store_true', default=False, dest='test', help='Produce a dummy message for testing') opts, args = parser.parse_args() - + if opts.verbose: DEBUG = True logging.basicConfig(format='%(asctime)s [%(levelname)s] %(name)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', level=logging.DEBUG if DEBUG else logging.INFO) logger = logging.getLogger('dispatcher') - + conn = BrokerConnection(hostname=opts.host, port=opts.port, userid=opts.user, password=opts.password, virtual_host=opts.vhost) @@ -103,7 +109,7 @@ if __name__ == '__main__': consumer = Consumer(connection=conn, queue=opts.queue, exchange=opts.exchange, routing_key=opts.key, exchange_type="topic") - + callback = None if opts.callback: cb = opts.callback.rsplit('.', 1) @@ -111,16 +117,19 @@ if __name__ == '__main__': __import__(cb[0]) cb_module = sys.modules[cb[0]] callback = getattr(cb_module, cb[1]) - + def process_message(message_data, message): logger.debug('%s', message_data) if callback: callback(message_data) message.ack() - + consumer.register_callback(process_message) try: consumer.wait() except KeyboardInterrupt: pass +if __name__ == '__main__': + main() + diff --git a/snf-pithos-tools/pithos/tools/pithos-fs b/snf-pithos-tools/pithos/tools/fs.py similarity index 99% rename from snf-pithos-tools/pithos/tools/pithos-fs rename to snf-pithos-tools/pithos/tools/fs.py index 0c0b5bb..391a1ef 100755 --- a/snf-pithos-tools/pithos/tools/pithos-fs +++ b/snf-pithos-tools/pithos/tools/fs.py @@ -323,7 +323,7 @@ class StoreFS(Operations): return len(data) -if __name__ == '__main__': +def main(): if len(argv) != 2: print 'usage: %s ' % argv[0] exit(1) @@ -331,3 +331,8 @@ if __name__ == '__main__': user = getuser() fs = StoreFS(verbose=True) fuse = FUSE(fs, argv[1], foreground=True) + + +if __name__ == '__main__': + main() + diff --git a/snf-pithos-tools/pithos/tools/pithos-sh b/snf-pithos-tools/pithos/tools/sh.py similarity index 99% rename from snf-pithos-tools/pithos/tools/pithos-sh rename to snf-pithos-tools/pithos/tools/sh.py index 7b6a46c..767b26f 100755 --- a/snf-pithos-tools/pithos/tools/pithos-sh +++ b/snf-pithos-tools/pithos/tools/sh.py @@ -750,6 +750,7 @@ def print_versions(data, f=stdout): for id, t in data['versions']: f.write('%s @ %s\n' % (str(id).rjust(30), datetime.fromtimestamp(float(t)))) + def main(): try: name = argv[1] @@ -769,5 +770,6 @@ def main(): status = '%s ' % f.status if f.status else '' print '%s%s' % (status, f.data) + if __name__ == '__main__': main() diff --git a/snf-pithos-tools/pithos/tools/pithos-sync b/snf-pithos-tools/pithos/tools/sync.py similarity index 100% rename from snf-pithos-tools/pithos/tools/pithos-sync rename to snf-pithos-tools/pithos/tools/sync.py diff --git a/snf-pithos-tools/pithos/tools/pithos-test b/snf-pithos-tools/pithos/tools/test.py similarity index 99% rename from snf-pithos-tools/pithos/tools/pithos-test rename to snf-pithos-tools/pithos/tools/test.py index 32d6108..35c3fdd 100755 --- a/snf-pithos-tools/pithos/tools/pithos-test +++ b/snf-pithos-tools/pithos/tools/test.py @@ -2114,8 +2114,14 @@ o_names = ['kate.jpg', 'photos/plants/rose.jpg', 'photos/me.jpg'] -if __name__ == "__main__": + +def main(): if get_user() == 'test': unittest.main() else: print 'Will not run tests as any other user except \'test\' (current user: %s).' % get_user() + + +if __name__ == "__main__": + main() + diff --git a/snf-pithos-tools/setup.py b/snf-pithos-tools/setup.py index 8bdf548..e1feb37 100644 --- a/snf-pithos-tools/setup.py +++ b/snf-pithos-tools/setup.py @@ -60,10 +60,6 @@ CLASSIFIERS = [] # Package requirements INSTALL_REQUIRES = [ - 'Django>=1.2.3', - 'SQLAlchemy>=0.6.3', - 'MySQL-python>=1.2.2', - 'psycopg2>=2.2.1' ] EXTRAS_REQUIRES = { @@ -193,6 +189,11 @@ setup( entry_points = { 'console_scripts': [ + 'snf-pithos-sh = pithos.tools.sh:main', + 'snf-pithos-sync = pithos.tools.sync:main', + 'snf-pithos-test = pithos.tools.test:main', + 'snf-pithos-fs = pithos.tools.fs:main', + 'snf-pithos-dispatcher = pithos.tools.dispatcher:main', ], }, ) -- 1.7.10.4