PageRenderTime 13ms CodeModel.GetById 9ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/boto-2.5.2/bin/cq

#
#! | 89 lines | 85 code | 4 blank | 0 comment | 0 complexity | eb0fb88783ce3ce07e9fc9a1846cf8a3 MD5 | raw file
 1#!/usr/bin/env python
 2# Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/
 3#
 4# Permission is hereby granted, free of charge, to any person obtaining a
 5# copy of this software and associated documentation files (the
 6# "Software"), to deal in the Software without restriction, including
 7# without limitation the rights to use, copy, modify, merge, publish, dis-
 8# tribute, sublicense, and/or sell copies of the Software, and to permit
 9# persons to whom the Software is furnished to do so, subject to the fol-
10# lowing conditions:
11#
12# The above copyright notice and this permission notice shall be included
13# in all copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
17# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
18# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
19# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21# IN THE SOFTWARE.
22#
23import getopt, sys
24import boto.sqs
25from boto.sqs.connection import SQSConnection
26from boto.exception import SQSError
27
28def usage():
29    print 'cq [-c] [-q queue_name] [-o output_file] [-t timeout] [-r region]'
30  
31def main():
32    try:
33        opts, args = getopt.getopt(sys.argv[1:], 'hcq:o:t:r:',
34                                   ['help', 'clear', 'queue=',
35                                    'output=', 'timeout=', 'region='])
36    except:
37        usage()
38        sys.exit(2)
39    queue_name = ''
40    output_file = ''
41    timeout = 30
42    region = ''
43    clear = False
44    for o, a in opts:
45        if o in ('-h', '--help'):
46            usage()
47            sys.exit()
48        if o in ('-q', '--queue'):
49            queue_name = a
50        if o in ('-o', '--output'):
51            output_file = a
52        if o in ('-c', '--clear'):
53            clear = True
54        if o in ('-t', '--timeout'):
55            timeout = int(a)
56        if o in ('-r', '--region'):
57            region = a
58    if region:
59        c = boto.sqs.connect_to_region(region)
60    else:
61        c = SQSConnection()
62    if queue_name:
63        try:
64            rs = [c.create_queue(queue_name)]
65        except SQSError, e:
66            print 'An Error Occurred:'
67            print '%s: %s' % (e.status, e.reason)
68            print e.body
69            sys.exit()
70    else:
71        try:
72            rs = c.get_all_queues()
73        except SQSError, e:
74            print 'An Error Occurred:'
75            print '%s: %s' % (e.status, e.reason)
76            print e.body
77            sys.exit()
78    for q in rs:
79        if clear:
80            n = q.clear()
81            print 'clearing %d messages from %s' % (n, q.id)
82        elif output_file:
83            q.dump(output_file)
84        else:
85            print q.id, q.count(vtimeout=timeout)
86
87if __name__ == "__main__":
88    main()
89