3 print 'Starting cmsLHEtoEOSManager.py'
5 __version__ =
"$Revision: 1.13 $"
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 '
21 elements = theDirRecord.split(
' ')
23 return elements[-1].rstrip(
'\n').
split(
'/')[-1]
30 theCommand = defaultEOSlistCommand+
' '+defaultEOSRootPath
31 dirList = subprocess.Popen([
"/bin/sh",
"-c",theCommand], stdout=subprocess.PIPE)
32 for line
in dirList.stdout.readlines():
42 theCommand = defaultEOSlistCommand+
' '+defaultEOSRootPath
43 dirList = subprocess.Popen([
"/bin/sh",
"-c",theCommand], stdout=subprocess.PIPE)
44 for line
in dirList.stdout.readlines():
46 if line.rstrip(
'\n') !=
'':
54 def fileUpload(uploadPath,lheList, checkSumList, reallyDoIt):
59 realFileName = f.split(
'/')[-1]
61 newFileName = uploadPath+
'/'+str(realFileName)
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.':
69 print 'File '+newFileName+
' already exists: do you want to overwrite? [y/n]'
71 if reply ==
'y' or reply ==
'Y':
73 additionalOption =
' -f '
75 print 'Overwriting file '+newFileName+
'\n'
79 inUploadScript = defaultEOScpCommand + additionalOption +
' ' + str(f) +
' ' + defaultEOSLoadPath+uploadPath +
'/' + str(realFileName)
80 print 'Uploading file %s...' % str(f)
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
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'
96 print 'Checksum OK for file ' + str(realFileName)
105 print '\n Upload ended at '+time.asctime(time.localtime(time.time()))
109 if __name__ ==
'__main__':
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 ","' ,
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')
124 parser.add_option(
'-n',
'--new',
125 help=
'Create a new article' ,
130 parser.add_option(
'-u',
'--update',
131 help=
'Update the article <Id>' ,
135 parser.add_option(
'-l',
'--list',
136 help=
'List the files in article <Id>' ,
140 parser.add_option(
'-d',
'--dry-run',
141 help=
'dry run, it does nothing, but you can see what it would do',
146 parser.add_option(
'-c',
'--compress',
147 help=
'compress the local .lhe file with xz before upload',
152 (options,args) = parser.parse_args()
157 print 'cmsLHEtoEOSmanager '+__version__[1:-1]
159 print 'Running on ',time.asctime(time.localtime(time.time()))
162 reallyDoIt =
not options.dryRun
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"!')
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!')
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!')
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'
181 if options.artIdLi==0:
183 if len(options.fileList) > 0:
184 theList=(options.fileList.split(
','))
186 if options.files_from:
188 f = open(options.files_from)
190 raise Exception(
'Cannot open the file list, \'%s\'' % options.files_from)
193 if len(l) == 0
or l[0] ==
'#':
197 theCompressedFilesList = []
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"
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
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)
242 print 'Creating new article with identifier '+str(newArt)+
' ...\n'
243 uploadPath = defaultEOSRootPath+
'/'+str(newArt)
244 theCommand = defaultEOSmkdirCommand+
' '+uploadPath
246 exeUpload = subprocess.Popen([
"/bin/sh",
"-c",theCommand])
247 exeUpload.communicate()
251 elif options.artIdUp != 0:
252 newArt = options.artIdUp
254 uploadPath = defaultEOSRootPath+
'/'+str(newArt)
256 raise Exception(
'Article '+str(newArt)+
' to be updated does not exist!')
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():
270 fileUpload(uploadPath,theList, theCheckSumList, reallyDoIt)
271 listPath = defaultEOSRootPath+
'/'+str(newArt)
273 print 'Listing the '+str(newArt)+
' article content after upload:'
274 theCommand = defaultEOSlistCommand+
' '+listPath
276 exeFullList = subprocess.Popen([
"/bin/sh",
"-c",theCommand])
277 exeFullList.communicate()
279 print 'Dry run, nothing was done'