forked from baz/app-sales-machine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_tasks.py
86 lines (66 loc) · 2.26 KB
/
run_tasks.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from urllib2 import *
from BeautifulSoup import *
import sys
COOKIE = 'uid=thaye0mr; dev_appserver_login="[email protected]:True:185804764220139124118"'
def main(queue):
failed_tasks = set()
soup = BeautifulSoup(''.join(urlopen(Request(url='http://localhost:8080/_ah/admin/tasks?queue='+queue,
headers={'Cookie': COOKIE }))
.readlines()))
while True:
task = None
for t in soup.findAll(id=re.compile("^runform")):
task = parse(t)
if task['name'] in failed_tasks:
task = None
else:
break
if task is None:
if len(failed_tasks) != 0:
print 'There are',len(failed_tasks),'failed tasks in',queue
else:
print 'There are no tasks in',queue
return
print 'Executing task:',task['name']
try:
response = ''.join(urlopen(Request(
url='http://localhost:8080'+task['action'],
headers=task['headers'],
data = task['payload']
)).readlines())
soup = BeautifulSoup(delete_task(task,queue))
except Exception,e:
print e
failed_tasks.add(task['name'])
def delete_task(task,queue):
import urllib
#print 'deleting'
return ''.join(urlopen(Request(
url='http://localhost:8080/_ah/admin/tasks',
headers={'Cookie': COOKIE },
data = urllib.urlencode([('queue',queue),
('task',task['headers']['X-AppEngine-TaskName']),
('action:deletetask','true')])
)).readlines())
def parse(form):
res = {}
HEADER_KEY = 'header:'
res['action'] = form['action']
res['method'] = form['method']
res['headers'] = {'Cookie': COOKIE}
for e in form:
if isinstance(e,NavigableString) or e.name != 'input':
continue
attrs = dict(e.attrs)
if attrs.get('type','') != 'hidden':
continue
key = attrs['name']
value = attrs['value']
if key.startswith(HEADER_KEY):
res['headers'][key[len(HEADER_KEY):]] = value
elif key == 'payload':
res['payload'] = value
res['name'] = res['headers']['X-AppEngine-TaskName']
return res
if __name__ == '__main__':
main(sys.argv[1])