15 from FWCore.ParameterSet
import cfgName2py
22 """take a line and a corresponding comment and prepare the replaceDict such that it can be found again in the python version """
23 allKeywords = [
'module',
'int32',
'vint32',
'uint32',
'vuint32',
'double',
'vdouble',
'InputTag',
'VInputTag',
'PSet',
'VPSet',
'string',
'vstring',
'bool',
'vbool',
'path',
'sequence',
'schedule',
'endpath',
'es_source',
'es_module',
'block',
'FileInPath']
24 unnamedKeywords = [
'es_source',
'es_module']
27 words = line.lstrip().
split()
31 if firstWord
in allKeywords
and len(words) > 2
and words[2] ==
'=':
32 tokenInPython = words[1] +
" = "
33 replaceDict[tokenInPython] = comment
34 elif firstWord ==
'untracked' and len(words) > 3
and words[1]
in allKeywords
and words[3] ==
'=':
35 tokenInPython = words[2] +
" = cms.untracked"
36 replaceDict[tokenInPython] = comment
37 elif firstWord.startswith(
'include'):
39 pythonModule = pythonModule.replace(
"/",
".").
replace(
"python.",
"").
replace(
".py",
"")
40 tokenInPython =
"from "+pythonModule
41 tokenInPython = tokenInPython.replace(
"/",
".").
replace(
"python.",
"").
replace(
".py",
"")
42 replaceDict[tokenInPython] = comment
44 tokenInPython =
"process.load(\""+pythonModule
45 replaceDict[tokenInPython] = comment
46 elif firstWord ==
'source' and len(words) > 1
and words[1] ==
'=':
47 replaceDict[
'source = '] = comment
48 elif firstWord
in unnamedKeywords
and len(words) > 2
and words[1] ==
'=':
49 tokenInPython = words[2] +
' = cms.ES'
50 replaceDict[tokenInPython] = comment
51 elif firstWord ==
'replace' and len(words) > 2
and words[2] ==
'=':
52 tokenInPython= words[1] +
" = "
53 replaceDict[tokenInPython] = comment
54 elif firstWord ==
'replace' and len(words) > 2
and words[2] ==
'=':
55 tokenInPython= words[1] +
" = "
56 replaceDict[tokenInPython] = comment
58 tokenInPython =
'process.'+tokenInPython
59 replaceDict[tokenInPython] = comment
60 elif firstWord ==
'using' and len(words) == 2:
61 tokenInPython= words[1]
62 replaceDict[tokenInPython] = comment
65 replaceDict[
"@beginning"] +=
"\n"+comment.value
73 replaceDict[
"@beginning"] =
"# The following comments couldn't be translated into the new config version:\n"
74 allKeywords = [
'module',
'int32',
'vint32',
'uint32',
'vuint32',
'double',
'vdouble',
'InputTag',
'VInputTag',
'PSet',
'VPSet',
'string',
'bool',
'vbool',
'path',
'sequence',
'schedule',
'endpath',
'es_source',
'es_module',
'block',
'FileInPath']
75 unnamedKeywords = [
'es_source',
'es_module']
78 inSlashStarComment =
False
82 for line
in configString.splitlines():
83 if line.lstrip().startswith(
"/*"):
85 comment.type =
"slashstar"
86 splitStarSlash = line.lstrip().lstrip(
"/*").
split(
"*/")
87 comment.value =
"# "+splitStarSlash[0]+
"\n"
89 inSlashStarComment = (line.find(
'*/') == -1)
91 elif inSlashStarComment:
92 splitStarSlash = line.lstrip().lstrip(
"/*").
split(
"*/")
93 comment.value +=
"# "+splitStarSlash[0]+
"\n"
94 inSlashStarComment = (line.find(
'*/') == -1)
95 elif line.lstrip().startswith(
"#")
or line.lstrip().startswith(
"//"):
97 comment.value +=
"#"+line.lstrip().lstrip(
"//").lstrip(
"#") +
"\n"
100 comment.type =
"trailing"
101 comment.value =
"#"+line.lstrip().lstrip(
"//").lstrip(
"#") +
"\n"
104 words = line.lstrip().
split()
112 if len(line.split(
"#")) > 1
or len(line.split(
"//")) > 1:
114 if len(line.split(
"#")) > 1:
115 comment.value =
'#'+line.split(
"#")[1]
117 comment.value =
'#'+line.split(
"//")[1]
118 comment.type =
"inline"
131 if replaceDict[
"@beginning"] !=
"# The following comments couldn't be translated into the new config version:\n":
132 configString = replaceDict[
"@beginning"]+
"\n"+configString
134 replaceDict.pop(
"@beginning")
137 actualReplacements = {}
138 for keyword, comment
in replaceDict.iteritems():
139 for line
in configString.splitlines():
140 if line.lstrip().startswith(keyword):
141 indentation = line[0:line.find(keyword)]
142 if len([1
for l
in configString.splitlines()
if l.lstrip().startswith(keyword)]) !=1:
143 print "WARNING. Following keyword not unique:", keyword
145 if comment.type ==
"inline":
146 newLine = line +
" #"+comment.value+
"\n"
148 newLine = line.replace(line,comment.value+line)
149 newLine = newLine.replace(
'#', indentation+
'#')
150 actualReplacements[line] = newLine
154 for original, new
in actualReplacements.iteritems():
155 configString = configString.replace(original, new)
161 cfgFile =
file(cfgFileName)
162 cfgString = cfgFile.read()
167 pyFile =
file(pyFileName)
168 pyString = pyFile.read()
170 print pyFileName,
"does not exist"
173 print "Opening", pyFileName
177 pyOutFile =
file(pyFileName,
"w")
178 pyOutFile.write(newPyString)
179 print "Wrote", pyFileName
193 print "Please give either a filename, or 'local'"
194 elif argv[1] ==
'local':
195 for subsystem
in os.listdir(
"."):
198 for package
in os.listdir(
"./"+subsystem):
201 for name
in os.listdir(subsystem+
"/"+package+
"/data"):
202 if len(name.split(
"."))==2
and name.split(
".")[1]
in [
"cfg",
"cfi",
"cff"]:
203 print subsystem+
"/"+package+
"/data/"+name
204 loopfile(subsystem+
"/"+package+
"/data/"+name)