CMS 3D CMS Logo

List of all members | Public Member Functions
cfg-viewer.server Class Reference

Public Member Functions

def __init__ (self, name)
 
def printServer (self, name)
 

Detailed Description

Definition at line 1269 of file cfg-viewer.py.

Constructor & Destructor Documentation

def cfg-viewer.server.__init__ (   self,
  name 
)

Definition at line 1270 of file cfg-viewer.py.

References cfg-viewer.server.printServer().

1270  def __init__(self, name):
1271  self.printServer(name)
1272 
def __init__(self, name)
Definition: cfg-viewer.py:1270
def printServer(self, name)
Definition: cfg-viewer.py:1273

Member Function Documentation

def cfg-viewer.server.printServer (   self,
  name 
)

Definition at line 1273 of file cfg-viewer.py.

Referenced by cfg-viewer.server.__init__().

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