ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/PRODREQUEST/injectCFGFileCgi.py
Revision: 1.10
Committed: Tue May 29 12:31:52 2007 UTC (17 years, 11 months ago) by eulisse
Content type: text/x-python
Branch: MAIN
CVS Tags: V00-08-00, V00-07-09, V00-07-08, V00-07-07, V00-07-06, V00-07-05, V00-07-04, V00-07-03, V00-07-02, V00-07-01, V00-07-00, gePreProdManagerSupport, V00-06-08, V00-06-07, V00-06-06, V00-06-05, HEAD
Changes since 1.9: +2 -66 lines
Log Message:
* injection does not require creation of parsed parameter set anymore.
* missing comma in method declaration.
* debug messages are now off if --debug is not passed.
* default values used if missing arguments.
* Code cleanup for ProdRequest.createPayloadWithEntry.
* default for input_dataset and output_dataset is now None.

File Contents

# Content
1 #!/usr/bin/env python
2 # TODO: obsolete this file as soon as CVS browsing works.
3
4 from getopt import getopt
5 from sys import argv
6 from os.path import exists
7 from optparse import OptionParser
8 from os import getenv
9
10 import popen2
11 import sys
12 import os
13
14
15 # Code stolen from DBS/PythonAPI for POSTing in python.
16 # This way there is not need for depending on ProdRequest directly.
17 # Kudos to Lassi!
18 from cStringIO import StringIO
19 import urllib2
20 import gzip
21
22
23 def _encode(args):
24 """
25 Encode form (name, value) elements into multi-part/form-data.
26 We don't actually need to know what we are uploading here, so
27 just claim it's all text/plain.
28 """
29 boundary = '----------=_PRODREQUEST_CGI_BOUNDARY_=-----------'
30 (body, crlf) = ('', '\r\n')
31 for key, value in args.items():
32 body += '--' + boundary + crlf
33 body += ('Content-disposition: form-data; name="%s"' % key) + crlf
34 body += crlf + value + crlf
35 body += '--' + boundary + '--' + crlf + crlf
36 return ('multipart/form-data; boundary=' + boundary, body)
37
38 def _marshall(args, request):
39 """
40 Marshalls the arguments to the CGI script as multi-part/form-data,
41 not the default application/x-www-form-url-encoded. This improves
42 the transfer of the large inputs and eases command line invocation
43 of the CGI script.
44 """
45 (type, body) = _encode(args)
46 request.add_header ('Content-type', type)
47 request.add_header ('Content-length', str(len(body)))
48 request.add_data (body)
49
50 def _call (url, args):
51 """
52 Make a call to the CGI server, either a remote HTTP request (the
53 URL is of the form http:*), or invoke the CGI as a local executable
54 (the URL is of the form file:*).
55
56 For the HTTP case, we build a request object, add the form data
57 to as multipart/form-data, then fetch the result. The output is
58 compressed so we decompress it, parse out the DBS special HTTP
59 headers for status code and error information, and raise errors
60 as exceptions. If all went well, we return the output to caller.
61
62 The local execution is similar except we pass call details via
63 environment variables and form data on standard input to the CGI
64 script. The output isn't compressed. (FIXME: Not implemented!)
65 """
66 try:
67 #request = urllib2.Request (self._cgiUrl, urllib.urlencode(args))
68 request = urllib2.Request (url)
69 request.add_header ('Accept-encoding', 'gzip')
70 _marshall (args, request)
71 result = urllib2.build_opener ().open (request)
72 data = result.read()
73 if result.headers.get ('Content-encoding', '') == 'gzip':
74 data = gzip.GzipFile (fileobj=StringIO(data)).read ()
75 return data
76 except:
77 pass
78
79 if __name__ == "__main__":
80 optionParser = OptionParser ()
81 optionParser.add_option ("-w", "--workflow-name", dest="workflowName",
82 metavar="NAME", default="",
83 help="destination workflow NAME")
84 optionParser.add_option ("-t", "--tag", dest="tag",
85 metavar="TAG", default="",
86 help="CMSSW TAG")
87 optionParser.add_option ("-s", "--server", dest="prodrequestUrl",
88 metavar="URL", default="cmsdoc.cern.ch/cms/test/aprom/DBS/prodrequest",
89 help="ProdRequest URL")
90
91 options, args = optionParser.parse_args ()
92
93 if not options.workflowName:
94 print "Please specify the workflow name."
95 exit
96
97 if not options.tag:
98 print "Please specify the tag to be used for this import"
99 exit
100
101 if not len (args):
102 print "Please specify at least one configuration file to be injected."
103 exit
104
105 for cfgfile in args:
106 if not exists (cfgfile):
107 print cfgfile + " not found."
108 pass
109
110 cfg = file (cfgfile).read ()
111 try:
112 cfgname = cfgfile.rsplit ("/", 1)[1] + " - " + options.tag
113 version = os.getenv ("CMSSW_VERSION")
114 print "CMSSW version: %s" % version
115 print "Contacting http://%s/ProdRequest/createPayloadWithEntry" % options.prodrequestUrl
116 result = _call ("http://%s/ProdRequest/createPayloadWithEntry" % options.prodrequestUrl,
117 {"name": cfgname,
118 "parameterset": cfg,
119 "primary_dataset": "primary_dataset",
120 "output_dataset": "output_dataset",
121 "workflowName": options.workflowName,
122 "version": version})
123 print result
124 except Exception, e:
125 print "Cannot convert: %s" % cfgfile
126 print "%s" % e
127