You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
3.0 KiB

4 years ago
  1. #!/home/alpcentaur/ProjektA/PrototypeWebApp/venv/bin/python3.5
  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. if c is None:
  59. print 'Invalid region (%s)' % region
  60. sys.exit(1)
  61. else:
  62. c = SQSConnection()
  63. if queue_name:
  64. try:
  65. rs = [c.create_queue(queue_name)]
  66. except SQSError as e:
  67. print 'An Error Occurred:'
  68. print '%s: %s' % (e.status, e.reason)
  69. print e.body
  70. sys.exit()
  71. else:
  72. try:
  73. rs = c.get_all_queues()
  74. except SQSError as e:
  75. print 'An Error Occurred:'
  76. print '%s: %s' % (e.status, e.reason)
  77. print e.body
  78. sys.exit()
  79. for q in rs:
  80. if clear:
  81. n = q.clear()
  82. print 'clearing %d messages from %s' % (n, q.id)
  83. elif output_file:
  84. q.dump(output_file)
  85. else:
  86. print q.id, q.count(vtimeout=timeout)
  87. if __name__ == "__main__":
  88. main()