3 from __future__
import print_function
9 import http.client
as httplib
16 import urllib.request
as urllib2
21 from commands
import getstatusoutput
23 from subprocess
import getstatusoutput
26 from Monitoring.DQM
import visDQMUtils
28 from DQMServices.FileIO
import visDQMUtils
30 if sys.version_info[:3] >= (2, 4, 0):
31 HTTPS = httplib.HTTPSConnection
40 def __init__(self, host, context = None, *args, **kwargs):
42 context = ssl._create_default_https_context()
43 if ssl_key_file
or ssl_cert_file:
44 context.load_cert_chain(ssl_cert_file, ssl_key_file)
45 HTTPS.__init__(self, host, context = context, **kwargs)
49 return self.do_open(HTTPSCertAuth, req)
52 return mimetypes.guess_type(filename)[0]
or 'application/octet-stream'
56 Encode form (name, value) and (name, filename, type) elements into
57 multi-part/form-data. We don't actually need to know what we are
58 uploading here, so just claim it's all text/plain.
60 boundary = b
'----------=_DQM_FILE_BOUNDARY_=-----------'
61 (body, crlf) = (b
'', b
'\r\n')
62 for (key, value)
in args.items():
64 body += b
'--' + boundary + crlf
65 body += (b
'Content-Disposition: form-data; name="%s"' % key.encode(
'utf-8')) + crlf
66 body += crlf + payload + crlf
67 for (key, filename)
in files.items():
68 body += b
'--' + boundary + crlf
69 body += (b
'Content-Disposition: form-data; name="%s"; filename="%s"'
70 % (key.encode(
'utf-8'), os.path.basename(filename).
encode(
'utf-8'))) + crlf
71 body += (b
'Content-Type: %s' %
filetype(filename).
encode(
'utf-8')) + crlf
72 body += (b
'Content-Length: %d' % os.stat(filename)[ST_SIZE]) + crlf
73 with open(filename,
'rb')
as file:
74 body += crlf + file.read() + crlf
75 body += b
'--' + boundary + b
'--' + crlf + crlf
76 return (b
'multipart/form-data; boundary=' + boundary, body)
80 Marshalls the arguments to the CGI script as multi-part/form-data,
81 not the default application/x-www-form-url-encoded. This improves
82 the transfer of the large inputs and eases command line invocation
85 (type, body) =
encode(args, files)
86 request.add_header(
'Content-Type', type)
87 request.add_header(
'Content-Length',
str(len(body)))
91 ident =
"visDQMUpload DQMGUI/%s python/%s" % \
92 (os.getenv(
'DQMGUI_VERSION',
'?'),
"%d.%d.%d" % sys.version_info[:3])
93 datareq = urllib2.Request(url +
'/data/put')
94 datareq.add_header(
'Accept-encoding',
'gzip')
95 datareq.add_header(
'User-agent', ident)
100 result = urllib2.build_opener(urllib2.ProxyHandler({})).open(datareq)
103 if result.headers.get (
'Content-encoding',
'') ==
'gzip':
104 data = gzip.GzipFile (fileobj=StringIO(data)).read ()
105 return (result.headers, data)
107 x509_path = os.getenv(
"X509_USER_PROXY",
None)
108 if x509_path
and os.path.exists(x509_path):
109 ssl_key_file = ssl_cert_file = x509_path
112 x509_path = os.getenv(
"X509_USER_KEY",
None)
113 if x509_path
and os.path.exists(x509_path):
114 ssl_key_file = x509_path
116 if not ssl_cert_file:
117 x509_path = os.getenv(
"X509_USER_CERT",
None)
118 if x509_path
and os.path.exists(x509_path):
119 ssl_cert_file = x509_path
121 if not ssl_key_file
and not ssl_cert_file:
122 (status, uid) = getstatusoutput(
"id -u")
123 if os.path.exists(
"/tmp/x509up_u%s" % uid):
124 ssl_key_file = ssl_cert_file =
"/tmp/x509up_u%s" % uid
127 x509_path = os.getenv(
"HOME") +
"/.globus/userkey.pem"
128 if os.path.exists(x509_path):
129 ssl_key_file = x509_path
131 if not ssl_cert_file:
132 x509_path = os.getenv(
"HOME") +
"/.globus/usercert.pem"
133 if os.path.exists(x509_path):
134 ssl_cert_file = x509_path
136 if 'https://' in sys.argv[1]
and (
not ssl_key_file
or not os.path.exists(ssl_key_file)):
137 print(
"no certificate private key file found", file=sys.stderr)
140 if 'https://' in sys.argv[1]
and (
not ssl_cert_file
or not os.path.exists(ssl_cert_file)):
141 print(
"no certificate public key file found", file=sys.stderr)
145 for file_path
in sys.argv[2:]:
149 if not classification_ok:
150 print(
"Check of filename before upload failed with following message:")
151 print(classification_result)
155 print(
"Using SSL private key", ssl_key_file)
156 print(
"Using SSL public key", ssl_cert_file)
158 hasher = hashlib.md5()
159 with open(sys.argv[2],
'rb')
as file:
165 {
'size': os.stat(sys.argv[2])[ST_SIZE],
166 'checksum':
'md5:%s' % hasher.hexdigest() },
167 {
'file': file_path })
168 print(
'Status code: ', headers.get(
"Dqm-Status-Code",
"None"))
169 print(
'Message: ', headers.get(
"Dqm-Status-Message",
"None"))
170 print(
'Detail: ', headers.get(
"Dqm-Status-Detail",
"None"))
171 print(data.decode(
'utf-8'))
173 except urllib2.HTTPError
as e:
175 print(
'Status code: ', e.hdrs.get(
"Dqm-Status-Code",
"None"))
176 print(
'Message: ', e.hdrs.get(
"Dqm-Status-Message",
"None"))
177 print(
'Detail: ', e.hdrs.get(
"Dqm-Status-Detail",
"None"))