00001
00002
00003 __version__ = "$Revision: 1.9 $"
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 (options,args) = parser.parse_args()
00124
00125
00126
00127 print ''
00128 print 'cmsLHEtoEOSmanager '+__version__[1:-1]
00129 print ''
00130 print 'Running on ',time.asctime(time.localtime(time.time()))
00131 print ''
00132
00133 reallyDoIt = not options.dryRun
00134
00135
00136 if not options.newId and options.artIdUp==0 and options.artIdLi==0:
00137 raise Exception('Please specify the action to be taken, either "-n", "-u" or "-l"!')
00138
00139 if options.fileList=='' and (options.newId or options.artIdUp!=0):
00140 raise Exception('Please provide the input file list!')
00141
00142 if (options.newId and (options.artIdUp != 0 or options.artIdLi != 0)) or (options.artIdUp != 0 and options.artIdLi != 0):
00143 raise Exception('Options "-n", "-u" and "-l" are mutually exclusive, please chose only one!')
00144
00145 if options.newId:
00146 print 'Action: create new article\n'
00147 elif options.artIdUp != 0:
00148 print 'Action: update article '+str(options.artIdUp)+'\n'
00149 elif options.artIdLi != 0:
00150 print 'Action: list content of article '+str(options.artIdLi)+'\n'
00151
00152 if options.artIdLi==0:
00153 theList = options.fileList.split(',')
00154 for f in theList:
00155
00156 if not f.lower().endswith(".lhe"):
00157 raise Exception('Input file name must have the "lhe" final extension!')
00158
00159 if not os.path.exists(f):
00160 raise Exception('Input file '+f+' does not exists')
00161
00162
00163 newArt = 0
00164 uploadPath = ''
00165
00166
00167
00168 if options.newId:
00169 oldArt = lastArticle()
00170 newArt = oldArt+1
00171 print 'Creating new article with identifier '+str(newArt)+' ...\n'
00172 uploadPath = defaultEOSRootPath+'/'+str(newArt)
00173 theCommand = defaultEOSmkdirCommand+' '+uploadPath
00174 if reallyDoIt:
00175 exeUpload = subprocess.Popen(["/bin/sh","-c",theCommand])
00176 exeUpload.communicate()
00177
00178
00179
00180 elif options.artIdUp != 0:
00181 newArt = options.artIdUp
00182 if articleExist(newArt):
00183 uploadPath = defaultEOSRootPath+'/'+str(newArt)
00184 else:
00185 raise('Article '+str(newArt)+' to be updated does not exist!')
00186
00187
00188
00189 elif options.artIdLi !=0:
00190 listPath = defaultEOSRootPath+'/'+str(options.artIdLi)
00191 theCommand = defaultEOSlistCommand+' '+listPath
00192 exeList = subprocess.Popen(["/bin/sh","-c",theCommand], stdout=subprocess.PIPE)
00193 for line in exeList.stdout.readlines():
00194 if findXrdDir(line) != None:
00195 print findXrdDir(line)
00196
00197
00198 if newArt > 0:
00199 fileUpload(uploadPath,theList, reallyDoIt)
00200 listPath = defaultEOSRootPath+'/'+str(newArt)
00201 print ''
00202 print 'Listing the '+str(newArt)+' article content after upload:'
00203 theCommand = defaultEOSlistCommand+' '+listPath
00204 if reallyDoIt:
00205 exeFullList = subprocess.Popen(["/bin/sh","-c",theCommand])
00206 exeFullList.communicate()
00207 else:
00208 print 'Dry run, nothing was done'
00209