3 from __future__
import print_function
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 = optparse.OptionParser(
'usage: %prog [options] '
99 'Package/SubPackage/name\n'
100 'Creates new analysis code')
101 parser.add_option (
'--copy', dest=
'copy', type=
'string', default =
'blank',
102 help=
'Copies example. COPY should either be a file'
103 ' _or_ an example in PhysicsTools/FWLite/examples')
104 parser.add_option (
'--newPackage', dest=
'newPackage', action=
'store_true',
105 help=
'Will create Package/Subpackage folders if necessary')
106 parser.add_option (
'--toTest', dest=
'toTest', action=
'store_true',
107 help=
'Will create files in test/ instead of bin/')
108 (options, args) = parser.parse_args()
111 if not re.search (
'\.cc$', copy):
113 buildCopy = os.path.dirname (copy)
115 buildCopy +=
'/BuildFile'
117 buildCopy =
'BuildFile'
120 base +
"/src/PhysicsTools/FWLite/examples/",
121 release_base+
"/src/PhysicsTools/FWLite/examples/"]
124 for where
in searchList:
126 build = where + buildCopy
128 if os.path.exists (name):
132 if not os.path.exists (build):
133 print(
"Error: Found '%s', but no accompying " % name, \
134 "Buildfile '%s'. Aborting" % build)
140 print(
"Error: Did not find '%s' to copy. Aborting." % copy)
146 if len (pieces) != 3:
147 print(
"Error: Need to provide 'Package/SubPackage/name'")
150 match = ccRE.match (target)
152 target = match.group (1)
153 buildPiece = extractBuildFilePiece (fullBuild, copy, target)
155 print(
"Error: Could not extract necessary info from Buildfile. Aborting.")
158 firstDir = base +
'/src/' + pieces[0]
159 secondDir = firstDir +
'/' + pieces[1]
164 if not os.path.exists (secondDir)
and \
165 not options.newPackage:
166 raise RuntimeError(
"%s does not exist. Use '--newPackage' to create" \
167 % (
"/".join (pieces[0:2]) ))
168 dirList = [firstDir, secondDir, secondDir + bin]
169 targetCC = dirList[2] +
'/' + target +
'.cc'
170 targetBuild = dirList[2] +
'/BuildFile'
171 if os.path.exists (targetCC):
172 print(
'Error: "%s" already exists. Aborting.' % targetCC)
175 for currDir
in dirList:
176 if not os.path.exists (currDir):
179 shutil.copyfile (fullName, targetCC)
180 print(
"Copied:\n %s\nto:\n %s.\n" % (fullName, targetCC))
181 createBuildFile (targetBuild)
182 if extractBuildFilePiece (targetBuild, target):
183 print(
"Buildfile already has '%s'. Skipping" % target)
186 if addBuildPiece (targetBuild, buildPiece):
187 print(
"Added info to:\n %s." % targetBuild)
189 print(
"Unable to modify Buildfile. Sorry.")