CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
newFWLiteAna.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 import optparse
4 import os
5 import sys
6 import re
7 import shutil
8 
9 ccRE = re.compile (r'(\w+)\.cc')
10 
11 def extractBuildFilePiece (buildfile, copy, target = 'dummy'):
12  """Extracts necessary piece of the buildfile. Returns empty
13  string if not found."""
14  try:
15  build = open (buildfile, 'r')
16  except:
17  raise RuntimeError("Could not open BuildFile '%s' for reading. Aboring." \
18  % buildfile)
19  # make my regex
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)
25  if match:
26  ccName = match.group (1)
27  retval = ''
28  foundBin = False
29  for line in build:
30  # Are we in the middle of copying what we need?
31  if foundBin:
32  retval += line
33  # Are we copying what we need and reach the end?
34  if foundBin and endBinRE.search (line):
35  # all done
36  break
37  # Is this the start of a bin line with the right name?
38  match = startBinRE.search (line)
39  if match:
40  # strip of .exe if it's there
41  exeName = match.group (1)
42  exeMatch = exeNameRE.search (exeName)
43  if exeMatch:
44  exeName = exeMatch.group (1)
45  if exeName == ccName:
46  foundBin = True
47  line = re.sub (exeName, target, line)
48  retval = line
49  build.close()
50  return retval
51 
52 
53 def createBuildFile (buildfile):
54  """Creates a BuildFile if one doesn't already exist"""
55  if os.path.exists (buildfile):
56  return
57  build = open (buildfile, 'w')
58  build.write ("<!-- -*- XML -*- -->\n\n<environment>\n\n</environment>\n")
59  build.close()
60 
61 
62 def addBuildPiece (targetBuild, buildPiece):
63  """Adds needed piece for new executable. Returns true upon
64  success."""
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')
70  success = False
71  for line in oldBuild:
72  newBuild.write (line)
73  if environRE.search (line):
74  newBuild.write ('\n\n')
75  newBuild.write (buildPiece)
76  success = True
77  oldBuild.close()
78  newBuild.close()
79  return success
80 
81 
82 
83 ########################
84 ## ################## ##
85 ## ## Main Program ## ##
86 ## ################## ##
87 ########################
88 
89 if __name__ == '__main__':
90  # We need CMSSW to already be setup
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."
95  sys.exit()
96  # setup the options
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()
108  # get the name of the copy file and make sure we can find everything
109  copy = options.copy
110  if not re.search ('\.cc$', copy):
111  copy += '.cc'
112  buildCopy = os.path.dirname (copy)
113  if len (buildCopy):
114  buildCopy += '/BuildFile'
115  else:
116  buildCopy = 'BuildFile'
117  found = False
118  searchList = ['',
119  base + "/src/PhysicsTools/FWLite/examples/",
120  release_base+ "/src/PhysicsTools/FWLite/examples/"]
121  fullName = ''
122  fullBuild = ''
123  for where in searchList:
124  name = where + copy
125  build = where + buildCopy
126  # Is the copy file here
127  if os.path.exists (name):
128  # Yes.
129  found = True
130  # Is there a Buildfile too?
131  if not os.path.exists (build):
132  print "Error: Found '%s', but no accompying " % name, \
133  "Buildfile '%s'. Aborting" % build
134  sys.exit()
135  fullName = name
136  fullBuild = build
137  break
138  if not found:
139  print "Error: Did not find '%s' to copy. Aborting." % copy
140  sys.exit()
141  if len (args) != 1:
142  parser.print_usage()
143  sys.exit()
144  pieces = args[0].split('/')
145  if len (pieces) != 3:
146  print "Error: Need to provide 'Package/SubPackage/name'"
147  sys.exit()
148  target = pieces[2]
149  match = ccRE.match (target)
150  if match:
151  target = match.group (1)
152  buildPiece = extractBuildFilePiece (fullBuild, copy, target)
153  if not buildPiece:
154  print "Error: Could not extract necessary info from Buildfile. Aborting."
155  sys.exit()
156  # print buildPiece
157  firstDir = base + '/src/' + pieces[0]
158  secondDir = firstDir + '/' + pieces[1]
159  if options.toTest:
160  bin = '/test'
161  else:
162  bin = '/bin'
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
172  sys.exit()
173  # Start making directory structure
174  for currDir in dirList:
175  if not os.path.exists (currDir):
176  os.mkdir (currDir)
177  # copy the file over
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
183  else :
184  # we don't already have a piece here
185  if addBuildPiece (targetBuild, buildPiece):
186  print "Added info to:\n %s." % targetBuild
187  else:
188  print "Unable to modify Buildfile. Sorry."
def createBuildFile
Definition: newFWLiteAna.py:53
def extractBuildFilePiece
Definition: newFWLiteAna.py:11
double split
Definition: MVATrainer.cc:139