Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
#!/bin/sh

""":"

python_cmd="python"
python3 -c "from FWCore.PythonFramework.CmsRun import CmsRun" 2>/dev/null && python_cmd="python3"
exec ${python_cmd} $0 ${1+"$@"}

"""

print('Starting cmsLHEtoEOSManager.py')

__version__ = "$Revision: 1.13 $"

import os
import subprocess
import time
import re

defaultEOSRootPath = '/eos/cms/store/lhe'
if "CMSEOS_LHE_ROOT_DIRECTORY" in os.environ:
  defaultEOSRootPath = os.environ["CMSEOS_LHE_ROOT_DIRECTORY"]
defaultEOSLoadPath = 'root://eoscms.cern.ch/'
defaultEOSlistCommand = 'xrdfs '+defaultEOSLoadPath+' ls '
defaultEOSmkdirCommand = 'xrdfs '+defaultEOSLoadPath+' mkdir '
defaultEOSfeCommand = 'xrdfs '+defaultEOSLoadPath+' stat -q IsReadable '
defaultEOSchecksumCommand = 'xrdfs '+defaultEOSLoadPath+' query checksum '
defaultEOScpCommand = 'xrdcp -np '

def findXrdDir(theDirRecord):

    elements = theDirRecord.split(' ')
    if len(elements):
        return elements[-1].rstrip('\n').split('/')[-1]
    else:
        return None

def articleExist(artId):

    itExists = False
    theCommand = defaultEOSlistCommand+' '+defaultEOSRootPath
    dirList = subprocess.Popen(["/bin/sh","-c",theCommand], stdout=subprocess.PIPE, universal_newlines=True)
    for line in dirList.stdout.readlines():
        if findXrdDir(line) == str(artId): 
            itExists = True

    return itExists

def lastArticle():

    artList = [0]

    theCommand = defaultEOSlistCommand+' '+defaultEOSRootPath
    dirList = subprocess.Popen(["/bin/sh","-c",theCommand], stdout=subprocess.PIPE, universal_newlines=True)
    for line in dirList.stdout.readlines():
        try:
            if line.rstrip('\n') != '':
                artList.append(int(findXrdDir(line)))
        except:
            break

    return max(artList)


def fileUpload(uploadPath,lheList, checkSumList, reallyDoIt, force=False):

    inUploadScript = ''
    index = 0
    for f in lheList:
        realFileName = f.split('/')[-1]
        # Check the file existence
        newFileName = uploadPath+'/'+str(realFileName)
        addFile = True
        additionalOption = ''  
        theCommand = defaultEOSfeCommand+' '+newFileName
        exeFullList = subprocess.Popen(["/bin/sh","-c",theCommand], stdout=subprocess.PIPE, universal_newlines=True)
        result = exeFullList.stdout.readlines()
        if [line for line in result if ("flags:" in line.lower()) and ("isreadable" in line.lower())] and (not force):
            addFile = False
            print('File '+newFileName+' already exists: do you want to overwrite? [y/n]')
            reply = raw_input()
            if reply == 'y' or reply == 'Y':
                addFile = True
                additionalOption = ' -f '
                print('')
                print('Overwriting file '+newFileName+'\n')
        # add the file
        if addFile:
#            print 'Adding file '+str(f)+'\n'
            inUploadScript = defaultEOScpCommand + additionalOption + ' ' + str(f) + ' ' + defaultEOSLoadPath+uploadPath + '/' + str(realFileName)
            print('Uploading file %s...' % str(f))
            if reallyDoIt:
                exeRealUpload = subprocess.Popen(["/bin/sh","-c",inUploadScript])
                exeRealUpload.communicate()
                eosCheckSumCommand = defaultEOSchecksumCommand + uploadPath + '/' + str(realFileName) + ' | awk \'{print $2}\' | cut -d= -f2'
                exeEosCheckSum = subprocess.Popen(eosCheckSumCommand ,shell=True, stdout=subprocess.PIPE, universal_newlines=True)
                EosCheckSum = exeEosCheckSum.stdout.read()
                assert exeEosCheckSum.wait() == 0
               # print 'checksum: eos = ' + EosCheckSum + 'orig file = ' + checkSumList[index] + '\n'
                if checkSumList[index] not in EosCheckSum:
                    print('WARNING! The checksum for file ' + str(realFileName) + ' in EOS\n')
                    print(EosCheckSum + '\n')
                    print('does not match the checksum of the original one\n')
                    print(checkSumList[index] + '\n')
                    print('please try to re-upload file ' + str(realFileName) + ' to EOS.\n')
                else:
                    print('Checksum OK for file ' + str(realFileName))
        index = index+1
 
