comparison tools/condel/condel_web.py @ 0:742c7ee37f8b

push
author Saket Choudhary <saketkc@gmail.com>
date Wed, 20 Nov 2013 00:48:30 +0530
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:742c7ee37f8b
1 import sys
2 sys.path.insert(0, '/home/saket/requests-new-urllib3-api/requests/packages/')
3 sys.path.insert(0, '/home/saket/requests-new-urllib3-api')
4
5
6 import requests
7 import pycurl
8 import os
9 from os.path import getsize
10 import argparse
11 import sys
12 import cStringIO
13 from functools import wraps
14 import tempfile, shutil,time
15
16 __url__="http://bg.upf.edu/condel/taskService"
17 def stop_err( msg ):
18 sys.stderr.write( '%s\n' % msg )
19 sys.exit()
20
21 def retry(ExceptionToCheck, tries=10, delay=3, backoff=2, logger=None):
22 """Retry calling the decorated function using an exponential backoff.
23
24 http://www.saltycrane.com/blog/2009/11/trying-out-retry-decorator-python/
25 original from: http://wiki.python.org/moin/PythonDecoratorLibrary#Retry
26
27 :param ExceptionToCheck: the exception to check. may be a tuple of
28 exceptions to check
29 :type ExceptionToCheck: Exception or tuple
30 :param tries: number of times to try (not retry) before giving up
31 :type tries: int
32 :param delay: initial delay between retries in seconds
33 :type delay: int
34 :param backoff: backoff multiplier e.g. value of 2 will double the delay
35 each retry
36 :type backoff: int
37 :param logger: logger to use. If None, print
38 :type logger: logging.Logger instance
39 """
40 def deco_retry(f):
41
42 @wraps(f)
43 def f_retry(*args, **kwargs):
44 mtries, mdelay = tries, delay
45 while mtries > 1:
46 try:
47 return f(*args, **kwargs)
48 except ExceptionToCheck, e:
49 #msg = "%s, Retrying in %d seconds..." % (str(e), mdelay)
50 msg = "Retrying in %d seconds..." % (mdelay)
51 if logger:
52 logger.warning(msg)
53 else:
54 #print msg
55 pass
56 time.sleep(mdelay)
57 mtries -= 1
58 mdelay *= backoff
59 return f(*args, **kwargs)
60
61 return f_retry # true decorator
62
63 return deco_retry
64
65 class TransficUploader:
66 def __init__(self):
67
68 self.c = pycurl.Curl()
69 self.c.setopt(pycurl.URL, __url__)
70 self.c.setopt(pycurl.UPLOAD, 1)
71 #c.setopt(pycurl.USERPWD, 'saket.kumar:whatsinaname.')
72 self.c.setopt(pycurl.PROXY, 'http://saket.kumar:whatsinaname.@netmon.iitb.ac.in:80/')
73 #c.setopt(pycurl.PROXYPORT, 80)
74 #c.setopt(pycurl.PROXYTYPE_HTTP,1)
75 #self.c.setopt(pycurl.VERBOSE, 1)
76 self.c.setopt(pycurl.HTTPHEADER, ['Expect:'])
77 #c.setopt(pycurl.HTTPPROXYTUNNEL, 1)
78 self.c.setopt(pycurl.UPLOAD, 1)
79 #c.perform()
80 self.c.setopt(pycurl.NOPROGRESS, 1);
81 #curl_easy_setopt(hnd, CURLOPT_UPLOAD, 1L);
82 self.c.setopt(pycurl.USERAGENT, "curl/7.27.0");
83 #curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, slist1);
84 #curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
85 self.c.setopt(pycurl.SSL_VERIFYPEER, 1);
86 self.c.setopt(pycurl.CUSTOMREQUEST, "PUT")
87 #curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
88 self.c.setopt(pycurl.TCP_NODELAY, 1);
89 self.buf = cStringIO.StringIO()
90 self.c.setopt(self.c.WRITEFUNCTION, self.buf.write)
91
92
93 def upload_file(self,filepath):
94 f = open(filepath)
95 self.c.setopt(pycurl.INFILE, f)
96 self.c.setopt(pycurl.INFILESIZE, getsize(filepath))
97
98 def run(self):
99 self.c.perform()
100
101 def get_url(self):
102 return self.buf.getvalue().strip()
103
104 @retry(requests.exceptions.HTTPError)
105 def result_exists(self,url ):
106 #url="http://www.cravat.us/results/%s/%s.zip" %(job_id,job_id)
107 download_request = requests.request("GET", url)
108 if download_request.status_code==404 or download_request==500:
109 raise requests.HTTPError()
110 else:
111 return url
112 @retry(requests.exceptions.HTTPError)
113 def download_result(self, url,outpath):
114 tmp_dir = tempfile.mkdtemp()
115 r = requests.get( url, stream=True )
116 if r.status_code == 500:
117 raise requests.HTTPError()
118 else:
119 path = os.path.join( tmp_dir,"results.csv")
120 with open(path, 'wb') as f:
121 for chunk in r.iter_content(128):
122 f.write(chunk)
123 shutil.move(path,outpath)
124 shutil.rmtree(tmp_dir)
125
126
127
128 def main(params):
129 parser = argparse.ArgumentParser()
130 parser.add_argument("--input",type=str,required=True)
131 parser.add_argument("--output",type=str,required=True)
132 args = parser.parse_args(params)
133 uploader = TransficUploader();
134 uploader.upload_file(args.input)
135 uploader.run()
136 url = uploader.get_url()
137 url = uploader.result_exists(url)
138 download = uploader.download_result(url,args.output)
139 if __name__=="__main__":
140 main(sys.argv[1:])