3 from __future__
import print_function
4 import sys, os, os.path, re, string, httplib, mimetypes, urllib, urllib2, httplib, gzip, md5
5 from cStringIO
import StringIO
15 if sys.version_info[:3] >= (2, 4, 0):
16 HTTPS = httplib.HTTPSConnection
23 HTTPS.__init__(self, host, key_file = ssl_key_file, cert_file = ssl_cert_file, **kwargs)
27 return self.do_open(HTTPSCertAuth, req)
30 return mimetypes.guess_type(filename)[0]
or 'application/octet-stream' 34 Encode form (name, value) and (name, filename, type) elements into 35 multi-part/form-data. We don't actually need to know what we are 36 uploading here, so just claim it's all text/plain. 38 boundary =
'----------=_DQM_FILE_BOUNDARY_=-----------' 39 (body, crlf) = (
'',
'\r\n')
40 for (key, value)
in args.items():
41 body +=
'--' + boundary + crlf
42 body += (
'Content-disposition: form-data; name="%s"' % key) + crlf
43 body += crlf +
str(value) + crlf
44 for (key, filename)
in files.items():
45 body +=
'--' + boundary + crlf
46 body += (
'Content-Disposition: form-data; name="%s"; filename="%s"' 47 % (key, os.path.basename(filename))) + crlf
48 body += (
'Content-Type: %s' %
filetype(filename)) + crlf
49 body += crlf + open(filename,
"r").read() + crlf 50 body += '--' + boundary +
'--' + crlf + crlf
51 return (
'multipart/form-data; boundary=' + boundary, body)
55 Marshalls the arguments to the CGI script as multi-part/form-data, 56 not the default application/x-www-form-url-encoded. This improves 57 the transfer of the large inputs and eases command line invocation 60 (type, body) =
encode(args, files)
61 request.add_header(
'Content-type', type)
62 request.add_header(
'Content-length',
str(len(body)))
63 request.add_data(body)
66 ident =
"visDQMUpload DQMGUI/%s CMSSW/%s python/%s" % \
67 (os.getenv(
'DQMGUI_VERSION',
'?'),
68 os.getenv(
'DQM_CMSSW_VERSION', os.getenv(
'CMSSW_VERSION',
'?')),
69 "%d.%d.%d" % sys.version_info[:3])
71 if url.startswith(
"https:"):
72 authreq = urllib2.Request(url +
'/authenticate')
73 authreq.add_header(
'User-agent', ident)
75 cookie = result.headers.get(
'Set-Cookie')
77 raise RuntimeError(
"Did not receive authentication cookie")
78 cookie = cookie.split(
";")[0]
80 datareq = urllib2.Request(url +
'/data/put')
81 datareq.add_header(
'Accept-encoding',
'gzip')
82 datareq.add_header(
'User-agent', ident)
84 datareq.add_header(
'Cookie', cookie)
86 result = urllib2.build_opener().open(datareq)
88 if result.headers.get (
'Content-encoding',
'') ==
'gzip':
89 data = gzip.GzipFile (fileobj=StringIO(data)).read ()
90 return (result.headers, data)
93 sys.stdout.write(
"\n")
94 sys.stdout.write(
"This scripts intends to do the upload of files to the DQM server. It\n")
95 sys.stdout.write(
"runs some basic checks on the file name as it is crucial to follow\n")
96 sys.stdout.write(
"the naming convention.\n")
97 sys.stdout.write(
"\n")
98 sys.stdout.write(
"Mandatory Option\n")
99 sys.stdout.write(
" -d, --destination parameter to specify the DQM server\n")
100 sys.stdout.write(
"\n")
101 sys.stdout.write(
" Proxy Options\n")
102 sys.stdout.write(
" The script will try to find your grid proxy automatically. To\n")
103 sys.stdout.write(
" do so, it checks $X509_* environment variables and your globus\n")
104 sys.stdout.write(
" directory (~/.globus). To override this automatism, you can use\n")
105 sys.stdout.write(
" the following two options:\n")
106 sys.stdout.write(
" --ssl-key-file location of your private key\n")
107 sys.stdout.write(
" --ssl-cert-file location of your public key\n")
108 sys.stdout.write(
"\n")
109 sys.stdout.write(
"Other Options\n")
110 sys.stdout.write(
" -h, --help show this help message\n")
111 sys.stdout.write(
" -s, --no-submission suppress the submission\n")
112 sys.stdout.write(
" -r, --no-registration suppress the submission\n")
113 sys.stdout.write(
" --no-filename-check omit the file name check\n")
114 sys.stdout.write(
"\n")
121 if opts.ssl_key_file
and os.path.exists(opts.ssl_key_file):
122 ssl_key_file = opts.ssl_key_file
123 if opts.ssl_cert_file
and os.path.exists(opts.ssl_cert_file):
124 ssl_cert_file = opts.ssl_cert_file
127 x509_path = os.getenv(
"X509_USER_PROXY",
None)
128 if x509_path
and os.path.exists(x509_path):
129 ssl_key_file = ssl_cert_file = x509_path
132 x509_path = os.getenv(
"X509_USER_KEY",
None)
133 if x509_path
and os.path.exists(x509_path):
134 ssl_key_file = x509_path
136 if not ssl_cert_file:
137 x509_path = os.getenv(
"X509_USER_CERT",
None)
138 if x509_path
and os.path.exists(x509_path):
139 ssl_cert_file = x509_path
142 x509_path = os.getenv(
"HOME") +
"/.globus/userkey.pem" 143 if os.path.exists(x509_path):
144 ssl_key_file = x509_path
146 if not ssl_cert_file:
147 x509_path = os.getenv(
"HOME") +
"/.globus/usercert.pem" 148 if os.path.exists(x509_path):
149 ssl_cert_file = x509_path
151 if not ssl_key_file
or not os.path.exists(ssl_key_file):
152 sys.stderr.write(
"no certificate private key file found, please specify one via $X509_USER_PROXY, $X509_USER_KEY or --ssl-key-file\n")
155 if not ssl_cert_file
or not os.path.exists(ssl_cert_file):
156 sys.stderr.write(
"no certificate public key file found, please specify one via $X509_USER_CERT or --ssl-cert-file\n")
159 print(
"Using SSL private key", ssl_key_file)
160 print(
"Using SSL public key", ssl_cert_file)
164 regWhitespace = re.compile(
'.*\s.*')
165 if regWhitespace.match(fileName):
166 sys.stderr.write(
"whitespace detected!\n")
169 regRelval=re.compile(
'.*relval.*')
170 regCMSSW=re.compile(
'.*CMSSW_[0-9]+_[0-9]+_[0-9]+_.*')
171 regCMSSWpre=re.compile(
'.*CMSSW_[0-9]+_[0-9]+_[0-9]+_pre[0-9]+_.*')
172 if regRelval.match(fileName):
174 if not regCMSSW.match(fileName):
187 {
'size': os.stat(filename).st_size,
188 'checksum':
"md5:%s" % md5.new(
file(filename).
read()).hexdigest() },
189 {
'file': filename })
190 print(
'Status code: ', headers.get(
"Dqm-Status-Code",
"None"))
191 print(
'Message: ', headers.get(
"Dqm-Status-Message",
"None"))
192 print(
'Detail: ', headers.get(
"Dqm-Status-Detail",
"None"))
194 except urllib2.HTTPError
as e:
196 print(
'Status code: ', e.hdrs.get(
"Dqm-Status-Code",
"None"))
197 print(
'Message: ', e.hdrs.get(
"Dqm-Status-Message",
"None"))
198 print(
'Detail: ', e.hdrs.get(
"Dqm-Status-Detail",
"None"))
202 filename = filename.split(
"/")[-1]
203 regMC=re.compile(
'.*_R([0-9]*)__*')
204 if regMC.match(filename):
205 m = re.search(
'.*_R([0-9]*)(__.*).root',filename)
207 dataset = m.group(2).
replace(
"__",
"/")
209 m = re.search(
'.*_R([0-9]*)(_?.*).root',filename)
211 dataset = m.group(2).
replace(
"__",
"/")
213 dataset=
"/Global/Online/ALL" 217 return destination+
"start?workspace=summary;dataset="+dataset+
";sampletype=offline_data" 219 return destination+
"start?workspace=summary;runnr="+runNr+
";dataset="+dataset+
";sampletype=online_data" 222 filename = filename.split(
"/")[-1]
223 regMC=re.compile(
'.*_R([0-9]*)__*')
224 if regMC.match(filename):
225 m = re.search(
'.*_R([0-9]*)(__.*).root',filename)
227 dataset = m.group(2).
replace(
"__",
"/")
229 m = re.search(
'.*_R([0-9]*)(_?.*).root',filename)
231 dataset = m.group(2).
replace(
"__",
"/")
233 dataset=
"/Global/Online/ALL" 234 tempurl =
"https://www-ekp.physik.uni-karlsruhe.de/~zeise/cgi-bin/register.py?run="+runNr+
"&dataset="+dataset+
"&filename="+filename+
"&tags="+tags+
"&instance="+destination
235 print(
"Link that is used to register: ", tempurl)
236 urllib.urlopen(tempurl)
240 parser = optparse.OptionParser(add_help_option=
False)
241 parser.add_option(
"-h",
"--help", action=
"callback", callback=print_help),
242 parser.add_option(
"",
"--no-filename-check", dest=
"no_filename_check", default=
False, action=
"store_true")
243 parser.add_option(
"-d",
"--destination", dest=
"destination", default=
"", action=
"store")
244 parser.add_option(
"-t",
"--tags", dest=
"tags", default=
"", action=
"store")
245 parser.add_option(
"-s",
"--no-submission", dest=
"submission", default=
True, action=
"store_false")
246 parser.add_option(
"-r",
"--no-registration", dest=
"registration", default=
True, action=
"store_false")
247 parser.add_option(
"",
"--ssl-key-file", dest=
"ssl_key_file", default=
"", action=
"store")
248 parser.add_option(
"",
"--ssl-cert-file", dest=
"ssl_cert_file", default=
"", action=
"store")
249 (opts, args) = parser.parse_args()
252 if not opts.destination:
253 sys.stderr.write(
"no destination specified\n")
258 sys.stderr.write(
"no input files specified\n")
260 for fileName
in args:
261 fileName=fileName.strip()
262 if not os.path.exists(fileName):
263 sys.stderr.write(
"file '%s' doesn't exist!\n" % fileName)
265 if not opts.no_filename_check
and not checkFileName(fileName):
267 sys.stderr.write(
"file '%s' passed name check, upload will follow!\n" % fileName)
271 sys.stdout.write(
"file '%s' would be uploaded to '%s'\n" % (fileName, opts.destination))
272 if opts.registration:
274 print(
"You should see the plots here: "+
getURL(fileName, opts.destination))
276 if __name__ ==
'__main__':
277 sys.exit(
main(sys.argv[1:]))
def upload(url, args, files)
def checkFileName(fileName)
def getURL(filename, destination)
def replace(string, replacements)
def registerFileAtLogServer(filename, destination, tags)
def marshall(args, files, request)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
def __init__(self, host, args, kwargs)
def default_open(self, req)
def startUpload(url, filename)