3 from __future__
import print_function
4 from argparse
import ArgumentParser, ArgumentDefaultsHelpFormatter
10 ccRE = re.compile (
r'(\w+)\.cc')
13 """Extracts necessary piece of the buildfile. Returns empty 14 string if not found.""" 16 build = open (buildfile,
'r') 18 raise RuntimeError(
"Could not open BuildFile '%s' for reading. Aboring." \
21 startBinRE = re.compile (
r'<\s*bin\s+name=(\S+)')
22 endBinRE = re.compile (
r'<\s*/bin>')
23 exeNameRE = re.compile (
r'(\w+)\.exe')
24 ccName = os.path.basename (copy)
25 match = ccRE.match (ccName)
27 ccName = match.group (1)
35 if foundBin
and endBinRE.search (line):
39 match = startBinRE.search (line)
42 exeName = match.group (1)
43 exeMatch = exeNameRE.search (exeName)
45 exeName = exeMatch.group (1)
48 line = re.sub (exeName, target, line)
55 """Creates a BuildFile if one doesn't already exist""" 56 if os.path.exists (buildfile):
58 build = open (buildfile,
'w')
59 build.write (
"<!-- -*- XML -*- -->\n\n<environment>\n\n</environment>\n")
64 """Adds needed piece for new executable. Returns true upon 66 backup = targetBuild +
".bak" 67 shutil.copyfile (targetBuild, backup)
68 oldBuild = open (backup,
'r') 69 newBuild = open (targetBuild, 'w')
70 environRE = re.compile (
r'<\s*environment')
74 if environRE.search (line):
75 newBuild.write (
'\n\n')
76 newBuild.write (buildPiece)
90 if __name__ ==
'__main__':
92 base = os.environ.get (
'CMSSW_BASE')
93 release_base = os.environ.get (
'CMSSW_RELEASE_BASE')
94 if not base
or not release_base:
95 print(
"Error: You must have already setup a CMSSW release. Aborting.")
98 parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter, description=
'Creates new analysis code')
99 parser.add_argument(
'--copy', dest=
'copy', type=str, default =
'blank',
100 help=
'Copies example. COPY should either be a file' 101 ' _or_ an example in PhysicsTools/FWLite/examples')
102 parser.add_argument(
'--newPackage', dest=
'newPackage', action=
'store_true',
103 help=
'Will create Package/Subpackage folders if necessary')
104 parser.add_argument(
'--toTest', dest=
'toTest', action=
'store_true',
105 help=
'Will create files in test/ instead of bin/')
106 parser.add_argument(
"name", metavar=
"Package/SubPackage/name", type=str)
107 options = parser.parse_args()
110 if not re.search (
'\.cc$', copy):
112 buildCopy = os.path.dirname (copy)
114 buildCopy +=
'/BuildFile' 116 buildCopy =
'BuildFile' 119 base +
"/src/PhysicsTools/FWLite/examples/",
120 release_base+
"/src/PhysicsTools/FWLite/examples/"]
123 for where
in searchList:
125 build = where + buildCopy
127 if os.path.exists (name):
131 if not os.path.exists (build):
132 print(
"Error: Found '%s', but no accompanying " % name, \
133 "Buildfile '%s'. Aborting" % build)
139 print(
"Error: Did not find '%s' to copy. Aborting." % copy)
141 pieces = options.name.split(
'/')
142 if len (pieces) != 3:
143 print(
"Error: Need to provide 'Package/SubPackage/name'")
146 match = ccRE.match (target)
148 target = match.group (1)
149 buildPiece = extractBuildFilePiece (fullBuild, copy, target)
151 print(
"Error: Could not extract necessary info from Buildfile. Aborting.")
154 firstDir = base +
'/src/' + pieces[0]
155 secondDir = firstDir +
'/' + pieces[1]
160 if not os.path.exists (secondDir)
and \
161 not options.newPackage:
162 raise RuntimeError(
"%s does not exist. Use '--newPackage' to create" \
163 % (
"/".join (pieces[0:2]) ))
164 dirList = [firstDir, secondDir, secondDir + bin]
165 targetCC = dirList[2] +
'/' + target +
'.cc' 166 targetBuild = dirList[2] +
'/BuildFile' 167 if os.path.exists (targetCC):
168 print(
'Error: "%s" already exists. Aborting.' % targetCC)
171 for currDir
in dirList:
172 if not os.path.exists (currDir):
175 shutil.copyfile (fullName, targetCC)
176 print(
"Copied:\n %s\nto:\n %s.\n" % (fullName, targetCC))
177 createBuildFile (targetBuild)
178 if extractBuildFilePiece (targetBuild, target):
179 print(
"Buildfile already has '%s'. Skipping" % target)
182 if addBuildPiece (targetBuild, buildPiece):
183 print(
"Added info to:\n %s." % targetBuild)
185 print(
"Unable to modify Buildfile. Sorry.")
def addBuildPiece(targetBuild, buildPiece)
def createBuildFile(buildfile)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
def extractBuildFilePiece(buildfile, copy, target='dummy')