3 from __future__
import print_function
4 print(
'Starting cmsLHEtoEOSManager.py')
6 __version__ =
"$Revision: 1.13 $" 13 defaultEOSRootPath =
'/eos/cms/store/lhe' 14 defaultEOSLoadPath =
'root://eoscms/' 15 defaultEOSlistCommand =
'xrd eoscms dirlist ' 16 defaultEOSmkdirCommand =
'xrd eoscms mkdir ' 17 defaultEOSfeCommand =
'xrd eoscms existfile ' 18 defaultEOScpCommand =
'xrdcp -np ' 22 elements = theDirRecord.split(
' ')
24 return elements[-1].rstrip(
'\n').
split(
'/')[-1]
31 theCommand = defaultEOSlistCommand+
' '+defaultEOSRootPath
32 dirList = subprocess.Popen([
"/bin/sh",
"-c",theCommand], stdout=subprocess.PIPE, universal_newlines=
True)
33 for line
in dirList.stdout.readlines():
43 theCommand = defaultEOSlistCommand+
' '+defaultEOSRootPath
44 dirList = subprocess.Popen([
"/bin/sh",
"-c",theCommand], stdout=subprocess.PIPE, universal_newlines=
True)
45 for line
in dirList.stdout.readlines():
47 if line.rstrip(
'\n') !=
'':
55 def fileUpload(uploadPath,lheList, checkSumList, reallyDoIt):
60 realFileName = f.split(
'/')[-1]
62 newFileName = uploadPath+
'/'+
str(realFileName)
65 theCommand = defaultEOSfeCommand+
' '+newFileName
66 exeFullList = subprocess.Popen([
"/bin/sh",
"-c",theCommand], stdout=subprocess.PIPE)
67 result = exeFullList.stdout.readlines()
68 if result[0].rstrip(
'\n') ==
'The file exists.':
70 print(
'File '+newFileName+
' already exists: do you want to overwrite? [y/n]')
72 if reply ==
'y' or reply ==
'Y':
74 additionalOption =
' -f ' 76 print(
'Overwriting file '+newFileName+
'\n')
80 inUploadScript = defaultEOScpCommand + additionalOption +
' ' +
str(f) +
' ' + defaultEOSLoadPath+uploadPath +
'/' +
str(realFileName)
81 print(
'Uploading file %s...' %
str(f))
83 exeRealUpload = subprocess.Popen([
"/bin/sh",
"-c",inUploadScript])
84 exeRealUpload.communicate()
85 eosCheckSumCommand =
'/afs/cern.ch/project/eos/installation/0.3.15/bin/eos.select find --checksum ' + uploadPath +
'/' +
str(realFileName) +
' | awk \'{print $2}\' | cut -d= -f2' 86 exeEosCheckSum = subprocess.Popen(eosCheckSumCommand ,shell=
True, stdout=subprocess.PIPE)
87 EosCheckSum = exeEosCheckSum.stdout.read()
88 assert exeEosCheckSum.wait() == 0
90 if checkSumList[index]
not in EosCheckSum:
91 print(
'WARNING! The checksum for file ' +
str(realFileName) +
' in EOS\n')
92 print(EosCheckSum +
'\n')
93 print(
'does not match the checksum of the original one\n')
94 print(checkSumList[index] +
'\n')
95 print(
'please try to re-upload file ' +
str(realFileName) +
' to EOS.\n')
97 print(
'Checksum OK for file ' +
str(realFileName))
106 print(
'\n Upload ended at '+time.asctime(time.localtime(time.time())))
110 if __name__ ==
'__main__':
115 usage=
'cmsLHEtoEOSManager.py <options>' 116 parser = optparse.OptionParser(usage)
117 parser.add_option(
'-f',
'--file',
118 help=
'LHE local file list to be uploaded, separated by ","' ,
122 parser.add_option(
'-F',
'--files-from', metavar =
'FILE',
123 help=
'File containing the list of LHE local files be uploaded, one file per line')
125 parser.add_option(
'-n',
'--new',
126 help=
'Create a new article' ,
131 parser.add_option(
'-u',
'--update',
132 help=
'Update the article <Id>' ,
136 parser.add_option(
'-l',
'--list',
137 help=
'List the files in article <Id>' ,
141 parser.add_option(
'-d',
'--dry-run',
142 help=
'dry run, it does nothing, but you can see what it would do',
147 parser.add_option(
'-c',
'--compress',
148 help=
'compress the local .lhe file with xz before upload',
153 (options,args) = parser.parse_args()
158 print(
'cmsLHEtoEOSmanager '+__version__[1:-1])
160 print(
'Running on ',time.asctime(time.localtime(time.time())))
163 reallyDoIt =
not options.dryRun
166 if not options.newId
and options.artIdUp==0
and options.artIdLi==0:
167 raise Exception(
'Please specify the action to be taken, either "-n", "-u" or "-l"!')
169 if options.fileList ==
'' and not options.files_from
and (options.newId
or options.artIdUp!=0):
170 raise Exception(
'Please provide the input file list!')
172 if (options.newId
and (options.artIdUp != 0
or options.artIdLi != 0))
or (options.artIdUp != 0
and options.artIdLi != 0):
173 raise Exception(
'Options "-n", "-u" and "-l" are mutually exclusive, please choose only one!')
176 print(
'Action: create new article\n')
177 elif options.artIdUp != 0:
178 print(
'Action: update article '+
str(options.artIdUp)+
'\n')
179 elif options.artIdLi != 0:
180 print(
'Action: list content of article '+
str(options.artIdLi)+
'\n')
182 if options.artIdLi==0:
184 if len(options.fileList) > 0:
185 theList=(options.fileList.split(
','))
187 if options.files_from:
189 f = open(options.files_from)
191 raise Exception(
'Cannot open the file list, \'%s\'' % options.files_from)
194 if len(l) == 0
or l[0] ==
'#':
198 theCompressedFilesList = []
203 if not ( f.lower().endswith(
".lhe")
or f.lower().endswith(
".lhe.xz") ):
204 raise Exception(
'Input file name must have the "lhe" or "lhe.xz" final extension!')
205 if( f.lower().endswith(
".lhe.xz") ):
206 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")
207 print(
"xmllint file.lhe\n")
208 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")
210 if not os.path.exists(f):
211 raise Exception(
'Input file '+f+
' does not exists')
212 if( f.lower().endswith(
".lhe") ):
213 theCheckIntegrityCommand =
'xmllint -noout '+f
214 exeCheckIntegrity = subprocess.Popen([
"/bin/sh",
"-c", theCheckIntegrityCommand])
215 intCode = exeCheckIntegrity.wait()
216 if(intCode
is not 0):
217 raise Exception(
'Input file '+f+
' is corrupted')
218 if reallyDoIt
and options.compress:
219 print(
"Compressing file",f)
220 if( f.lower().endswith(
".lhe.xz") ):
221 raise Exception(
'Input file '+f+
' is already compressed! This is inconsistent with the --compress option!')
222 theCompressionCommand =
'xz '+f
223 exeCompression = subprocess.Popen([
"/bin/sh",
"-c",theCompressionCommand])
224 exeCompression.communicate()
225 theCompressedFilesList.append(f+
'.xz')
226 if reallyDoIt
and options.compress:
227 theList = theCompressedFilesList
229 exeCheckSum = subprocess.Popen([
"/afs/cern.ch/cms/caf/bin/cms_adler32",f], stdout=subprocess.PIPE)
230 getCheckSum = subprocess.Popen([
"awk",
"{print $1}"], stdin=exeCheckSum.stdout, stdout=subprocess.PIPE)
231 exeCheckSum.stdout.close()
232 output,err = getCheckSum.communicate()
233 theCheckSumList.append(output)
243 print(
'Creating new article with identifier '+
str(newArt)+
' ...\n')
244 uploadPath = defaultEOSRootPath+
'/'+
str(newArt)
245 theCommand = defaultEOSmkdirCommand+
' '+uploadPath
247 exeUpload = subprocess.Popen([
"/bin/sh",
"-c",theCommand])
248 exeUpload.communicate()
252 elif options.artIdUp != 0:
253 newArt = options.artIdUp
255 uploadPath = defaultEOSRootPath+
'/'+
str(newArt)
257 raise Exception(
'Article '+
str(newArt)+
' to be updated does not exist!')
261 elif options.artIdLi !=0:
262 listPath = defaultEOSRootPath+
'/'+
str(options.artIdLi)
263 theCommand = defaultEOSlistCommand+
' '+listPath
264 exeList = subprocess.Popen([
"/bin/sh",
"-c",theCommand], stdout=subprocess.PIPE, universal_newlines=
True)
265 for line
in exeList.stdout.readlines():
271 fileUpload(uploadPath,theList, theCheckSumList, reallyDoIt)
272 listPath = defaultEOSRootPath+
'/'+
str(newArt)
274 print(
'Listing the '+
str(newArt)+
' article content after upload:')
275 theCommand = defaultEOSlistCommand+
' '+listPath
277 exeFullList = subprocess.Popen([
"/bin/sh",
"-c",theCommand])
278 exeFullList.communicate()
280 print(
'Dry run, nothing was done')
std::vector< std::string_view > split(std::string_view, const char *)
S & print(S &os, JobReport::InputFile const &f)
def findXrdDir(theDirRecord)
def fileUpload(uploadPath, lheList, checkSumList, reallyDoIt)