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"
208 exeCheckSum = subprocess.Popen([
"/afs/cern.ch/cms/caf/bin/cms_adler32",f], stdout=subprocess.PIPE)
209 getCheckSum = subprocess.Popen([
"awk",
"{print $1}"], stdin=exeCheckSum.stdout, stdout=subprocess.PIPE)
210 exeCheckSum.stdout.close()
211 output,err = getCheckSum.communicate()
213 theCheckSumList.append(output)
215 if not os.path.exists(f):
216 raise Exception(
'Input file '+f+
' does not exists')
217 if( f.lower().endswith(
".lhe") ):
218 theCheckIntegrityCommand =
'xmllint -noout '+f
219 exeCheckIntegrity = subprocess.Popen([
"/bin/sh",
"-c", theCheckIntegrityCommand])
220 intCode = exeCheckIntegrity.wait()
221 exeCheckSum = subprocess.Popen([
"/afs/cern.ch/cms/caf/bin/cms_adler32",f], stdout=subprocess.PIPE)
222 getCheckSum = subprocess.Popen([
"awk",
"{print $1}"], stdin=exeCheckSum.stdout, stdout=subprocess.PIPE)
223 exeCheckSum.stdout.close()
224 output,err = getCheckSum.communicate()
226 theCheckSumList.append(output)
227 if(intCode
is not 0):
228 raise Exception(
'Input file '+f+
' is corrupted')
229 if reallyDoIt
and options.compress:
230 print "Compressing file",f
231 if( f.lower().endswith(
".lhe.xz") ):
232 raise Exception(
'Input file '+f+
' is already compressed! This is inconsistent with the --compress option!')
233 theCompressionCommand =
'xz '+f
234 exeCompression = subprocess.Popen([
"/bin/sh",
"-c",theCompressionCommand])
235 exeCompression.communicate()
236 exeCheckSum = subprocess.Popen([
"/afs/cern.ch/cms/caf/bin/cms_adler32",f+
".xz"], stdout=subprocess.PIPE)
237 getCheckSum = subprocess.Popen([
"awk",
"{print $1}"], stdin=exeCheckSum.stdout, stdout=subprocess.PIPE)
238 exeCheckSum.stdout.close()
239 output,err = getCheckSum.communicate()
241 theCheckSumList.append(output)
242 theCompressedFilesList.append(f+
'.xz')
243 if reallyDoIt
and options.compress:
244 theList = theCompressedFilesList
254 print 'Creating new article with identifier '+str(newArt)+
' ...\n'
255 uploadPath = defaultEOSRootPath+
'/'+str(newArt)
256 theCommand = defaultEOSmkdirCommand+
' '+uploadPath
258 exeUpload = subprocess.Popen([
"/bin/sh",
"-c",theCommand])
259 exeUpload.communicate()
263 elif options.artIdUp != 0:
264 newArt = options.artIdUp
266 uploadPath = defaultEOSRootPath+
'/'+str(newArt)
268 raise Exception(
'Article '+str(newArt)+
' to be updated does not exist!')
272 elif options.artIdLi !=0:
273 listPath = defaultEOSRootPath+
'/'+str(options.artIdLi)
274 theCommand = defaultEOSlistCommand+
' '+listPath
275 exeList = subprocess.Popen([
"/bin/sh",
"-c",theCommand], stdout=subprocess.PIPE)
276 for line
in exeList.stdout.readlines():
282 fileUpload(uploadPath,theList, theCheckSumList, reallyDoIt)
283 listPath = defaultEOSRootPath+
'/'+str(newArt)
285 print 'Listing the '+str(newArt)+
' article content after upload:'
286 theCommand = defaultEOSlistCommand+
' '+listPath
288 exeFullList = subprocess.Popen([
"/bin/sh",
"-c",theCommand])
289 exeFullList.communicate()
291 print 'Dry run, nothing was done'
if(c.getParameter< edm::InputTag >("puppiValueMap").label().size()!=0)