00001
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
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
00075 if addFile:
00076 print 'Adding file '+str(f)+'\n'
00077 inUploadScript += defaultEOScpCommand+additionalOption+' '+str(f)+' '+defaultEOSLoadPath+uploadPath+'/'+str(realFileName)+'\n'
00078
00079
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
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 xz before upload',
00125 action='store_true',
00126 default=False,
00127 dest='compress')
00128
00129 (options,args) = parser.parse_args()
00130
00131
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
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
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 if( f.lower().endswith(".lhe.xz") ):
00166 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"
00167 print "xmllint file.lhe\n"
00168 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"
00169
00170 if not os.path.exists(f):
00171 raise Exception('Input file '+f+' does not exists')
00172 if( f.lower().endswith(".lhe") ):
00173 theCheckIntegrityCommand = 'xmllint -noout '+f
00174 exeCheckIntegrity = subprocess.Popen(["/bin/sh","-c", theCheckIntegrityCommand])
00175 intCode = exeCheckIntegrity.wait()
00176 if(intCode is not 0):
00177 raise Exception('Input file '+f+ ' is corrupted')
00178 if reallyDoIt and options.compress:
00179 print "Compressing file",f
00180 theCompressionCommand = 'xz '+f
00181 exeCompression = subprocess.Popen(["/bin/sh","-c",theCompressionCommand])
00182 exeCompression.communicate()
00183 theCompressedFilesList.append(f+'.xz')
00184 if reallyDoIt and options.compress:
00185 theList = theCompressedFilesList
00186
00187
00188
00189 newArt = 0
00190 uploadPath = ''
00191
00192
00193
00194 if options.newId:
00195 oldArt = lastArticle()
00196 newArt = oldArt+1
00197 print 'Creating new article with identifier '+str(newArt)+' ...\n'
00198 uploadPath = defaultEOSRootPath+'/'+str(newArt)
00199 theCommand = defaultEOSmkdirCommand+' '+uploadPath
00200 if reallyDoIt:
00201 exeUpload = subprocess.Popen(["/bin/sh","-c",theCommand])
00202 exeUpload.communicate()
00203
00204
00205
00206 elif options.artIdUp != 0:
00207 newArt = options.artIdUp
00208 if articleExist(newArt):
00209 uploadPath = defaultEOSRootPath+'/'+str(newArt)
00210 else:
00211 raise('Article '+str(newArt)+' to be updated does not exist!')
00212
00213
00214
00215 elif options.artIdLi !=0:
00216 listPath = defaultEOSRootPath+'/'+str(options.artIdLi)
00217 theCommand = defaultEOSlistCommand+' '+listPath
00218 exeList = subprocess.Popen(["/bin/sh","-c",theCommand], stdout=subprocess.PIPE)
00219 for line in exeList.stdout.readlines():
00220 if findXrdDir(line) != None:
00221 print findXrdDir(line)
00222
00223
00224 if newArt > 0:
00225 fileUpload(uploadPath,theList, reallyDoIt)
00226 listPath = defaultEOSRootPath+'/'+str(newArt)
00227 print ''
00228 print 'Listing the '+str(newArt)+' article content after upload:'
00229 theCommand = defaultEOSlistCommand+' '+listPath
00230 if reallyDoIt:
00231 exeFullList = subprocess.Popen(["/bin/sh","-c",theCommand])
00232 exeFullList.communicate()
00233 else:
00234 print 'Dry run, nothing was done'
00235