# launch the upload shell script        

#    print '\n Launching upload script \n'+inUploadScript+'\n at '+time.asctime(time.localtime(time.time()))+' ...\n'
#    if reallyDoIt:  
#      exeRealUpload = subprocess.Popen(["/bin/sh","-c",inUploadScript])
#      exeRealUpload.communicate()
    print('\n Upload ended at '+time.asctime(time.localtime(time.time())))

#################################################################################################    
        
if __name__ == '__main__':
    
    import optparse
    
    # Here we define an option parser to handle commandline options..
    usage='cmsLHEtoEOSManager.py <options>'
    parser = optparse.OptionParser(usage)
    parser.add_option('-f', '--file',
                      help='LHE local file list to be uploaded, separated by ","' ,
                      default='',
                      dest='fileList')

    parser.add_option('-F', '--files-from', metavar = 'FILE',
                      help='File containing the list of LHE local files be uploaded, one file per line')

    parser.add_option('-n', '--new', 
                      help='Create a new article' ,
                      action='store_true',
                      default=False,
                      dest='newId')                      

    parser.add_option('-u', '--update', 
                      help='Update the article <Id>' ,
                      default=0,
                      type=int,
                      dest='artIdUp')                      

    parser.add_option('-l', '--list', 
                      help='List the files in article <Id>' ,
                      default=0,
                      type=int,
                      dest='artIdLi')                     
    
    parser.add_option('-d', '--dry-run',
                      help='dry run, it does nothing, but you can see what it would do',
                      action='store_true',
                      default=False,
                      dest='dryRun')
    
    parser.add_option('-c', '--compress',
                      help='compress the local .lhe file with xz before upload',
                      action='store_true',
                      default=False,
                      dest='compress')

    parser.add_option('--force',
                      help='Force update if file already exists.',
                      action='store_true',
                      default=False,
                      dest='force')

    (options,args) = parser.parse_args()

    # print banner

    print('')
    print('cmsLHEtoEOSmanager '+__version__[1:-1])
    print('')
    print('Running on ',time.asctime(time.localtime(time.time())))
    print('')
    
    reallyDoIt = not options.dryRun

    # Now some fault control. If an error is found we raise an exception
    if not options.newId and options.artIdUp==0 and options.artIdLi==0:
        raise Exception('Please specify the action to be taken, either "-n", "-u" or "-l"!')
    
    if options.fileList == '' and not options.files_from and (options.newId or options.artIdUp!=0):
        raise Exception('Please provide the input file list!')

    if (options.newId and (options.artIdUp != 0 or options.artIdLi != 0)) or (options.artIdUp != 0 and options.artIdLi != 0):
        raise Exception('Options "-n", "-u" and "-l" are mutually exclusive, please choose only one!')

    if options.newId:
        print('Action: create new article\n')
    elif options.artIdUp != 0:
        print('Action: update article '+str(options.artIdUp)+'\n')
    elif options.artIdLi != 0:
        print('Action: list content of article '+str(options.artIdLi)+'\n')

    if options.artIdLi==0:
        theList = []
        if len(options.fileList) > 0:
            theList=(options.fileList.split(','))
            
        if options.files_from:
            try:
                f = open(options.files_from)
            except IOError:
                raise Exception('Cannot open the file list, \'%s\'' % options.files_from)
            for l in f:
                l = l.strip()
                if len(l) == 0 or l[0] == '#':
                    continue
                theList.append(l)

        theCompressedFilesList = []
        theCheckSumList = []
        for f in theList: 
            # Check the file name extension
            print(f)
            if not ( f.lower().endswith(".lhe") or f.lower().endswith(".lhe.xz") ):
                raise Exception('Input file name must have the "lhe" or "lhe.xz" final extension!')
            if( f.lower().endswith(".lhe.xz") ):
                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")
                print("xmllint file.lhe\n")
                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")
            # Check the local file existence
            if not os.path.exists(f):
                raise Exception('Input file '+f+' does not exists')
            if( f.lower().endswith(".lhe") ):
                theCheckIntegrityCommand = 'xmllint -noout '+f
                exeCheckIntegrity = subprocess.Popen(["/bin/sh","-c", theCheckIntegrityCommand])
                intCode = exeCheckIntegrity.wait()
                if(intCode != 0):
                    raise Exception('Input file '+f+ ' is corrupted')
            if reallyDoIt and options.compress:
              print("Compressing file",f)
              if( f.lower().endswith(".lhe.xz") ):
                  raise Exception('Input file '+f+' is already compressed! This is inconsistent with the --compress option!')
              theCompressionCommand = 'xz '+f
              exeCompression = subprocess.Popen(["/bin/sh","-c",theCompressionCommand])
              exeCompression.communicate()
              theCompressedFilesList.append(f+'.xz')
        if reallyDoIt and options.compress:
          theList = theCompressedFilesList
        for f in theList:
            try:
                exeCheckSum = subprocess.Popen(["/afs/cern.ch/cms/caf/bin/cms_adler32",f], stdout=subprocess.PIPE, universal_newlines=True)
                getCheckSum = subprocess.Popen(["awk", "{print $1}"], stdin=exeCheckSum.stdout, stdout=subprocess.PIPE, universal_newlines=True)
                exeCheckSum.stdout.close()
                output,err = getCheckSum.communicate()
                theCheckSumList.append(output.strip())
            except:
                theCheckSumList.append("missing-adler32")

    newArt = 0
    uploadPath = ''

