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 __version__ = "$Revision: 1.12 $"
4 
5 import os
6 import subprocess
7 import time
8 import re
9 
10 defaultEOSRootPath = '/eos/cms/store/lhe'
11 defaultEOSLoadPath = 'root://eoscms/'
12 defaultEOSlistCommand = 'xrd eoscms dirlist '
13 defaultEOSmkdirCommand = 'xrd eoscms mkdir '
14 defaultEOSfeCommand = 'xrd eoscms existfile '
15 defaultEOScpCommand = 'xrdcp -np '
16 
17 def findXrdDir(theDirRecord):
18 
19  elements = theDirRecord.split(' ')
20  if len(elements) > 1:
21  return elements[-1].rstrip('\n').split('/')[-1]
22  else:
23  return None
24 
25 def articleExist(artId):
26 
27  itExists = False
28  theCommand = defaultEOSlistCommand+' '+defaultEOSRootPath
29  dirList = subprocess.Popen(["/bin/sh","-c",theCommand], stdout=subprocess.PIPE)
30  for line in dirList.stdout.readlines():
31  if findXrdDir(line) == str(artId):
32  itExists = True
33 
34  return itExists
35 
37 
38  artList = [0]
39 
40  theCommand = defaultEOSlistCommand+' '+defaultEOSRootPath
41  dirList = subprocess.Popen(["/bin/sh","-c",theCommand], stdout=subprocess.PIPE)
42  for line in dirList.stdout.readlines():
43  try:
44  if line.rstrip('\n') != '':
45  artList.append(int(findXrdDir(line)))
46  except:
47  break
48 
49  return max(artList)
50 
51 
52 def fileUpload(uploadPath,lheList, reallyDoIt):
53 
54  inUploadScript = ''
55 
56  for f in lheList:
57  realFileName = f.split('/')[-1]
58  # Check the file existence
59  newFileName = uploadPath+'/'+str(realFileName)
60  addFile = True
61  additionalOption = ''
62  theCommand = defaultEOSfeCommand+' '+newFileName
63  exeFullList = subprocess.Popen(["/bin/sh","-c",theCommand], stdout=subprocess.PIPE)
64  result = exeFullList.stdout.readlines()
65  if result[0].rstrip('\n') == 'The file exists.':
66  addFile = False
67  print 'File '+newFileName+' already exists: do you want to overwrite? [y/n]'
68  reply = raw_input()
69  if reply == 'y' or reply == 'Y':
70  addFile = True
71  additionalOption = ' -f '
72  print ''
73  print 'Overwriting file '+newFileName+'\n'
74  # add the file
75  if addFile:
76  print 'Adding file '+str(f)+'\n'
77  inUploadScript += defaultEOScpCommand+additionalOption+' '+str(f)+' '+defaultEOSLoadPath+uploadPath+'/'+str(realFileName)+'\n'
78 
79 # launch the upload shell script
80 
81  print '\n Launching upload script \n'+inUploadScript+'\n at '+time.asctime(time.localtime(time.time()))+' ...\n'
82  if reallyDoIt:
83  exeRealUpload = subprocess.Popen(["/bin/sh","-c",inUploadScript])
84  exeRealUpload.communicate()
85  print '\n Upload ended at '+time.asctime(time.localtime(time.time()))
86 
87 #################################################################################################
88 
89 if __name__ == '__main__':
90 
91  import optparse
92 
93  # Here we define an option parser to handle commandline options..
94  usage='cmsLHEtoEOSManager.py <options>'
95  parser = optparse.OptionParser(usage)
96  parser.add_option('-f', '--file',
97  help='LHE local file list to be uploaded, separated by ","' ,
98  default='',
99  dest='fileList')
100 
101  parser.add_option('-n', '--new',
102  help='Create a new article' ,
103  action='store_true',
104  default=False,
105  dest='newId')
106 
107  parser.add_option('-u', '--update',
108  help='Update the article <Id>' ,
109  default=0,
110  dest='artIdUp')
111 
112  parser.add_option('-l', '--list',
113  help='List the files in article <Id>' ,
114  default=0,
115  dest='artIdLi')
116 
117  parser.add_option('-d', '--dry-run',
118  help='dry run, it does nothing, but you can see what it would do',
119  action='store_true',
120  default=False,
121  dest='dryRun')
122 
123  parser.add_option('-c', '--compress',
124  help='compress the local .lhe file with xz before upload',
125  action='store_true',
126  default=False,
127  dest='compress')
128 
129  (options,args) = parser.parse_args()
130 
131  # print banner
132 
133  print ''
134  print 'cmsLHEtoEOSmanager '+__version__[1:-1]
135  print ''
136  print 'Running on ',time.asctime(time.localtime(time.time()))
137  print ''
138 
139  reallyDoIt = not options.dryRun
140 
141  # Now some fault control..If an error is found we raise an exception
142  if not options.newId and options.artIdUp==0 and options.artIdLi==0:
143  raise Exception('Please specify the action to be taken, either "-n", "-u" or "-l"!')
144 
145  if options.fileList=='' and (options.newId or options.artIdUp!=0):
146  raise Exception('Please provide the input file list!')
147 
148  if (options.newId and (options.artIdUp != 0 or options.artIdLi != 0)) or (options.artIdUp != 0 and options.artIdLi != 0):
149  raise Exception('Options "-n", "-u" and "-l" are mutually exclusive, please chose only one!')
150 
151  if options.newId:
152  print 'Action: create new article\n'
153  elif options.artIdUp != 0:
154  print 'Action: update article '+str(options.artIdUp)+'\n'
155  elif options.artIdLi != 0:
156  print 'Action: list content of article '+str(options.artIdLi)+'\n'
157 
158  if options.artIdLi==0:
159  theList = options.fileList.split(',')
160  theCompressedFilesList = []
161  for f in theList:
162  # Check the file name extension
163  if not ( f.lower().endswith(".lhe") or f.lower().endswith(".lhe.xz") ):
164  raise Exception('Input file name must have the "lhe" or "lhe.xz" final extension!')
165  if( f.lower().endswith(".lhe.xz") ):
166  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"
167  print "xmllint file.lhe\n"
168  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"
169  # Check the local file existence
170  if not os.path.exists(f):
171  raise Exception('Input file '+f+' does not exists')
172  if( f.lower().endswith(".lhe") ):
173  theCheckIntegrityCommand = 'xmllint -noout '+f
174  exeCheckIntegrity = subprocess.Popen(["/bin/sh","-c", theCheckIntegrityCommand])
175  intCode = exeCheckIntegrity.wait()
176  if(intCode is not 0):
177  raise Exception('Input file '+f+ ' is corrupted')
178  if reallyDoIt and options.compress:
179  print "Compressing file",f
180  theCompressionCommand = 'xz '+f
181  exeCompression = subprocess.Popen(["/bin/sh","-c",theCompressionCommand])
182  exeCompression.communicate()
183  theCompressedFilesList.append(f+'.xz')
184  if reallyDoIt and options.compress:
185  theList = theCompressedFilesList
186 
187 
188 
189  newArt = 0
190  uploadPath = ''
191 
192 # new article
193 
194  if options.newId:
195  oldArt = lastArticle()
196  newArt = oldArt+1
197  print 'Creating new article with identifier '+str(newArt)+' ...\n'
198  uploadPath = defaultEOSRootPath+'/'+str(newArt)
199  theCommand = defaultEOSmkdirCommand+' '+uploadPath
200  if reallyDoIt:
201  exeUpload = subprocess.Popen(["/bin/sh","-c",theCommand])
202  exeUpload.communicate()
203 
204 # update article
205 
206  elif options.artIdUp != 0:
207  newArt = options.artIdUp
208  if articleExist(newArt):
209  uploadPath = defaultEOSRootPath+'/'+str(newArt)
210  else:
211  raise('Article '+str(newArt)+' to be updated does not exist!')
212 
213 # list article
214 
215  elif options.artIdLi !=0:
216  listPath = defaultEOSRootPath+'/'+str(options.artIdLi)
217  theCommand = defaultEOSlistCommand+' '+listPath
218  exeList = subprocess.Popen(["/bin/sh","-c",theCommand], stdout=subprocess.PIPE)
219  for line in exeList.stdout.readlines():
220  if findXrdDir(line) != None:
221  print findXrdDir(line)
222 
223 
224  if newArt > 0:
225  fileUpload(uploadPath,theList, reallyDoIt)
226  listPath = defaultEOSRootPath+'/'+str(newArt)
227  print ''
228  print 'Listing the '+str(newArt)+' article content after upload:'
229  theCommand = defaultEOSlistCommand+' '+listPath
230  if reallyDoIt:
231  exeFullList = subprocess.Popen(["/bin/sh","-c",theCommand])
232  exeFullList.communicate()
233  else:
234  print 'Dry run, nothing was done'
235 
const T & max(const T &a, const T &b)
if(dp >Float(M_PI)) dp-
double split
Definition: MVATrainer.cc:139