-
Notifications
You must be signed in to change notification settings - Fork 1
/
consume.py
executable file
·52 lines (47 loc) · 1.65 KB
/
consume.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/python
from optparse import OptionParser
from lib import swarmtoolscore
import sys
import httplib
import os
import signal
conn = None
def usage(script_name):
print "%s [consume] \n"%(script_name)
print "Use '%s [method] --help' for a method's usage and options."%(script_name)
sys.exit()
def signal_handler(signal, frame):
global conn
conn.close()
sys.exit(0)
def consume(hostname, api_key, swarm_id, resource_id):
global conn
conn = httplib.HTTPConnection(hostname)
conn.request("GET", "/stream?swarm_id=%s&resource_id=%s"%(swarm_id, resource_id), None, {"x-bugswarmapikey":api_key})
resp = conn.getresponse()
while(1):
txt = resp.read(1)
sys.stdout.write(txt)
sys.stdout.flush()
conn.close();
def main():
server_info = swarmtoolscore.get_server_info()
keys = swarmtoolscore.get_keys()
if len(sys.argv) == 1:
usage(sys.argv[0])
elif sys.argv[1] == "consume":
signal.signal(signal.SIGINT, signal_handler)
opt_usage = "usage: \n %s SWARM_ID RESOURCE_ID"%(sys.argv[1])
opt_usage += "\n\n *SWARM_ID: The ID of the swarm to consume." \
+"\n *RESOURCE_ID: The ID of the resource to use for consumption."
parser = OptionParser(usage = opt_usage)
(options, args) = parser.parse_args()
if len(args) != 3:
print "Invalid number of args. See --help for correct usage."
sys.exit()
swarm_id = args[1]
resource_id = args[2]
consume(server_info["hostname"], keys["participation"], swarm_id, resource_id)
else:
usage(sys.argv[0])
main()