9 ccRE = re.compile (
r'(\w+)\.cc')
12 """Extracts necessary piece of the buildfile. Returns empty
13 string if not found."""
15 build = open (buildfile,
'r')
17 raise RuntimeError(
"Could not open BuildFile '%s' for reading. Aboring." \
20 startBinRE = re.compile (
r'<\s*bin\s+name=(\S+)')
21 endBinRE = re.compile (
r'<\s*/bin>')
22 exeNameRE = re.compile (
r'(\w+)\.exe')
23 ccName = os.path.basename (copy)
24 match = ccRE.match (ccName)
26 ccName = match.group (1)
34 if foundBin
and endBinRE.search (line):
38 match = startBinRE.search (line)
41 exeName = match.group (1)
42 exeMatch = exeNameRE.search (exeName)
44 exeName = exeMatch.group (1)
47 line = re.sub (exeName, target, line)
54 """Creates a BuildFile if one doesn't already exist"""
55 if os.path.exists (buildfile):
57 build = open (buildfile,
'w')
58 build.write (
"<!-- -*- XML -*- -->\n\n<environment>\n\n</environment>\n")
63 """Adds needed piece for new executable. Returns true upon
65 backup = targetBuild +
".bak"
66 shutil.copyfile (targetBuild, backup)
67 oldBuild = open (backup,
'r')
68 newBuild = open (targetBuild, 'w')
69 environRE = re.compile (
r'<\s*environment')
73 if environRE.search (line):
74 newBuild.write (
'\n\n')
75 newBuild.write (buildPiece)
89 if __name__ ==
'__main__':
91 base = os.environ.get (
'CMSSW_BASE')
92 release_base = os.environ.get (
'CMSSW_RELEASE_BASE')
93 if not base
or not release_base:
94 print "Error: You must have already setup a CMSSW release. Aborting."
97 parser = optparse.OptionParser(
'usage: %prog [options] '
98 'Package/SubPackage/name\n'
99 'Creates new analysis code')
100 parser.add_option (
'--copy', dest=
'copy', type=
'string', default =
'blank',
101 help=
'Copies example. COPY should either be a file'
102 ' _or_ an example in PhysicsTools/FWLite/examples')
103 parser.add_option (
'--newPackage', dest=
'newPackage', action=
'store_true',
104 help=
'Will create Package/Subpackage folders if necessary')
105 parser.add_option (
'--toTest', dest=
'toTest', action=
'store_true',
106 help=
'Will create files in test/ instead of bin/')
107 (options, args) = 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 accompying " % name, \
133 "Buildfile '%s'. Aborting" % build
139 print "Error: Did not find '%s' to copy. Aborting." % copy
145 if len (pieces) != 3:
146 print "Error: Need to provide 'Package/SubPackage/name'"
149 match = ccRE.match (target)
151 target = match.group (1)
152 buildPiece = extractBuildFilePiece (fullBuild, copy, target)
154 print "Error: Could not extract necessary info from Buildfile. Aborting."
157 firstDir = base +
'/src/' + pieces[0]
158 secondDir = firstDir +
'/' + pieces[1]
163 if not os.path.exists (secondDir)
and \
164 not options.newPackage:
165 raise RuntimeError(
"%s does not exist. Use '--newPackage' to create" \
166 % (
"/".join (pieces[0:2]) ))
167 dirList = [firstDir, secondDir, secondDir + bin]
168 targetCC = dirList[2] +
'/' + target +
'.cc'
169 targetBuild = dirList[2] +
'/BuildFile'
170 if os.path.exists (targetCC):
171 print 'Error: "%s" already exists. Aborting.' % targetCC
174 for currDir
in dirList:
175 if not os.path.exists (currDir):
178 shutil.copyfile (fullName, targetCC)
179 print "Copied:\n %s\nto:\n %s.\n" % (fullName, targetCC)
180 createBuildFile (targetBuild)
181 if extractBuildFilePiece (targetBuild, target):
182 print "Buildfile already has '%s'. Skipping" % target
185 if addBuildPiece (targetBuild, buildPiece):
186 print "Added info to:\n %s." % targetBuild
188 print "Unable to modify Buildfile. Sorry."
def extractBuildFilePiece