CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
cmsLHEtoEOSManager.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 print 'Starting cmsLHEtoEOSManager.py'
4 
5 __version__ = "$Revision: 1.13 $"
6 
7 import os
8 import subprocess
9 import time
10 import re
11 
12 defaultEOSRootPath = '/eos/cms/store/lhe'
13 defaultEOSLoadPath = 'root://eoscms/'
14 defaultEOSlistCommand = 'xrd eoscms dirlist '
15 defaultEOSmkdirCommand = 'xrd eoscms mkdir '
16 defaultEOSfeCommand = 'xrd eoscms existfile '
17 defaultEOScpCommand = 'xrdcp -np '
18 
19 def findXrdDir(theDirRecord):
20 
21  elements = theDirRecord.split(' ')
22  if len(elements) > 1:
23  return elements[-1].rstrip('\n').split('/')[-1]
24  else:
25  return None
26 
27 def articleExist(artId):
28 
29  itExists = False
30  theCommand = defaultEOSlistCommand+' '+defaultEOSRootPath
31  dirList = subprocess.Popen(["/bin/sh","-c",theCommand], stdout=subprocess.PIPE)
32  for line in dirList.stdout.readlines():
33  if findXrdDir(line) == str(artId):
34  itExists = True
35 
36  return itExists
37 
39 
40  artList = [0]
41 
42  theCommand = defaultEOSlistCommand+' '+defaultEOSRootPath
43  dirList = subprocess.Popen(["/bin/sh","-c",theCommand], stdout=subprocess.PIPE)
44  for line in dirList.stdout.readlines():
45  try:
46  if line.rstrip('\n') != '':
47  artList.append(int(findXrdDir(line)))
48  except:
49  break
50 
51  return max(artList)
52 
53 
54 def fileUpload(uploadPath,lheList, checkSumList, reallyDoIt):
55 
56  inUploadScript = ''
57  index = 0
58  for f in lheList:
59  realFileName = f.split('/')[-1]
60  # Check the file existence
61  newFileName = uploadPath+'/'+str(realFileName)
62  addFile = True
63  additionalOption = ''
64  theCommand = defaultEOSfeCommand+' '+newFileName
65  exeFullList = subprocess.Popen(["/bin/sh","-c",theCommand], stdout=subprocess.PIPE)
66  result = exeFullList.stdout.readlines()
67  if result[0].rstrip('\n') == 'The file exists.':
68  addFile = False
69  print 'File '+newFileName+' already exists: do you want to overwrite? [y/n]'
70  reply = raw_input()
71  if reply == 'y' or reply == 'Y':
72  addFile = True
73  additionalOption = ' -f '
74  print ''
75  print 'Overwriting file '+newFileName+'\n'
76  # add the file
77  if addFile:
78 # print 'Adding file '+str(f)+'\n'
79  inUploadScript = defaultEOScpCommand + additionalOption + ' ' + str(f) + ' ' + defaultEOSLoadPath+uploadPath + '/' + str(realFileName)
80  print 'Uploading file %s...' % str(f)
81  if reallyDoIt:
82  exeRealUpload = subprocess.Popen(["/bin/sh","-c",inUploadScript])
83  exeRealUpload.communicate()
84  eosCheckSumCommand = '/afs/cern.ch/project/eos/installation/0.3.15/bin/eos.select find --checksum ' + uploadPath + '/' + str(realFileName) + ' | awk \'{print $2}\' | cut -d= -f2'
85  exeEosCheckSum = subprocess.Popen(eosCheckSumCommand ,shell=True, stdout=subprocess.PIPE)
86  EosCheckSum = exeEosCheckSum.stdout.read()
87  assert exeEosCheckSum.wait() == 0
88  # print 'checksum: eos = ' + EosCheckSum + 'orig file = ' + checkSumList[index] + '\n'
89  if checkSumList[index] not in EosCheckSum:
90  print 'WARNING! The checksum for file ' + str(realFileName) + ' in EOS\n'
91  print EosCheckSum + '\n'
92  print 'does not match the checksum of the original one\n'
93  print checkSumList[index] + '\n'
94  print 'please try to re-upload file ' + str(realFileName) + ' to EOS.\n'
95  else:
96  print 'Checksum OK for file ' + str(realFileName)
97  index = index+1
98 
99 # launch the upload shell script
100 
101 # print '\n Launching upload script \n'+inUploadScript+'\n at '+time.asctime(time.localtime(time.time()))+' ...\n'
102 # if reallyDoIt:
103 # exeRealUpload = subprocess.Popen(["/bin/sh","-c",inUploadScript])
104 # exeRealUpload.communicate()
105  print '\n Upload ended at '+time.asctime(time.localtime(time.time()))
106 
107 #################################################################################################
108 
109 if __name__ == '__main__':
110 
111  import optparse
112 
113  # Here we define an option parser to handle commandline options..
114  usage='cmsLHEtoEOSManager.py <options>'
115  parser = optparse.OptionParser(usage)
116  parser.add_option('-f', '--file',
117  help='LHE local file list to be uploaded, separated by ","' ,
118  default='',
119  dest='fileList')
120 
121  parser.add_option('-F', '--files-from', metavar = 'FILE',
122  help='File containing the list of LHE local files be uploaded, one file per line')
123 
124  parser.add_option('-n', '--new',
125  help='Create a new article' ,
126  action='store_true',
127  default=False,
128  dest='newId')
129 
130  parser.add_option('-u', '--update',
131  help='Update the article <Id>' ,
132  default=0,
133  dest='artIdUp')
134 
135  parser.add_option('-l', '--list',
136  help='List the files in article <Id>' ,
137  default=0,
138  dest='artIdLi')
139 
140  parser.add_option('-d', '--dry-run',
141  help='dry run, it does nothing, but you can see what it would do',
142  action='store_true',
143  default=False,
144  dest='dryRun')
145 
146  parser.add_option('-c', '--compress',
147  help='compress the local .lhe file with xz before upload',
148  action='store_true',
149  default=False,
150  dest='compress')
151 
152  (options,args) = parser.parse_args()
153 
154  # print banner
155 
156  print ''
157  print 'cmsLHEtoEOSmanager '+__version__[1:-1]
158  print ''
159  print 'Running on ',time.asctime(time.localtime(time.time()))
160  print ''
161 
162  reallyDoIt = not options.dryRun
163 
164  # Now some fault control. If an error is found we raise an exception
165  if not options.newId and options.artIdUp==0 and options.artIdLi==0:
166  raise Exception('Please specify the action to be taken, either "-n", "-u" or "-l"!')
167 
168  if options.fileList == '' and not options.files_from and (options.newId or options.artIdUp!=0):
169  raise Exception('Please provide the input file list!')
170 
171  if (options.newId and (options.artIdUp != 0 or options.artIdLi != 0)) or (options.artIdUp != 0 and options.artIdLi != 0):
172  raise Exception('Options "-n", "-u" and "-l" are mutually exclusive, please choose only one!')
173 
174  if options.newId:
175  print 'Action: create new article\n'
176  elif options.artIdUp != 0:
177  print 'Action: update article '+str(options.artIdUp)+'\n'
178  elif options.artIdLi != 0:
179  print 'Action: list content of article '+str(options.artIdLi)+'\n'
180 
181  if options.artIdLi==0:
182  theList = []
183  if len(options.fileList) > 0:
184  theList=(options.fileList.split(','))
185 
186  if options.files_from:
187  try:
188  f = open(options.files_from)
189  except IOError:
190  raise Exception('Cannot open the file list, \'%s\'' % options.files_from)
191  for l in f:
192  l = l.strip()
193  if len(l) == 0 or l[0] == '#':
194  continue
195  theList.append(l)
196 
197  theCompressedFilesList = []
198  theCheckSumList = []
199  for f in theList:
200  # Check the file name extension
201  print f
202  if not ( f.lower().endswith(".lhe") or f.lower().endswith(".lhe.xz") ):
203  raise Exception('Input file name must have the "lhe" or "lhe.xz" final extension!')
204  if( f.lower().endswith(".lhe.xz") ):
205  print "Important! Input file "+f+" is already zipped: please make sure you verified its integrity with xmllint before zipping it. You can do it with:\n"
206  print "xmllint file.lhe\n"
207  print "Otherwise it is best to pass the unzipped file to this script and let it check its integrity and compress the file with the --compress option\n"
208  exeCheckSum = subprocess.Popen(["/afs/cern.ch/cms/caf/bin/cms_adler32",f], stdout=subprocess.PIPE)
209  getCheckSum = subprocess.Popen(["awk", "{print $1}"], stdin=exeCheckSum.stdout, stdout=subprocess.PIPE)
210  exeCheckSum.stdout.close()
211  output,err = getCheckSum.communicate()
212  # print 'orig file checksum = ' + output + '\n'
213  theCheckSumList.append(output)
214  # Check the local file existence
215  if not os.path.exists(f):
216  raise Exception('Input file '+f+' does not exists')
217  if( f.lower().endswith(".lhe") ):
218  theCheckIntegrityCommand = 'xmllint -noout '+f
219  exeCheckIntegrity = subprocess.Popen(["/bin/sh","-c", theCheckIntegrityCommand])
220  intCode = exeCheckIntegrity.wait()
221  exeCheckSum = subprocess.Popen(["/afs/cern.ch/cms/caf/bin/cms_adler32",f], stdout=subprocess.PIPE)
222  getCheckSum = subprocess.Popen(["awk", "{print $1}"], stdin=exeCheckSum.stdout, stdout=subprocess.PIPE)
223  exeCheckSum.stdout.close()
224  output,err = getCheckSum.communicate()
225  # print 'orig file checksum = ' + output + '\n'
226  theCheckSumList.append(output)
227  if(intCode is not 0):
228  raise Exception('Input file '+f+ ' is corrupted')
229  if reallyDoIt and options.compress:
230  print "Compressing file",f
231  if( f.lower().endswith(".lhe.xz") ):
232  raise Exception('Input file '+f+' is already compressed! This is inconsistent with the --compress option!')
233  theCompressionCommand = 'xz '+f
234  exeCompression = subprocess.Popen(["/bin/sh","-c",theCompressionCommand])
235  exeCompression.communicate()
236  exeCheckSum = subprocess.Popen(["/afs/cern.ch/cms/caf/bin/cms_adler32",f+".xz"], stdout=subprocess.PIPE)
237  getCheckSum = subprocess.Popen(["awk", "{print $1}"], stdin=exeCheckSum.stdout, stdout=subprocess.PIPE)
238  exeCheckSum.stdout.close()
239  output,err = getCheckSum.communicate()
240  # print 'orig file checksum = ' + output + '\n'
241  theCheckSumList.append(output)
242  theCompressedFilesList.append(f+'.xz')
243  if reallyDoIt and options.compress:
244  theList = theCompressedFilesList
245 
246  newArt = 0
247  uploadPath = ''
248 
249 # new article
250 
251  if options.newId:
252  oldArt = lastArticle()
253  newArt = oldArt+1
254  print 'Creating new article with identifier '+str(newArt)+' ...\n'
255  uploadPath = defaultEOSRootPath+'/'+str(newArt)
256  theCommand = defaultEOSmkdirCommand+' '+uploadPath
257  if reallyDoIt:
258  exeUpload = subprocess.Popen(["/bin/sh","-c",theCommand])
259  exeUpload.communicate()
260 
261 # update article
262 
263  elif options.artIdUp != 0:
264  newArt = options.artIdUp
265  if articleExist(newArt):
266  uploadPath = defaultEOSRootPath+'/'+str(newArt)
267  else:
268  raise Exception('Article '+str(newArt)+' to be updated does not exist!')
269 
270 # list article
271 
272  elif options.artIdLi !=0:
273  listPath = defaultEOSRootPath+'/'+str(options.artIdLi)
274  theCommand = defaultEOSlistCommand+' '+listPath
275  exeList = subprocess.Popen(["/bin/sh","-c",theCommand], stdout=subprocess.PIPE)
276  for line in exeList.stdout.readlines():
277  if findXrdDir(line) != None:
278  print findXrdDir(line)
279 
280 
281  if newArt > 0:
282  fileUpload(uploadPath,theList, theCheckSumList, reallyDoIt)
283  listPath = defaultEOSRootPath+'/'+str(newArt)
284  print ''
285  print 'Listing the '+str(newArt)+' article content after upload:'
286  theCommand = defaultEOSlistCommand+' '+listPath
287  if reallyDoIt:
288  exeFullList = subprocess.Popen(["/bin/sh","-c",theCommand])
289  exeFullList.communicate()
290  else:
291  print 'Dry run, nothing was done'
292 
if(c.getParameter< edm::InputTag >("puppiValueMap").label().size()!=0)
double split
Definition: MVATrainer.cc:139