Definition at line 1268 of file cfg-viewer.py.
def cfg-viewer.server.__init__ |
( |
|
self, |
|
|
|
name |
|
) |
| |
def cfg-viewer.server.printServer |
( |
|
self, |
|
|
|
name |
|
) |
| |
Definition at line 1272 of file cfg-viewer.py.
Referenced by cfg-viewer.server.__init__().
1273 with open(name,
'w')
as f:
1275 #!/usr/bin/env python\nimport SimpleHTTPServer\nimport SocketServer 1276 import shutil\nimport os\nimport cgi\nimport socket\nimport errno\n 1277 #Right now cannot deal with SequenceTypes\nclass CfgConvert: 1278 def __init__(self, obj):\n self._pName = obj["processName"] 1279 self._pFileN = obj["processFileName"]\n obj.__delitem__("processName") 1280 obj.__delitem__("processFileName")\n self._obj = obj 1282 self.header=\"""import FWCore.ParameterSet.Config as cms 1283 process = cms.Process('%(n)s')\nfrom %(fileN)s import *\n%(changes)s\n\n\""" 1284 def _doConversion(self):\n return self.header%( 1285 {"changes":self._doWork(self._obj), 1286 "n":self._pName,"fileN":self._pFileN})\n\n def _doWork(self, obj): 1287 result =""\n for key, value in obj.iteritems(): 1288 _type = value["Type"]\n _file = value["File"] 1289 _params = value["Parameters"]\n _oType = value["oType"] 1290 if(_oType):\n _oType= "'%s',"%(value["oType"]) 1291 #f = eval(func%(_type))\n if(type(_params)== list): 1292 if(len(_params)==0): 1293 result +="process.%s= cms.%s(%s)\\n"%(key,_type,_oType[:-1]) 1294 else:\n params = self._convert(_params) 1295 result+="process.%s= cms.%s(%s%s)\\n"%(key,_type,_oType, params) 1296 #elif(type(_params)== dict): 1297 # return "Sorry path and endpaths can not currently be translated." 1298 #params = self._doWork(_params)\n 1299 #obj = f("actualType", *parameters)\n return result\n 1300 def _convert(self,params): 1301 # NOTE: params do not take names inside functions 1302 # It is: name = cms.type(value)\n # okay we have a list like 1303 #[name,params, untracked,type]||[name,params,type] 1304 # 2 format options in the params\n # 1. [["name", "value", "type"]] 1305 # 2. [["PsetName",[[as above or this]], "Pset"]]\n 1306 # At position 0 is name. list[0]==name or list[0]==list 1307 # then that will be all 1308 # At position 1 is value or list. list[1]==value||list. 1309 # At position 2 is type. list[2] == type || untracked. 1310 # If there is a position 3 its type. list[3]== type. 1311 if(all(type(x) == list for x in params)): 1312 return ",".join([self._convert(x) for x in params]) 1313 length = len(params)\n if(length==1): 1314 if(type(params[0]) == list):\n # wont have params[1]etc 1315 # do inners\n return self._convert(params[0]) 1316 #wrong format\n print "Error 01 %s"%(str(params))\n return "" 1317 if(length !=3 and length !=4):\n #wrong format 1318 print "Error 02 len is %s"%(str(params))\n return "" 1319 # okay get on with it.\n name = params[0] 1320 if(type(params[1])==list):\n # do listy things 1321 value = self._convert(params[1])\n else:\n value=params[1] 1322 ty = params[2]\n if(name==ty):\n name=""\n if(length==4): 1323 ty+="."+params[3]\n if(name== params[3]):\n name="" 1324 if("vstring" in ty):\n # split the value up by commas 1325 value = ",".join(["'%s'"%x for x in value.split(",")]) 1326 elif("string" in ty):\n #surround string with quotation marks 1327 value = "%s"%(value)\n # okay now done with everything 1328 call= self._func%(ty)\n if(name): 1329 return"%s=%s(%s)"%(name,call,value)\n return"%s(%s)"%(call,value)\n 1330 class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):\n 1331 def do_GET(self):\n if(self.path == "/index.html"): 1332 # find out what cfg html folders we have below 1333 # and add each main html page to the index\n li = os.listdir(".") 1335 dirs = [x for x in li if os.path.isdir(x) and x.endswith(dEnd)] 1337 names = [os.path.join(x,name) for x in dirs if name in os.listdir(x)] 1338 tmpte = '<li><a href ="%(n)s">%(s)s</a></li>' 1339 lis = [tmpte%{"n":x,"s":os.path.split(x)[0].replace(dEnd, "")} 1340 for x in names]\n with open("index.html", 'w') as f: 1341 f.write(\"""\n<!DOCTYPE html>\n<html>\n <head> 1342 <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> 1343 <title>cfg-browser</title>\n </head>\n <body> 1344 <h4>Configuration files:</h4>\n <ul>\n %s\n </ul>\n </body> 1345 </html>\n \"""%("".join(lis)))\n 1346 return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)\n 1348 ctype,pdict= cgi.parse_header(self.headers.getheader('content-type')) 1349 bdy = cgi.parse_multipart(self.rfile,pdict) 1350 ch= " ".join(bdy[bdy.keys()[0]])\n self.conversion(eval(ch)) 1351 self.writeFile()\n print "finished writing"\n 1352 def conversion(self, json):\n # Need to convert my json into config 1353 result = CfgConvert(json)._doConversion() 1354 with open("changed_cfg.py", 'w')as f:\n f.write(result)\n 1355 def writeFile(self):\n with open("changed_cfg.py", 'rb') as f: 1356 self.send_response(200) 1357 self.send_header("Content-Type", 'text/html') 1358 self.send_header("Content-Disposition", 1359 'attachment; filename="changed_cfg.py"') 1360 fs = os.fstat(f.fileno()) 1361 self.send_header("Content-Length", str(fs.st_size)) 1362 self.end_headers()\n shutil.copyfileobj(f, self.wfile)\n 1363 def main(port=8000, reattempts=5):\n if(port <1024): 1364 print \"""This port number may be refused permission. 1365 It is better to use a port number > 1023.\"""\n try: 1366 Handler = ServerHandler 1367 httpd = SocketServer.TCPServer(("", port), Handler) 1368 print "using port", port 1369 print "Open http://localhost:%s/index.html in your browser."%(port) 1370 httpd.serve_forever()\n except socket.error as e: 1371 if(reattempts >0 and e[0] == errno.EACCES): 1372 print "Permission was denied." 1373 elif(reattempts >0 and e[0]== errno.EADDRINUSE): 1374 print "Address %s is in use."%(port)\n else:\n raise 1375 print "Trying again with a new port number."\n if(port < 1024): 1376 port = 1024\n else:\n port = port +1 1377 main(port, reattempts-1)\n\nif __name__ == "__main__":\n import sys 1378 if(len(sys.argv)>1):\n try:\n port = int(sys.argv[1]) 1379 main(port)\n sys.exit() \n except ValueError: 1380 print "Integer not valid, using default port number."\n main() def printServer(self, name)