Revision: | 1.1 |
Committed: | Tue Oct 26 11:00:02 2010 UTC (14 years, 6 months ago) by mcinquil |
Content type: | text/x-python |
Branch: | MAIN |
CVS Tags: | CRAB_2_9_1, CRAB_2_9_1_pre2, CRAB_2_9_1_pre1, CRAB_2_9_0, CRAB_2_9_0_pre2, CRAB_2_9_0_pre1, CRAB_2_8_8, CRAB_2_8_8_pre1, CRAB_2_8_7_patch3, CRAB_2_8_7_patch2, CRAB_2_8_7_patch1, CRAB_2_8_7, CRAB_2_8_7_pre2, CRAB_2_8_7_pre1, CRAB_2_8_6, CRAB_2_8_6_pre1, CRAB_2_8_5_patch3, CRAB_2_8_5_patch2, CRAB_2_8_5_patch1, CRAB_2_8_5, CRAB_2_8_5_pre5, CRAB_2_8_5_pre4, CRAB_2_8_5_pre3, CRAB_2_8_4_patch3, CRAB_2_8_5_pre2, CRAB_2_8_4_patch2, CRAB_2_8_5_pre1, CRAB_2_8_4_patch1, CRAB_2_8_4, CRAB_2_8_4_pre5, CRAB_2_8_4_pre4, CRAB_2_8_4_pre3, CRAB_2_8_4_pre2, CRAB_2_8_4_pre1, CRAB_2_8_3, CRAB_2_8_3_pre4, CRAB_2_8_3_pre3, CRAB_2_8_3_pre2, CRAB_2_8_3_pre1, CRAB_2_8_2_patch1, CRAB_2_8_2, CRAB_2_8_2_pre5, CRAB_2_8_2_pre4, CRAB_2_8_2_pre3, CRAB_2_8_2_pre2, CRAB_2_8_2_pre1, CRAB_2_8_1, CRAB_2_8_0, CRAB_2_8_0_pre1, CRAB_2_7_10_pre3, CRAB_2_7_9_patch2_pre1, CRAB_2_7_10_pre2, CRAB_2_7_10_pre1, CRAB_2_7_9_patch1, CRAB_2_7_9, CRAB_2_7_9_pre5, CRAB_2_7_9_pre4, CRAB_2_7_9_pre3, CRAB_2_7_9_pre2, CRAB_2_7_8_patch2, CRAB_2_7_9_pre1, CRAB_2_7_8_patch2_pre1, CRAB_2_7_8_patch1, CRAB_2_7_8_patch1_pre1, CRAB_2_7_8, CRAB_2_7_8_pre3, CRAB_2_7_8_pre2, CRAB_2_7_8_dash3, CRAB_2_7_8_dash2, CRAB_2_7_8_dash, CRAB_2_7_7_patch1, CRAB_2_7_7_patch1_pre1, CRAB_2_7_8_pre1, CRAB_2_7_7, CRAB_2_7_7_pre2, CRAB_2_7_7_pre1, CRAB_2_7_6_patch1, CRAB_2_7_6, CRAB_2_7_6_pre1, HEAD |
Error occurred while calculating annotation data. | |
Log Message: | Adding error report tool -uploadLog [jobid] |
# | Content |
---|---|
1 | import mimetypes |
2 | import urllib2 |
3 | import httplib |
4 | import string |
5 | import random |
6 | |
7 | uploadFileServer= "http://gangamon.cern.ch/django/cmserrorreports/" |
8 | |
9 | def random_string (length): |
10 | return ''.join ([random.choice (string.letters) for ii in range (length + 1)]) |
11 | |
12 | def encode_multipart_data (files): |
13 | boundary = random_string (30) |
14 | |
15 | def get_content_type (filename): |
16 | return mimetypes.guess_type (filename)[0] or 'application/octet-stream' |
17 | |
18 | def encode_file (field_name): |
19 | filename = files [field_name] |
20 | return ('--' + boundary, |
21 | 'Content-Disposition: form-data; name="%s"; filename="%s"' % (field_name, filename), |
22 | 'Content-Type: %s' % get_content_type(filename), |
23 | '', open (filename, 'rb').read ()) |
24 | |
25 | lines = [] |
26 | for name in files: |
27 | lines.extend (encode_file (name)) |
28 | lines.extend (('--%s--' % boundary, '')) |
29 | body = '\r\n'.join (lines) |
30 | |
31 | headers = {'content-type': 'multipart/form-data; boundary=' + boundary, |
32 | 'content-length': str (len (body))} |
33 | |
34 | return body, headers |
35 | |
36 | |
37 | def run_upload (server, path): |
38 | |
39 | upload_file = make_upload_file (server) |
40 | return upload_file (path) |
41 | |
42 | def make_upload_file (server): |
43 | |
44 | def upload_file (path): |
45 | |
46 | #print 'Uploading %r to %r' % (path, server) |
47 | |
48 | data = {'MAX_FILE_SIZE': '3145728', |
49 | 'sub': '', |
50 | 'mode': 'regist'} |
51 | files = {'file': path} |
52 | |
53 | return send_post (server, files) |
54 | |
55 | return upload_file |
56 | |
57 | def send_post (url, files): |
58 | |
59 | req = urllib2.Request (url) |
60 | connection = httplib.HTTPConnection (req.get_host ()) |
61 | connection.request ('POST', req.get_selector (), |
62 | *encode_multipart_data (files)) |
63 | response = connection.getresponse () |
64 | |
65 | responseResult = response.read() |
66 | |
67 | responseResult = responseResult[responseResult.find("<span id=\"download_path\""):] |
68 | startIndex = responseResult.find("path:") + 5 |
69 | endIndex = responseResult.find("</span>") |
70 | |
71 | return responseResult[startIndex:endIndex] |
72 | |
73 | if __name__ == "__main__": |
74 | import sys |
75 | |
76 | try: |
77 | report_path = sys.argv[1] |
78 | except IndexError: |
79 | print "Specify an argument" |
80 | sys.exit(-1) |
81 | |
82 | run_upload(server=uploadFileServer, path=report_path) |