CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2_patch1/src/GeneratorInterface/LHEInterface/scripts/cmsLHEtoEOSManager.py

Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 __version__ = "$Revision: 1.12 $"
00004 
00005 import os
00006 import subprocess
00007 import time
00008 import re
00009 
00010 defaultEOSRootPath = '/eos/cms/store/lhe'
00011 defaultEOSLoadPath = 'root://eoscms/'
00012 defaultEOSlistCommand = 'xrd eoscms dirlist '
00013 defaultEOSmkdirCommand = 'xrd eoscms mkdir '
00014 defaultEOSfeCommand = 'xrd eoscms existfile '
00015 defaultEOScpCommand = 'xrdcp -np '
00016 
00017 def findXrdDir(theDirRecord):
00018 
00019     elements = theDirRecord.split(' ')
00020     if len(elements) > 1:
00021         return elements[-1].rstrip('\n').split('/')[-1]
00022     else:
00023         return None
00024 
00025 def articleExist(artId):
00026 
00027     itExists = False
00028     theCommand = defaultEOSlistCommand+' '+defaultEOSRootPath
00029     dirList = subprocess.Popen(["/bin/sh","-c",theCommand], stdout=subprocess.PIPE)
00030     for line in dirList.stdout.readlines():
00031         if findXrdDir(line) == str(artId): 
00032             itExists = True
00033 
00034     return itExists
00035 
00036 def lastArticle():
00037 
00038     artList = [0]
00039 
00040     theCommand = defaultEOSlistCommand+' '+defaultEOSRootPath
00041     dirList = subprocess.Popen(["/bin/sh","-c",theCommand], stdout=subprocess.PIPE)
00042     for line in dirList.stdout.readlines():
00043         try:
00044             if line.rstrip('\n') != '':
00045                 artList.append(int(findXrdDir(line)))
00046         except:
00047             break
00048 
00049     return max(artList)
00050 
00051 
00052 def fileUpload(uploadPath,lheList, reallyDoIt):
00053 
00054     inUploadScript = ''
00055 
00056     for f in lheList:
00057         realFileName = f.split('/')[-1]
00058         # Check the file existence
00059         newFileName = uploadPath+'/'+str(realFileName)
00060         addFile = True
00061         additionalOption = ''  
00062         theCommand = defaultEOSfeCommand+' '+newFileName
00063         exeFullList = subprocess.Popen(["/bin/sh","-c",theCommand], stdout=subprocess.PIPE)
00064         result = exeFullList.stdout.readlines()
00065         if result[0].rstrip('\n') == 'The file exists.':
00066             addFile = False
00067             print 'File '+newFileName+' already exists: do you want to overwrite? [y/n]'
00068             reply = raw_input()
00069             if reply == 'y' or reply == 'Y':
00070                 addFile = True
00071                 additionalOption = ' -f '
00072                 print ''
00073                 print 'Overwriting file '+newFileName+'\n'
00074         # add the file
00075         if addFile:
00076             print 'Adding file '+str(f)+'\n'
00077             inUploadScript += defaultEOScpCommand+additionalOption+' '+str(f)+' '+defaultEOSLoadPath+uploadPath+'/'+str(realFileName)+'\n'
00078 
00079 # launch the upload shell script        
00080 
00081     print '\n Launching upload script \n'+inUploadScript+'\n at '+time.asctime(time.localtime(time.time()))+' ...\n'
00082     if reallyDoIt:  
00083       exeRealUpload = subprocess.Popen(["/bin/sh","-c",inUploadScript])
00084       exeRealUpload.communicate()
00085     print '\n Upload ended at '+time.asctime(time.localtime(time.time()))
00086 
00087 #################################################################################################    
00088         
00089 if __name__ == '__main__':
00090     
00091     import optparse
00092     
00093     # Here we define an option parser to handle commandline options..
00094     usage='cmsLHEtoEOSManager.py <options>'
00095     parser = optparse.OptionParser(usage)
00096     parser.add_option('-f', '--file',
00097                       help='LHE local file list to be uploaded, separated by ","' ,
00098                       default='',
00099                       dest='fileList')
00100 
00101     parser.add_option('-n', '--new', 
00102                       help='Create a new article' ,
00103                       action='store_true',
00104                       default=False,
00105                       dest='newId')                      
00106 
00107     parser.add_option('-u', '--update', 
00108                       help='Update the article <Id>' ,
00109                       default=0,
00110                       dest='artIdUp')                      
00111 
00112     parser.add_option('-l', '--list', 
00113                       help='List the files in article <Id>' ,
00114                       default=0,
00115                       dest='artIdLi')                     
00116     
00117     parser.add_option('-d', '--dry-run',
00118                       help='dry run, it does nothing, but you can see what it would do',
00119                       action='store_true',
00120                       default=False,
00121                       dest='dryRun')
00122     
00123     parser.add_option('-c', '--compress',
00124                       help='compress the local .lhe file with xc before upload',
00125                       action='store_true',
00126                       default=False,
00127                       dest='compress')
00128 
00129     (options,args) = parser.parse_args()
00130 
00131     # print banner
00132 
00133     print ''
00134     print 'cmsLHEtoEOSmanager '+__version__[1:-1]
00135     print ''
00136     print 'Running on ',time.asctime(time.localtime(time.time()))
00137     print ''
00138     
00139     reallyDoIt = not options.dryRun
00140 
00141     # Now some fault control..If an error is found we raise an exception
00142     if not options.newId and options.artIdUp==0 and options.artIdLi==0:
00143         raise Exception('Please specify the action to be taken, either "-n", "-u" or "-l"!')
00144     
00145     if options.fileList=='' and (options.newId or options.artIdUp!=0):
00146         raise Exception('Please provide the input file list!')
00147 
00148     if (options.newId and (options.artIdUp != 0 or options.artIdLi != 0)) or (options.artIdUp != 0 and options.artIdLi != 0):
00149         raise Exception('Options "-n", "-u" and "-l" are mutually exclusive, please chose only one!')
00150 
00151     if options.newId:
00152         print 'Action: create new article\n'
00153     elif options.artIdUp != 0:
00154         print 'Action: update article '+str(options.artIdUp)+'\n'
00155     elif options.artIdLi != 0:
00156         print 'Action: list content of article '+str(options.artIdLi)+'\n'
00157 
00158     if options.artIdLi==0:
00159         theList = options.fileList.split(',')
00160         theCompressedFilesList = []
00161         for f in theList: 
00162             # Check the file name extension
00163             if not ( f.lower().endswith(".lhe") or f.lower().endswith(".lhe.xz") ):
00164                 raise Exception('Input file name must have the "lhe" or "lhe.xz" final extension!')
00165             # Check the local file existence
00166             if not os.path.exists(f):
00167                 raise Exception('Input file '+f+' does not exists')
00168             if reallyDoIt and options.compress:
00169               print "Compressing file",f
00170               theCompressionCommand = 'xz '+f
00171               exeCompression = subprocess.Popen(["/bin/sh","-c",theCompressionCommand])
00172               exeCompression.communicate()
00173               theCompressedFilesList.append(f+'.xz')
00174         if reallyDoIt and options.compress:
00175           theList = theCompressedFilesList
00176               
00177         
00178 
00179     newArt = 0
00180     uploadPath = ''
00181 
00182 # new article
00183 
00184     if options.newId:
00185         oldArt = lastArticle()
00186         newArt = oldArt+1
00187         print 'Creating new article with identifier '+str(newArt)+' ...\n'
00188         uploadPath = defaultEOSRootPath+'/'+str(newArt)
00189         theCommand = defaultEOSmkdirCommand+' '+uploadPath
00190         if reallyDoIt:
00191           exeUpload = subprocess.Popen(["/bin/sh","-c",theCommand])
00192           exeUpload.communicate()
00193 
00194 # update article
00195         
00196     elif options.artIdUp != 0:
00197         newArt = options.artIdUp
00198         if articleExist(newArt):
00199             uploadPath = defaultEOSRootPath+'/'+str(newArt)
00200         else:
00201             raise('Article '+str(newArt)+' to be updated does not exist!')
00202 
00203 # list article
00204         
00205     elif options.artIdLi !=0:
00206         listPath = defaultEOSRootPath+'/'+str(options.artIdLi)
00207         theCommand = defaultEOSlistCommand+' '+listPath
00208         exeList = subprocess.Popen(["/bin/sh","-c",theCommand], stdout=subprocess.PIPE)
00209         for line in exeList.stdout.readlines():
00210             if findXrdDir(line) != None:
00211                 print findXrdDir(line)
00212 
00213 
00214     if newArt > 0:
00215         fileUpload(uploadPath,theList, reallyDoIt)
00216         listPath = defaultEOSRootPath+'/'+str(newArt)
00217         print ''
00218         print 'Listing the '+str(newArt)+' article content after upload:'
00219         theCommand = defaultEOSlistCommand+' '+listPath
00220         if reallyDoIt:
00221           exeFullList = subprocess.Popen(["/bin/sh","-c",theCommand])
00222           exeFullList.communicate()
00223         else:
00224           print 'Dry run, nothing was done'
00225