# new article

    if options.newId:
        oldArt = lastArticle()
        newArt = oldArt+1
        print('Creating new article with identifier '+str(newArt)+' ...\n')
        uploadPath = defaultEOSRootPath+'/'+str(newArt)
        theCommand = defaultEOSmkdirCommand+' '+uploadPath
        if reallyDoIt:
          exeUpload = subprocess.Popen(["/bin/sh","-c",theCommand])
          exeUpload.communicate()

# update article
        
    elif options.artIdUp != 0:
        newArt = options.artIdUp
        if articleExist(newArt):
            uploadPath = defaultEOSRootPath+'/'+str(newArt)
        else:
            raise Exception('Article '+str(newArt)+' to be updated does not exist!')

# list article
        
    elif options.artIdLi !=0:
        listPath = defaultEOSRootPath+'/'+str(options.artIdLi)
        theCommand = defaultEOSlistCommand+' '+listPath
        exeList = subprocess.Popen(["/bin/sh","-c",theCommand], stdout=subprocess.PIPE, universal_newlines=True)
        for line in exeList.stdout.readlines():
            if findXrdDir(line) != None:
                print(findXrdDir(line))


    if newArt > 0:
        fileUpload(uploadPath,theList, theCheckSumList, reallyDoIt, options.force)
        listPath = defaultEOSRootPath+'/'+str(newArt)
        print('')
        print('Listing the '+str(newArt)+' article content after upload:')
        theCommand = defaultEOSlistCommand+' '+listPath
        if reallyDoIt:
          exeFullList = subprocess.Popen(["/bin/sh","-c",theCommand])
          exeFullList.communicate()
        else:
          print('Dry run, nothing was done')