3 import sys, os, os.path, re, string, httplib, mimetypes, urllib, urllib2, httplib, gzip, md5
4 from cStringIO
import StringIO
14 if sys.version_info[:3] >= (2, 4, 0):
15 HTTPS = httplib.HTTPSConnection
22 HTTPS.__init__(self, host, key_file = ssl_key_file, cert_file = ssl_cert_file, **kwargs)
26 return self.do_open(HTTPSCertAuth, req)
29 return mimetypes.guess_type(filename)[0]
or 'application/octet-stream'
33 Encode form (name, value) and (name, filename, type) elements into
34 multi-part/form-data. We don't actually need to know what we are
35 uploading here, so just claim it's all text/plain.
37 boundary =
'----------=_DQM_FILE_BOUNDARY_=-----------'
38 (body, crlf) = (
'',
'\r\n')
39 for (key, value)
in args.items():
40 body +=
'--' + boundary + crlf
41 body += (
'Content-disposition: form-data; name="%s"' % key) + crlf
42 body += crlf + str(value) + crlf
43 for (key, filename)
in files.items():
44 body +=
'--' + boundary + crlf
45 body += (
'Content-Disposition: form-data; name="%s"; filename="%s"'
46 % (key, os.path.basename(filename))) + crlf
47 body += (
'Content-Type: %s' %
filetype(filename)) + crlf
48 body += crlf + open(filename,
"r").read() + crlf
49 body += '--' + boundary +
'--' + crlf + crlf
50 return (
'multipart/form-data; boundary=' + boundary, body)
54 Marshalls the arguments to the CGI script as multi-part/form-data,
55 not the default application/x-www-form-url-encoded. This improves
56 the transfer of the large inputs and eases command line invocation
59 (type, body) =
encode(args, files)
60 request.add_header(
'Content-type', type)
61 request.add_header(
'Content-length', str(len(body)))
62 request.add_data(body)
65 ident =
"visDQMUpload DQMGUI/%s CMSSW/%s python/%s" % \
66 (os.getenv(
'DQMGUI_VERSION',
'?'),
67 os.getenv(
'DQM_CMSSW_VERSION', os.getenv(
'CMSSW_VERSION',
'?')),
68 "%d.%d.%d" % sys.version_info[:3])
70 if url.startswith(
"https:"):
71 authreq = urllib2.Request(url +
'/authenticate')
72 authreq.add_header(
'User-agent', ident)
74 cookie = result.headers.get(
'Set-Cookie')
76 raise RuntimeError(
"Did not receive authentication cookie")
77 cookie = cookie.split(
";")[0]
79 datareq = urllib2.Request(url +
'/data/put')
80 datareq.add_header(
'Accept-encoding',
'gzip')
81 datareq.add_header(
'User-agent', ident)
83 datareq.add_header(
'Cookie', cookie)
85 result = urllib2.build_opener().open(datareq)
87 if result.headers.get (
'Content-encoding',
'') ==
'gzip':
88 data = gzip.GzipFile (fileobj=StringIO(data)).read ()
89 return (result.headers, data)
92 sys.stdout.write(
"\n")
93 sys.stdout.write(
"This scripts intends to do the upload of files to the DQM server. It\n")
94 sys.stdout.write(
"runs some basic checks on the file name as it is crucial to follow\n")
95 sys.stdout.write(
"the naming convention.\n")
96 sys.stdout.write(
"\n")
97 sys.stdout.write(
"Mandatory Option\n")
98 sys.stdout.write(
" -d, --destination parameter to specify the DQM server\n")
99 sys.stdout.write(
"\n")
100 sys.stdout.write(
" Proxy Options\n")
101 sys.stdout.write(
" The script will try to find your grid proxy automatically. To\n")
102 sys.stdout.write(
" do so, it checks $X509_* environment variables and your globus\n")
103 sys.stdout.write(
" directory (~/.globus). To override this automatism, you can use\n")
104 sys.stdout.write(
" the following two options:\n")
105 sys.stdout.write(
" --ssl-key-file location of your private key\n")
106 sys.stdout.write(
" --ssl-cert-file location of your public key\n")
107 sys.stdout.write(
"\n")
108 sys.stdout.write(
"Other Options\n")
109 sys.stdout.write(
" -h, --help show this help message\n")
110 sys.stdout.write(
" -s, --no-submission suppress the submission\n")
111 sys.stdout.write(
" -r, --no-registration suppress the submission\n")
112 sys.stdout.write(
" --no-filename-check omit the file name check\n")
113 sys.stdout.write(
"\n")
120 if opts.ssl_key_file
and os.path.exists(opts.ssl_key_file):
121 ssl_key_file = opts.ssl_key_file
122 if opts.ssl_cert_file
and os.path.exists(opts.ssl_cert_file):
123 ssl_cert_file = opts.ssl_cert_file
126 x509_path = os.getenv(
"X509_USER_PROXY",
None)
127 if x509_path
and os.path.exists(x509_path):
128 ssl_key_file = ssl_cert_file = x509_path
131 x509_path = os.getenv(
"X509_USER_KEY",
None)
132 if x509_path
and os.path.exists(x509_path):
133 ssl_key_file = x509_path
135 if not ssl_cert_file:
136 x509_path = os.getenv(
"X509_USER_CERT",
None)
137 if x509_path
and os.path.exists(x509_path):
138 ssl_cert_file = x509_path
141 x509_path = os.getenv(
"HOME") +
"/.globus/userkey.pem"
142 if os.path.exists(x509_path):
143 ssl_key_file = x509_path
145 if not ssl_cert_file:
146 x509_path = os.getenv(
"HOME") +
"/.globus/usercert.pem"
147 if os.path.exists(x509_path):
148 ssl_cert_file = x509_path
150 if not ssl_key_file
or not os.path.exists(ssl_key_file):
151 sys.stderr.write(
"no certificate private key file found, please specify one via $X509_USER_PROXY, $X509_USER_KEY or --ssl-key-file\n")
154 if not ssl_cert_file
or not os.path.exists(ssl_cert_file):
155 sys.stderr.write(
"no certificate public key file found, please specify one via $X509_USER_CERT or --ssl-cert-file\n")
158 print "Using SSL private key", ssl_key_file
159 print "Using SSL public key", ssl_cert_file
163 regWhitespace = re.compile(
'.*\s.*')
164 if regWhitespace.match(fileName):
165 sys.stderr.write(
"whitespace detected!\n")
168 regRelval=re.compile(
'.*relval.*')
169 regCMSSW=re.compile(
'.*CMSSW_[0-9]+_[0-9]+_[0-9]+_.*')
170 regCMSSWpre=re.compile(
'.*CMSSW_[0-9]+_[0-9]+_[0-9]+_pre[0-9]+_.*')
171 if regRelval.match(fileName):
173 if not regCMSSW.match(fileName):
186 {
'size': os.stat(filename).st_size,
187 'checksum':
"md5:%s" % md5.new(
file(filename).
read()).hexdigest() },
188 {
'file': filename })
189 print 'Status code: ', headers.get(
"Dqm-Status-Code",
"None")
190 print 'Message: ', headers.get(
"Dqm-Status-Message",
"None")
191 print 'Detail: ', headers.get(
"Dqm-Status-Detail",
"None")
193 except urllib2.HTTPError
as e:
195 print 'Status code: ', e.hdrs.get(
"Dqm-Status-Code",
"None")
196 print 'Message: ', e.hdrs.get(
"Dqm-Status-Message",
"None")
197 print 'Detail: ', e.hdrs.get(
"Dqm-Status-Detail",
"None")
201 filename = filename.split(
"/")[-1]
202 regMC=re.compile(
'.*_R([0-9]*)__*')
203 if regMC.match(filename):
204 m = re.search(
'.*_R([0-9]*)(__.*).root',filename)
206 dataset = m.group(2).
replace(
"__",
"/")
208 m = re.search(
'.*_R([0-9]*)(_?.*).root',filename)
210 dataset = m.group(2).
replace(
"__",
"/")
212 dataset=
"/Global/Online/ALL"
216 return destination+
"start?workspace=summary;dataset="+dataset+
";sampletype=offline_data"
218 return destination+
"start?workspace=summary;runnr="+runNr+
";dataset="+dataset+
";sampletype=online_data"
221 filename = filename.split(
"/")[-1]
222 regMC=re.compile(
'.*_R([0-9]*)__*')
223 if regMC.match(filename):
224 m = re.search(
'.*_R([0-9]*)(__.*).root',filename)
226 dataset = m.group(2).
replace(
"__",
"/")
228 m = re.search(
'.*_R([0-9]*)(_?.*).root',filename)
230 dataset = m.group(2).
replace(
"__",
"/")
232 dataset=
"/Global/Online/ALL"
233 tempurl =
"https://www-ekp.physik.uni-karlsruhe.de/~zeise/cgi-bin/register.py?run="+runNr+
"&dataset="+dataset+
"&filename="+filename+
"&tags="+tags+
"&instance="+destination
234 print "Link that is used to register: ", tempurl
235 urllib.urlopen(tempurl)
239 parser = optparse.OptionParser(add_help_option=
False)
240 parser.add_option(
"-h",
"--help", action=
"callback", callback=print_help),
241 parser.add_option(
"",
"--no-filename-check", dest=
"no_filename_check", default=
False, action=
"store_true")
242 parser.add_option(
"-d",
"--destination", dest=
"destination", default=
"", action=
"store")
243 parser.add_option(
"-t",
"--tags", dest=
"tags", default=
"", action=
"store")
244 parser.add_option(
"-s",
"--no-submission", dest=
"submission", default=
True, action=
"store_false")
245 parser.add_option(
"-r",
"--no-registration", dest=
"registration", default=
True, action=
"store_false")
246 parser.add_option(
"",
"--ssl-key-file", dest=
"ssl_key_file", default=
"", action=
"store")
247 parser.add_option(
"",
"--ssl-cert-file", dest=
"ssl_cert_file", default=
"", action=
"store")
248 (opts, args) = parser.parse_args()
251 if not opts.destination:
252 sys.stderr.write(
"no destination specified\n")
257 sys.stderr.write(
"no input files specified\n")
259 for fileName
in args:
260 fileName=fileName.strip()
261 if not os.path.exists(fileName):
262 sys.stderr.write(
"file '%s' doesn't exist!\n" % fileName)
264 if not opts.no_filename_check
and not checkFileName(fileName):
266 sys.stderr.write(
"file '%s' passed name check, upload will follow!\n" % fileName)
270 sys.stdout.write(
"file '%s' would be uploaded to '%s'\n" % (fileName, opts.destination))
271 if opts.registration:
273 print "You should see the plots here: "+
getURL(fileName, opts.destination)
275 if __name__ ==
'__main__':
276 sys.exit(
main(sys.argv[1:]))
def registerFileAtLogServer