14 from commands
import getstatusoutput
15 from cStringIO
import StringIO
18 from Monitoring.DQM
import visDQMUtils
23 if sys.version_info[:3] >= (2, 4, 0):
24 HTTPS = httplib.HTTPSConnection
31 HTTPS.__init__(self, host, key_file = ssl_key_file, cert_file = ssl_cert_file, **kwargs)
35 return self.do_open(HTTPSCertAuth, req)
38 return mimetypes.guess_type(filename)[0]
or 'application/octet-stream' 42 Encode form (name, value) and (name, filename, type) elements into 43 multi-part/form-data. We don't actually need to know what we are 44 uploading here, so just claim it's all text/plain. 46 boundary =
'----------=_DQM_FILE_BOUNDARY_=-----------' 47 (body, crlf) = (
'',
'\r\n')
48 for (key, value)
in args.items():
50 body +=
'--' + boundary + crlf
51 body += (
'Content-Disposition: form-data; name="%s"' % key) + crlf
52 body += crlf + payload + crlf
53 for (key, filename)
in files.items():
54 body +=
'--' + boundary + crlf
55 body += (
'Content-Disposition: form-data; name="%s"; filename="%s"' 56 % (key, os.path.basename(filename))) + crlf
57 body += (
'Content-Type: %s' %
filetype(filename)) + crlf
58 body += (
'Content-Length: %d' % os.stat(filename)[ST_SIZE]) + crlf
59 body += crlf + open(filename,
"r").read() + crlf 60 body += '--' + boundary +
'--' + crlf + crlf
61 return (
'multipart/form-data; boundary=' + boundary, body)
65 Marshalls the arguments to the CGI script as multi-part/form-data, 66 not the default application/x-www-form-url-encoded. This improves 67 the transfer of the large inputs and eases command line invocation 70 (type, body) =
encode(args, files)
71 request.add_header(
'Content-Type', type)
72 request.add_header(
'Content-Length',
str(len(body)))
73 request.add_data(body)
76 ident =
"visDQMUpload DQMGUI/%s python/%s" % \
77 (os.getenv(
'DQMGUI_VERSION',
'?'),
"%d.%d.%d" % sys.version_info[:3])
78 datareq = urllib2.Request(url +
'/data/put')
79 datareq.add_header(
'Accept-encoding',
'gzip')
80 datareq.add_header(
'User-agent', ident)
85 result = urllib2.build_opener(urllib2.ProxyHandler({})).open(datareq)
88 if result.headers.get (
'Content-encoding',
'') ==
'gzip':
89 data = gzip.GzipFile (fileobj=StringIO(data)).read ()
90 return (result.headers, data)
92 x509_path = os.getenv(
"X509_USER_PROXY",
None)
93 if x509_path
and os.path.exists(x509_path):
94 ssl_key_file = ssl_cert_file = x509_path
97 x509_path = os.getenv(
"X509_USER_KEY",
None)
98 if x509_path
and os.path.exists(x509_path):
99 ssl_key_file = x509_path
101 if not ssl_cert_file:
102 x509_path = os.getenv(
"X509_USER_CERT",
None)
103 if x509_path
and os.path.exists(x509_path):
104 ssl_cert_file = x509_path
106 if not ssl_key_file
and not ssl_cert_file:
107 (status, uid) = getstatusoutput(
"id -u")
108 if os.path.exists(
"/tmp/x509up_u%s" % uid):
109 ssl_key_file = ssl_cert_file =
"/tmp/x509up_u%s" % uid
112 x509_path = os.getenv(
"HOME") +
"/.globus/userkey.pem" 113 if os.path.exists(x509_path):
114 ssl_key_file = x509_path
116 if not ssl_cert_file:
117 x509_path = os.getenv(
"HOME") +
"/.globus/usercert.pem" 118 if os.path.exists(x509_path):
119 ssl_cert_file = x509_path
121 if 'https://' in sys.argv[1]
and (
not ssl_key_file
or not os.path.exists(ssl_key_file)):
122 print >>sys.stderr,
"no certificate private key file found" 125 if 'https://' in sys.argv[1]
and (
not ssl_cert_file
or not os.path.exists(ssl_cert_file)):
126 print >>sys.stderr,
"no certificate public key file found" 130 for file_path
in sys.argv[2:]:
134 if not classification_ok:
135 print "Check of filename before upload failed with following message:" 136 print classification_result
140 print "Using SSL private key", ssl_key_file
141 print "Using SSL public key", ssl_cert_file
144 {
'size': os.stat(sys.argv[2])[ST_SIZE],
145 'checksum':
"md5:%s" % hashlib.md5(
file(sys.argv[2]).read()).hexdigest() },
146 {
'file': file_path })
147 print 'Status code: ', headers.get(
"Dqm-Status-Code",
"None")
148 print 'Message: ', headers.get(
"Dqm-Status-Message",
"None")
149 print 'Detail: ', headers.get(
"Dqm-Status-Detail",
"None")
152 except urllib2.HTTPError, e:
154 print 'Status code: ', e.hdrs.get(
"Dqm-Status-Code",
"None")
155 print 'Message: ', e.hdrs.get(
"Dqm-Status-Message",
"None")
156 print 'Detail: ', e.hdrs.get(
"Dqm-Status-Detail",
"None")
def upload(url, args, files)
def default_open(self, req)
def __init__(self, host, args, kwargs)
def marshall(args, files, request)
def classifyDQMFile(path)