3 from __future__
import print_function
4 print(
'Starting cmsLHEtoEOSManager.py')
6 __version__ =
"$Revision: 1.13 $"
13 defaultEOSRootPath =
'/eos/cms/store/lhe'
14 if "CMSEOS_LHE_ROOT_DIRECTORY" in os.environ:
15 defaultEOSRootPath = os.environ[
"CMSEOS_LHE_ROOT_DIRECTORY"]
16 defaultEOSLoadPath =
'root://eoscms.cern.ch/'
17 defaultEOSlistCommand =
'xrdfs '+defaultEOSLoadPath+
' ls '
18 defaultEOSmkdirCommand =
'xrdfs '+defaultEOSLoadPath+
' mkdir '
19 defaultEOSfeCommand =
'xrdfs '+defaultEOSLoadPath+
' stat -q IsReadable '
20 defaultEOSchecksumCommand =
'xrdfs '+defaultEOSLoadPath+
' query checksum '
21 defaultEOScpCommand =
'xrdcp -np '
25 elements = theDirRecord.split(
' ')
27 return elements[-1].rstrip(
'\n').
split(
'/')[-1]
34 theCommand = defaultEOSlistCommand+
' '+defaultEOSRootPath
35 dirList = subprocess.Popen([
"/bin/sh",
"-c",theCommand], stdout=subprocess.PIPE, universal_newlines=
True)
36 for line
in dirList.stdout.readlines():
46 theCommand = defaultEOSlistCommand+
' '+defaultEOSRootPath
47 dirList = subprocess.Popen([
"/bin/sh",
"-c",theCommand], stdout=subprocess.PIPE, universal_newlines=
True)
48 for line
in dirList.stdout.readlines():
50 if line.rstrip(
'\n') !=
'':
58 def fileUpload(uploadPath,lheList, checkSumList, reallyDoIt, force=False):
63 realFileName = f.split(
'/')[-1]
65 newFileName = uploadPath+
'/'+
str(realFileName)
68 theCommand = defaultEOSfeCommand+
' '+newFileName
69 exeFullList = subprocess.Popen([
"/bin/sh",
"-c",theCommand], stdout=subprocess.PIPE, universal_newlines=
True)
70 result = exeFullList.stdout.readlines()
71 if [line
for line
in result
if (
"flags:" in line.lower())
and (
"isreadable" in line.lower())]
and (
not force):
73 print(
'File '+newFileName+
' already exists: do you want to overwrite? [y/n]')
75 if reply ==
'y' or reply ==
'Y':
77 additionalOption =
' -f '
79 print(
'Overwriting file '+newFileName+
'\n')
83 inUploadScript = defaultEOScpCommand + additionalOption +
' ' +
str(f) +
' ' + defaultEOSLoadPath+uploadPath +
'/' +
str(realFileName)
84 print(
'Uploading file %s...' %
str(f))
86 exeRealUpload = subprocess.Popen([
"/bin/sh",
"-c",inUploadScript])
87 exeRealUpload.communicate()
88 eosCheckSumCommand = defaultEOSchecksumCommand + uploadPath +
'/' +
str(realFileName) +
' | awk \'{print $2}\' | cut -d= -f2'
89 exeEosCheckSum = subprocess.Popen(eosCheckSumCommand ,shell=
True, stdout=subprocess.PIPE, universal_newlines=
True)
90 EosCheckSum = exeEosCheckSum.stdout.read()
91 assert exeEosCheckSum.wait() == 0
93 if checkSumList[index]
not in EosCheckSum:
94 print(
'WARNING! The checksum for file ' +
str(realFileName) +
' in EOS\n')
95 print(EosCheckSum +
'\n')
96 print(
'does not match the checksum of the original one\n')
97 print(checkSumList[index] +
'\n')
98 print(
'please try to re-upload file ' +
str(realFileName) +
' to EOS.\n')
100 print(
'Checksum OK for file ' +
str(realFileName))
109 print(
'\n Upload ended at '+time.asctime(time.localtime(time.time())))
113 if __name__ ==
'__main__':
118 usage=
'cmsLHEtoEOSManager.py <options>'
119 parser = optparse.OptionParser(usage)
120 parser.add_option(
'-f',
'--file',
121 help=
'LHE local file list to be uploaded, separated by ","' ,
125 parser.add_option(
'-F',
'--files-from', metavar =
'FILE',
126 help=
'File containing the list of LHE local files be uploaded, one file per line')
128 parser.add_option(
'-n',
'--new',
129 help=
'Create a new article' ,
134 parser.add_option(
'-u',
'--update',
135 help=
'Update the article <Id>' ,
140 parser.add_option(
'-l',
'--list',
141 help=
'List the files in article <Id>' ,
146 parser.add_option(
'-d',
'--dry-run',
147 help=
'dry run, it does nothing, but you can see what it would do',
152 parser.add_option(
'-c',
'--compress',
153 help=
'compress the local .lhe file with xz before upload',
158 parser.add_option(
'--force',
159 help=
'Force update if file already exists.',
164 (options,args) = parser.parse_args()
169 print(
'cmsLHEtoEOSmanager '+__version__[1:-1])
171 print(
'Running on ',time.asctime(time.localtime(time.time())))
174 reallyDoIt =
not options.dryRun
177 if not options.newId
and options.artIdUp==0
and options.artIdLi==0:
178 raise Exception(
'Please specify the action to be taken, either "-n", "-u" or "-l"!')
180 if options.fileList ==
'' and not options.files_from
and (options.newId
or options.artIdUp!=0):
181 raise Exception(
'Please provide the input file list!')
183 if (options.newId
and (options.artIdUp != 0
or options.artIdLi != 0))
or (options.artIdUp != 0
and options.artIdLi != 0):
184 raise Exception(
'Options "-n", "-u" and "-l" are mutually exclusive, please choose only one!')
187 print(
'Action: create new article\n')
188 elif options.artIdUp != 0:
189 print(
'Action: update article '+
str(options.artIdUp)+
'\n')
190 elif options.artIdLi != 0:
191 print(
'Action: list content of article '+
str(options.artIdLi)+
'\n')
193 if options.artIdLi==0:
195 if len(options.fileList) > 0:
196 theList=(options.fileList.split(
','))
198 if options.files_from:
200 f = open(options.files_from)
202 raise Exception(
'Cannot open the file list, \'%s\'' % options.files_from)
205 if len(l) == 0
or l[0] ==
'#':
209 theCompressedFilesList = []
214 if not ( f.lower().endswith(
".lhe")
or f.lower().endswith(
".lhe.xz") ):
215 raise Exception(
'Input file name must have the "lhe" or "lhe.xz" final extension!')
216 if( f.lower().endswith(
".lhe.xz") ):
217 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")
218 print(
"xmllint file.lhe\n")
219 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")
221 if not os.path.exists(f):
222 raise Exception(
'Input file '+f+
' does not exists')
223 if( f.lower().endswith(
".lhe") ):
224 theCheckIntegrityCommand =
'xmllint -noout '+f
225 exeCheckIntegrity = subprocess.Popen([
"/bin/sh",
"-c", theCheckIntegrityCommand])
226 intCode = exeCheckIntegrity.wait()
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 theCompressedFilesList.append(f+
'.xz')
237 if reallyDoIt
and options.compress:
238 theList = theCompressedFilesList
241 exeCheckSum = subprocess.Popen([
"/afs/cern.ch/cms/caf/bin/cms_adler32",f], stdout=subprocess.PIPE, universal_newlines=
True)
242 getCheckSum = subprocess.Popen([
"awk",
"{print $1}"], stdin=exeCheckSum.stdout, stdout=subprocess.PIPE, universal_newlines=
True)
243 exeCheckSum.stdout.close()
244 output,err = getCheckSum.communicate()
245 theCheckSumList.append(output.strip())
247 theCheckSumList.append(
"missing-adler32")
257 print(
'Creating new article with identifier '+
str(newArt)+
' ...\n')
258 uploadPath = defaultEOSRootPath+
'/'+
str(newArt)
259 theCommand = defaultEOSmkdirCommand+
' '+uploadPath
261 exeUpload = subprocess.Popen([
"/bin/sh",
"-c",theCommand])
262 exeUpload.communicate()
266 elif options.artIdUp != 0:
267 newArt = options.artIdUp
269 uploadPath = defaultEOSRootPath+
'/'+
str(newArt)
271 raise Exception(
'Article '+
str(newArt)+
' to be updated does not exist!')
275 elif options.artIdLi !=0:
276 listPath = defaultEOSRootPath+
'/'+
str(options.artIdLi)
277 theCommand = defaultEOSlistCommand+
' '+listPath
278 exeList = subprocess.Popen([
"/bin/sh",
"-c",theCommand], stdout=subprocess.PIPE, universal_newlines=
True)
279 for line
in exeList.stdout.readlines():
285 fileUpload(uploadPath,theList, theCheckSumList, reallyDoIt, options.force)
286 listPath = defaultEOSRootPath+
'/'+
str(newArt)
288 print(
'Listing the '+
str(newArt)+
' article content after upload:')
289 theCommand = defaultEOSlistCommand+
' '+listPath
291 exeFullList = subprocess.Popen([
"/bin/sh",
"-c",theCommand])
292 exeFullList.communicate()
294 print(
'Dry run, nothing was done')