PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms 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. #
  23. import getopt, sys
  24. import boto.sqs
  25. from boto.sqs.connection import SQSConnection
  26. from boto.exception import SQSError
  27. def usage():
  28. print 'cq [-c] [-q queue_name] [-o output_file] [-t timeout] [-r region]'
  29. def main():
  30. try:
  31. opts, args = getopt.getopt(sys.argv[1:], 'hcq:o:t:r:',
  32. ['help', 'clear', 'queue=',
  33. 'output=', 'timeout=', 'region='])
  34. except:
  35. usage()
  36. sys.exit(2)
  37. queue_name = ''
  38. output_file = ''
  39. timeout = 30
  40. region = ''
  41. clear = False
  42. for o, a in opts:
  43. if o in ('-h', '--help'):
  44. usage()
  45. sys.exit()
  46. if o in ('-q', '--queue'):
  47. queue_name = a
  48. if o in ('-o', '--output'):
  49. output_file = a
  50. if o in ('-c', '--clear'):
  51. clear = True
  52. if o in ('-t', '--timeout'):
  53. timeout = int(a)
  54. if o in ('-r', '--region'):
  55. region = a
  56. if region:
  57. c = boto.sqs.connect_to_region(region)
  58. else:
  59. c = SQSConnection()
  60. if queue_name:
  61. try:
  62. rs = [c.create_queue(queue_name)]
  63. except SQSError, e:
  64. print 'An Error Occurred:'
  65. print '%s: %s' % (e.status, e.reason)
  66. print e.body
  67. sys.exit()
  68. else:
  69. try:
  70. rs = c.get_all_queues()
  71. except SQSError, e:
  72. print 'An Error Occurred:'
  73. print '%s: %s' % (e.status, e.reason)
  74. print e.body
  75. sys.exit()
  76. for q in rs:
  77. if clear:
  78. n = q.clear()
  79. print 'clearing %d messages from %s' % (n, q.id)
  80. elif output_file:
  81. q.dump(output_file)
  82. else:
  83. print q.id, q.count(vtimeout=timeout)
  84. if __name__ == "__main__":
  85. main()