CMS 3D CMS Logo

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  # Check the local file existence
209  if not os.path.exists(f):
210  raise Exception('Input file '+f+' does not exists')
211  if( f.lower().endswith(".lhe") ):
212  theCheckIntegrityCommand = 'xmllint -noout '+f
213  exeCheckIntegrity = subprocess.Popen(["/bin/sh","-c", theCheckIntegrityCommand])
214  intCode = exeCheckIntegrity.wait()
215  if(intCode is not 0):
216  raise Exception('Input file '+f+ ' is corrupted')
217  if reallyDoIt and options.compress:
218  print "Compressing file",f
219  if( f.lower().endswith(".lhe.xz") ):
220  raise Exception('Input file '+f+' is already compressed! This is inconsistent with the --compress option!')
221  theCompressionCommand = 'xz '+f
222  exeCompression = subprocess.Popen(["/bin/sh","-c",theCompressionCommand])
223  exeCompression.communicate()
224  theCompressedFilesList.append(f+'.xz')
225  if reallyDoIt and options.compress:
226  theList = theCompressedFilesList
227  for f in theList:
228  exeCheckSum = subprocess.Popen(["/afs/cern.ch/cms/caf/bin/cms_adler32",f], stdout=subprocess.PIPE)
229  getCheckSum = subprocess.Popen(["awk", "{print $1}"], stdin=exeCheckSum.stdout, stdout=subprocess.PIPE)
230  exeCheckSum.stdout.close()
231  output,err = getCheckSum.communicate()
232  theCheckSumList.append(output)
233 
234  newArt = 0
235  uploadPath = ''
236 
237 # new article
238 
239  if options.newId:
240  oldArt = lastArticle()
241  newArt = oldArt+1
242  print 'Creating new article with identifier '+str(newArt)+' ...\n'
243  uploadPath = defaultEOSRootPath+'/'+str(newArt)
244  theCommand = defaultEOSmkdirCommand+' '+uploadPath
245  if reallyDoIt:
246  exeUpload = subprocess.Popen(["/bin/sh","-c",theCommand])
247  exeUpload.communicate()
248 
249 # update article
250 
251  elif options.artIdUp != 0:
252  newArt = options.artIdUp
253  if articleExist(newArt):
254  uploadPath = defaultEOSRootPath+'/'+str(newArt)
255  else:
256  raise Exception('Article '+str(newArt)+' to be updated does not exist!')
257 
258 # list article
259 
260  elif options.artIdLi !=0:
261  listPath = defaultEOSRootPath+'/'+str(options.artIdLi)
262  theCommand = defaultEOSlistCommand+' '+listPath
263  exeList = subprocess.Popen(["/bin/sh","-c",theCommand], stdout=subprocess.PIPE)
264  for line in exeList.stdout.readlines():
265  if findXrdDir(line) != None:
266  print findXrdDir(line)
267 
268 
269  if newArt > 0:
270  fileUpload(uploadPath,theList, theCheckSumList, reallyDoIt)
271  listPath = defaultEOSRootPath+'/'+str(newArt)
272  print ''
273  print 'Listing the '+str(newArt)+' article content after upload:'
274  theCommand = defaultEOSlistCommand+' '+listPath
275  if reallyDoIt:
276  exeFullList = subprocess.Popen(["/bin/sh","-c",theCommand])
277  exeFullList.communicate()
278  else:
279  print 'Dry run, nothing was done'
280 
def findXrdDir(theDirRecord)
def fileUpload(uploadPath, lheList, checkSumList, reallyDoIt)
if(dp >Float(M_PI)) dp-
double split
Definition: MVATrainer.cc:139