6 python3 -c "from FWCore.PythonFramework.CmsRun import CmsRun" 2>/dev/null && python_cmd="python3" 7 exec ${python_cmd} $0 ${1+"$@"} 11 from __future__
import print_function
12 print(
'Starting cmsLHEtoEOSManager.py')
14 __version__ =
"$Revision: 1.13 $" 21 defaultEOSRootPath =
'/eos/cms/store/lhe' 22 if "CMSEOS_LHE_ROOT_DIRECTORY" in os.environ:
23 defaultEOSRootPath = os.environ[
"CMSEOS_LHE_ROOT_DIRECTORY"]
24 defaultEOSLoadPath =
'root://eoscms.cern.ch/' 25 defaultEOSlistCommand =
'xrdfs '+defaultEOSLoadPath+
' ls ' 26 defaultEOSmkdirCommand =
'xrdfs '+defaultEOSLoadPath+
' mkdir ' 27 defaultEOSfeCommand =
'xrdfs '+defaultEOSLoadPath+
' stat -q IsReadable ' 28 defaultEOSchecksumCommand =
'xrdfs '+defaultEOSLoadPath+
' query checksum ' 29 defaultEOScpCommand =
'xrdcp -np ' 33 elements = theDirRecord.split(
' ')
35 return elements[-1].rstrip(
'\n').
split(
'/')[-1]
42 theCommand = defaultEOSlistCommand+
' '+defaultEOSRootPath
43 dirList = subprocess.Popen([
"/bin/sh",
"-c",theCommand], stdout=subprocess.PIPE, universal_newlines=
True)
44 for line
in dirList.stdout.readlines():
54 theCommand = defaultEOSlistCommand+
' '+defaultEOSRootPath
55 dirList = subprocess.Popen([
"/bin/sh",
"-c",theCommand], stdout=subprocess.PIPE, universal_newlines=
True)
56 for line
in dirList.stdout.readlines():
58 if line.rstrip(
'\n') !=
'':
66 def fileUpload(uploadPath,lheList, checkSumList, reallyDoIt, force=False):
71 realFileName = f.split(
'/')[-1]
73 newFileName = uploadPath+
'/'+
str(realFileName)
76 theCommand = defaultEOSfeCommand+
' '+newFileName
77 exeFullList = subprocess.Popen([
"/bin/sh",
"-c",theCommand], stdout=subprocess.PIPE, universal_newlines=
True)
78 result = exeFullList.stdout.readlines()
79 if [line
for line
in result
if (
"flags:" in line.lower())
and (
"isreadable" in line.lower())]
and (
not force):
81 print(
'File '+newFileName+
' already exists: do you want to overwrite? [y/n]')
83 if reply ==
'y' or reply ==
'Y':
85 additionalOption =
' -f ' 87 print(
'Overwriting file '+newFileName+
'\n')
91 inUploadScript = defaultEOScpCommand + additionalOption +
' ' +
str(f) +
' ' + defaultEOSLoadPath+uploadPath +
'/' +
str(realFileName)
92 print(
'Uploading file %s...' %
str(f))
94 exeRealUpload = subprocess.Popen([
"/bin/sh",
"-c",inUploadScript])
95 exeRealUpload.communicate()
96 eosCheckSumCommand = defaultEOSchecksumCommand + uploadPath +
'/' +
str(realFileName) +
' | awk \'{print $2}\' | cut -d= -f2' 97 exeEosCheckSum = subprocess.Popen(eosCheckSumCommand ,shell=
True, stdout=subprocess.PIPE, universal_newlines=
True)
98 EosCheckSum = exeEosCheckSum.stdout.read()
99 assert exeEosCheckSum.wait() == 0
101 if checkSumList[index]
not in EosCheckSum:
102 print(
'WARNING! The checksum for file ' +
str(realFileName) +
' in EOS\n')
103 print(EosCheckSum +
'\n')
104 print(
'does not match the checksum of the original one\n')
105 print(checkSumList[index] +
'\n')
106 print(
'please try to re-upload file ' +
str(realFileName) +
' to EOS.\n')
108 print(
'Checksum OK for file ' +
str(realFileName))
117 print(
'\n Upload ended at '+time.asctime(time.localtime(time.time())))
121 if __name__ ==
'__main__':
126 usage=
'cmsLHEtoEOSManager.py <options>' 127 parser = optparse.OptionParser(usage)
128 parser.add_option(
'-f',
'--file',
129 help=
'LHE local file list to be uploaded, separated by ","' ,
133 parser.add_option(
'-F',
'--files-from', metavar =
'FILE',
134 help=
'File containing the list of LHE local files be uploaded, one file per line')
136 parser.add_option(
'-n',
'--new',
137 help=
'Create a new article' ,
142 parser.add_option(
'-u',
'--update',
143 help=
'Update the article <Id>' ,
148 parser.add_option(
'-l',
'--list',
149 help=
'List the files in article <Id>' ,
154 parser.add_option(
'-d',
'--dry-run',
155 help=
'dry run, it does nothing, but you can see what it would do',
160 parser.add_option(
'-c',
'--compress',
161 help=
'compress the local .lhe file with xz before upload',
166 parser.add_option(
'--force',
167 help=
'Force update if file already exists.',
172 (options,args) = parser.parse_args()
177 print(
'cmsLHEtoEOSmanager '+__version__[1:-1])
179 print(
'Running on ',time.asctime(time.localtime(time.time())))
182 reallyDoIt =
not options.dryRun
185 if not options.newId
and options.artIdUp==0
and options.artIdLi==0:
186 raise Exception(
'Please specify the action to be taken, either "-n", "-u" or "-l"!')
188 if options.fileList ==
'' and not options.files_from
and (options.newId
or options.artIdUp!=0):
189 raise Exception(
'Please provide the input file list!')
191 if (options.newId
and (options.artIdUp != 0
or options.artIdLi != 0))
or (options.artIdUp != 0
and options.artIdLi != 0):
192 raise Exception(
'Options "-n", "-u" and "-l" are mutually exclusive, please choose only one!')
195 print(
'Action: create new article\n')
196 elif options.artIdUp != 0:
197 print(
'Action: update article '+
str(options.artIdUp)+
'\n')
198 elif options.artIdLi != 0:
199 print(
'Action: list content of article '+
str(options.artIdLi)+
'\n')
201 if options.artIdLi==0:
203 if len(options.fileList) > 0:
204 theList=(options.fileList.split(
','))
206 if options.files_from:
208 f = open(options.files_from)
210 raise Exception(
'Cannot open the file list, \'%s\'' % options.files_from)
213 if len(l) == 0
or l[0] ==
'#':
217 theCompressedFilesList = []
222 if not ( f.lower().endswith(
".lhe")
or f.lower().endswith(
".lhe.xz") ):
223 raise Exception(
'Input file name must have the "lhe" or "lhe.xz" final extension!')
224 if( f.lower().endswith(
".lhe.xz") ):
225 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")
226 print(
"xmllint file.lhe\n")
227 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")
229 if not os.path.exists(f):
230 raise Exception(
'Input file '+f+
' does not exists')
231 if( f.lower().endswith(
".lhe") ):
232 theCheckIntegrityCommand =
'xmllint -noout '+f
233 exeCheckIntegrity = subprocess.Popen([
"/bin/sh",
"-c", theCheckIntegrityCommand])
234 intCode = exeCheckIntegrity.wait()
236 raise Exception(
'Input file '+f+
' is corrupted')
237 if reallyDoIt
and options.compress:
238 print(
"Compressing file",f)
239 if( f.lower().endswith(
".lhe.xz") ):
240 raise Exception(
'Input file '+f+
' is already compressed! This is inconsistent with the --compress option!')
241 theCompressionCommand =
'xz '+f
242 exeCompression = subprocess.Popen([
"/bin/sh",
"-c",theCompressionCommand])
243 exeCompression.communicate()
244 theCompressedFilesList.append(f+
'.xz')
245 if reallyDoIt
and options.compress:
246 theList = theCompressedFilesList
249 exeCheckSum = subprocess.Popen([
"/afs/cern.ch/cms/caf/bin/cms_adler32",f], stdout=subprocess.PIPE, universal_newlines=
True)
250 getCheckSum = subprocess.Popen([
"awk",
"{print $1}"], stdin=exeCheckSum.stdout, stdout=subprocess.PIPE, universal_newlines=
True)
251 exeCheckSum.stdout.close()
252 output,err = getCheckSum.communicate()
253 theCheckSumList.append(output.strip())
255 theCheckSumList.append(
"missing-adler32")
265 print(
'Creating new article with identifier '+
str(newArt)+
' ...\n')
266 uploadPath = defaultEOSRootPath+
'/'+
str(newArt)
267 theCommand = defaultEOSmkdirCommand+
' '+uploadPath
269 exeUpload = subprocess.Popen([
"/bin/sh",
"-c",theCommand])
270 exeUpload.communicate()
274 elif options.artIdUp != 0:
275 newArt = options.artIdUp
277 uploadPath = defaultEOSRootPath+
'/'+
str(newArt)
279 raise Exception(
'Article '+
str(newArt)+
' to be updated does not exist!')
283 elif options.artIdLi !=0:
284 listPath = defaultEOSRootPath+
'/'+
str(options.artIdLi)
285 theCommand = defaultEOSlistCommand+
' '+listPath
286 exeList = subprocess.Popen([
"/bin/sh",
"-c",theCommand], stdout=subprocess.PIPE, universal_newlines=
True)
287 for line
in exeList.stdout.readlines():
293 fileUpload(uploadPath,theList, theCheckSumList, reallyDoIt, options.force)
294 listPath = defaultEOSRootPath+
'/'+
str(newArt)
296 print(
'Listing the '+
str(newArt)+
' article content after upload:')
297 theCommand = defaultEOSlistCommand+
' '+listPath
299 exeFullList = subprocess.Popen([
"/bin/sh",
"-c",theCommand])
300 exeFullList.communicate()
302 print(
'Dry run, nothing was done')
def findXrdDir(theDirRecord)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
def split(sequence, size)
def fileUpload(uploadPath, lheList, checkSumList, reallyDoIt, force=False)
if(threadIdxLocalY==0 &&threadIdxLocalX==0)