3 from __future__
import print_function
9 import http.client
as httplib
15 import urllib.request
as urllib2
20 from commands
import getstatusoutput
22 from subprocess
import getstatusoutput
25 from Monitoring.DQM
import visDQMUtils
27 from DQMServices.FileIO
import visDQMUtils
29 if sys.version_info[:3] >= (2, 4, 0):
30 HTTPS = httplib.HTTPSConnection
39 HTTPS.__init__(self, host, key_file = ssl_key_file, cert_file = ssl_cert_file, **kwargs)
43 return self.do_open(HTTPSCertAuth, req)
46 return mimetypes.guess_type(filename)[0]
or 'application/octet-stream'
50 Encode form (name, value) and (name, filename, type) elements into
51 multi-part/form-data. We don't actually need to know what we are
52 uploading here, so just claim it's all text/plain.
54 boundary = b
'----------=_DQM_FILE_BOUNDARY_=-----------'
55 (body, crlf) = (b
'', b
'\r\n')
56 for (key, value)
in args.items():
58 body += b
'--' + boundary + crlf
59 body += (b
'Content-Disposition: form-data; name="%s"' % key.encode(
'utf-8')) + crlf
60 body += crlf + payload + crlf
61 for (key, filename)
in files.items():
62 body += b
'--' + boundary + crlf
63 body += (b
'Content-Disposition: form-data; name="%s"; filename="%s"'
64 % (key.encode(
'utf-8'), os.path.basename(filename).
encode(
'utf-8'))) + crlf
65 body += (b
'Content-Type: %s' %
filetype(filename).
encode(
'utf-8')) + crlf
66 body += (b
'Content-Length: %d' % os.stat(filename)[ST_SIZE]) + crlf
67 with open(filename,
'rb')
as file:
68 body += crlf + file.read() + crlf
69 body += b
'--' + boundary + b
'--' + crlf + crlf
70 return (b
'multipart/form-data; boundary=' + boundary, body)
74 Marshalls the arguments to the CGI script as multi-part/form-data,
75 not the default application/x-www-form-url-encoded. This improves
76 the transfer of the large inputs and eases command line invocation
79 (type, body) =
encode(args, files)
80 request.add_header(
'Content-Type', type)
81 request.add_header(
'Content-Length',
str(len(body)))
85 ident =
"visDQMUpload DQMGUI/%s python/%s" % \
86 (os.getenv(
'DQMGUI_VERSION',
'?'),
"%d.%d.%d" % sys.version_info[:3])
87 datareq = urllib2.Request(url +
'/data/put')
88 datareq.add_header(
'Accept-encoding',
'gzip')
89 datareq.add_header(
'User-agent', ident)
94 result = urllib2.build_opener(urllib2.ProxyHandler({})).open(datareq)
97 if result.headers.get (
'Content-encoding',
'') ==
'gzip':
98 data = gzip.GzipFile (fileobj=StringIO(data)).read ()
99 return (result.headers, data)
101 x509_path = os.getenv(
"X509_USER_PROXY",
None)
102 if x509_path
and os.path.exists(x509_path):
103 ssl_key_file = ssl_cert_file = x509_path
106 x509_path = os.getenv(
"X509_USER_KEY",
None)
107 if x509_path
and os.path.exists(x509_path):
108 ssl_key_file = x509_path
110 if not ssl_cert_file:
111 x509_path = os.getenv(
"X509_USER_CERT",
None)
112 if x509_path
and os.path.exists(x509_path):
113 ssl_cert_file = x509_path
115 if not ssl_key_file
and not ssl_cert_file:
116 (status, uid) = getstatusoutput(
"id -u")
117 if os.path.exists(
"/tmp/x509up_u%s" % uid):
118 ssl_key_file = ssl_cert_file =
"/tmp/x509up_u%s" % uid
121 x509_path = os.getenv(
"HOME") +
"/.globus/userkey.pem"
122 if os.path.exists(x509_path):
123 ssl_key_file = x509_path
125 if not ssl_cert_file:
126 x509_path = os.getenv(
"HOME") +
"/.globus/usercert.pem"
127 if os.path.exists(x509_path):
128 ssl_cert_file = x509_path
130 if 'https://' in sys.argv[1]
and (
not ssl_key_file
or not os.path.exists(ssl_key_file)):
131 print(
"no certificate private key file found", file=sys.stderr)
134 if 'https://' in sys.argv[1]
and (
not ssl_cert_file
or not os.path.exists(ssl_cert_file)):
135 print(
"no certificate public key file found", file=sys.stderr)
139 for file_path
in sys.argv[2:]:
143 if not classification_ok:
144 print(
"Check of filename before upload failed with following message:")
145 print(classification_result)
149 print(
"Using SSL private key", ssl_key_file)
150 print(
"Using SSL public key", ssl_cert_file)
152 hasher = hashlib.md5()
153 with open(sys.argv[2],
'rb')
as file:
159 {
'size': os.stat(sys.argv[2])[ST_SIZE],
160 'checksum':
'md5:%s' % hasher.hexdigest() },
161 {
'file': file_path })
162 print(
'Status code: ', headers.get(
"Dqm-Status-Code",
"None"))
163 print(
'Message: ', headers.get(
"Dqm-Status-Message",
"None"))
164 print(
'Detail: ', headers.get(
"Dqm-Status-Detail",
"None"))
165 print(data.decode(
'utf-8'))
167 except urllib2.HTTPError
as e:
169 print(
'Status code: ', e.hdrs.get(
"Dqm-Status-Code",
"None"))
170 print(
'Message: ', e.hdrs.get(
"Dqm-Status-Message",
"None"))
171 print(
'Detail: ', e.hdrs.get(
"Dqm-Status-Detail",
"None"))