예제 #1
0
def count_entry(root_dir):
    dir = lfc.lfc_opendirg(root_dir, "")

    counter = {"dir":0, "file":0}

    if (dir == None) or (dir == 0):
        print_error("Error while looking for " + root_dir)
        return counter

    while True:
        read_pt = lfc.lfc_readdirxr(dir,"")
        
        if (read_pt == None) or (read_pt == 0):
            break
        entry, list = read_pt
        p = root_dir + '/' + entry.d_name
        if stat.S_ISDIR(entry.filemode):
            counter['dir'] += 1
            cnt = count_entry(p)
            #print cnt
            counter['dir'] += cnt['dir']
            counter['file'] += cnt['file']
        else:
            counter['file'] += 1

    lfc.lfc_closedir(dir)
    return counter
예제 #2
0
def readdirxr_recurse(*args):
  
  if len(args) < 1:
    folder = ''
    prefix = ''
  else:
    folder = args[0]
    prefix = folder + '/'
  
  if (folder == '') or (folder[0] != '/'):
    if 'LFC_HOME' in os.environ:
      folder = os.environ['LFC_HOME'] + '/' + prefix
    else:
      sys.exit('Relative folder path requires LFC_HOME to be set and exported')
  
  #---------------------------------------------------------------------------
  # Open the folder
  #---------------------------------------------------------------------------
  dir = lfc.lfc_opendirg(folder, '')
  if dir == None:
    err_num    = lfc.cvar.serrno
    err_string = lfc.sstrerror(err_num)
    sys.exit('Error ' + str(err_num) + ' on folder ' + folder + ': ' + err_string)
  
  #---------------------------------------------------------------------------
  # Loop on the entries of the folder
  #---------------------------------------------------------------------------
  folders = []
  
  while 1:
    read_pt = lfc.lfc_readdirxr(dir, '')
    if read_pt == None:
      break
    entry, replicas = read_pt
    name = prefix + entry.d_name
    if   entry.filemode & 040000:
      folders.append(name)
      comment = '   (Folder)'
    elif entry.nbreplicas == 0:
      comment = '   (No replica)'
    else:
      comment = ''
    print name + comment
    #-------------------------------------------------------------------------
    # If the entry is a regular file, list its replicas
    #-------------------------------------------------------------------------
    if comment == '':
      for replica in replicas:
         print '    ==>', replica.sfn
  
  lfc.lfc_closedir(dir)
  
  #---------------------------------------------------------------------------
  # Recurse on subfolders
  #---------------------------------------------------------------------------
  for name in folders:
    readdirxr_recurse(name)
예제 #3
0
def readdirxr(folderFiles):
  
  folderKeys = folderFiles.keys()
  folderKeys.sort()
  #print folderKeys
  
  #---------------------------------------------------------------------------
  # Loop on the folders
  #---------------------------------------------------------------------------
  for folder in folderKeys:
    prefix = folder
    #print '\n' + prefix + ':', folderFiles[prefix]
    
    if (folder == '') or (folder[0] != '/'):
      if 'LFC_HOME' in os.environ:
        folder = os.environ['LFC_HOME'] + '/' + folder
      else:
        sys.exit('Relative folder path requires LFC_HOME to be set and exported')
  
    #-------------------------------------------------------------------------
    # Open the folder
    #-------------------------------------------------------------------------
    dir = lfc.lfc_opendirg(folder, '')
    if dir == None:
      err_num = lfc.cvar.serrno
      err_string = lfc.sstrerror(err_num)
      sys.exit('Error ' + str(err_num) + ' on folder ' + folder + ': ' + err_string)
    
    #-------------------------------------------------------------------------
    # Loop on the entries of the folder which are in the dictionary
    #-------------------------------------------------------------------------
    while 1:
      read_pt = lfc.lfc_readdirxr(dir, '')
      if read_pt == None:
        break
      
      entry, replicas = read_pt
      if entry.d_name not in folderFiles[prefix]:
        break
      
      if   entry.filemode & 040000:
        comment = '   (Folder)'
      elif entry.nbreplicas == 0:
        comment = '   (No replica)'
      else:
        comment = ''
      print prefix + entry.d_name
      #-----------------------------------------------------------------------
      # If the entry is a regular file, list its replicas
      #-----------------------------------------------------------------------
      if comment == '':
        for replica in replicas:
           print '    ==>', replica.sfn
    
    lfc.lfc_closedir(dir)
예제 #4
0
def readdirg_recurse(*args):
  
  if len(args) < 1:
    folder = ''
    prefix = ''
  else:
    folder = args[0]
    prefix = folder + '/'
  
  if (folder == '') or (folder[0] != '/'):
    if 'LFC_HOME' in os.environ:
      folder = os.environ['LFC_HOME'] + '/' + prefix
    else:
      sys.exit('Relative folder path requires LFC_HOME to be set and exported')
  
  #---------------------------------------------------------------------------
  # Open the folder
  #---------------------------------------------------------------------------
  dir = lfc.lfc_opendirg(folder, '')
  if dir == None:
    err_num    = lfc.cvar.serrno
    err_string = lfc.sstrerror(err_num)
    sys.exit('Error ' + str(err_num) + ' on folder ' + folder + ': ' + err_string)
  
  listp = lfc.lfc_list()
  folderFiles = {prefix: []}
  
  #---------------------------------------------------------------------------
  # Append the entries of the folder to the current list
  #---------------------------------------------------------------------------
  files = []
  
  while 1:
    entry = lfc.lfc_readdirg(dir)
    if entry == None:
      break
    files.append({'name': entry.d_name,
                  'mode': entry.filemode})
  
  lfc.lfc_closedir(dir)
  
  #---------------------------------------------------------------------------
  # Loop on the entries of the current file list to build the dictionary
  #---------------------------------------------------------------------------
  for myfile in files:
    folderFiles[prefix].append(myfile['name'])
    
    #-------------------------------------------------------------------------
    # If the entry is a folder, recurse
    #-------------------------------------------------------------------------
    if myfile['mode'] & 040000:
      folderFiles.update(readdirg_recurse(prefix + myfile['name']))
  
  return folderFiles
예제 #5
0
def readdirg_recurse(*args):
  
  if len(args) < 1:
    folder = ''
    prefix = ''
  else:
    folder = args[0]
    prefix = folder + '/'
  
  if (folder == '') or (folder[0] != '/'):
    if 'LFC_HOME' in os.environ:
      folder = os.environ['LFC_HOME'] + '/' + prefix
    else:
      sys.exit('Relative folder path requires LFC_HOME to be set and exported')
  
  #---------------------------------------------------------------------------
  # Open the folder
  #---------------------------------------------------------------------------
  dir = lfc.lfc_opendirg(folder, '')
  if dir == None:
    err_num    = lfc.cvar.serrno
    err_string = lfc.sstrerror(err_num)
    sys.exit('Error ' + str(err_num) + ' on folder ' + folder + ': ' + err_string)
  
  files = []
  listp = lfc.lfc_list()
  
  #---------------------------------------------------------------------------
  # Loop on the entries of the folder to build the list of files
  #---------------------------------------------------------------------------
  while 1:
    entry = lfc.lfc_readdirg(dir)
    if entry == None:
      break
    files.append({'name': prefix + entry.d_name,
                  'mode': entry.filemode,
                  'guid': entry.guid})
  
  lfc.lfc_closedir(dir)
  
  #---------------------------------------------------------------------------
  # Loop on the current file list :  If the entry is a folder, recurse
  #---------------------------------------------------------------------------
  for myfile in files[:]:                            # Local copy is mandatory
    if myfile['mode'] & 040000:
      files.extend(readdirg_recurse(myfile['name']))
  
  return files
예제 #6
0
def readdirg(*args):
  
  if len(args) < 1:
    folder = ''
  else:
    folder = args[0]
  
  if (folder == '') or (folder[0] != '/'):
    if 'LFC_HOME' in os.environ:
      folder = os.environ['LFC_HOME'] + '/' + folder
    else:
      sys.exit('Relative folder path requires LFC_HOME to be set and exported')
  
  #---------------------------------------------------------------------------
  # Open the folder
  #---------------------------------------------------------------------------
  dir = lfc.lfc_opendirg(folder, '')
  if dir == None:
    err_num    = lfc.cvar.serrno
    err_string = lfc.sstrerror(err_num)
    sys.exit('Error ' + str(err_num) + ' on folder ' + folder + ': ' + err_string)
  
  #---------------------------------------------------------------------------
  # Loop on the entries of the folder
  #---------------------------------------------------------------------------
  while 1:
    entry = lfc.lfc_readdirg(dir)
    if entry == None:
      break
    if   entry.filemode & 040000:
      marker = '/'
    elif entry.filemode & 020000:     # not entry.guid:
      marker = '@'
    else:
      marker = ''
    print ('%06o' % entry.filemode) + '  ' + entry.d_name + marker
  
  lfc.lfc_closedir(dir)
예제 #7
0
  if entry == None:
    break
  if (entry.filemode & 040000):
    print '  ', entry.d_name, 'is a folder'
    b_OK = False
  elif entry.guid:
    print '  ', entry.d_name, 'is a regular file'
    b_OK = False
  if b_OK:
    symlinks.insert(0, entry.d_name)

#-----------------------------------------------------------------------------
#  Close the folder.
#  If at least one file found is not a symbolic link, stop.
#-----------------------------------------------------------------------------
lfc.lfc_closedir(dir)

if not b_OK:
  sys.exit(1)

print folder, ': ', len(symlinks), 'symbolic links found'

if len(symlinks) > 0:
  #---------------------------------------------------------------------------
  #  Here all files inside the folder are symbolic links.
  #  Open an LFC session.
  #---------------------------------------------------------------------------
  res = lfc.lfc_startsess('', '')
  if res != 0:
    err_num = lfc.cvar.serrno
    err_string = lfc.sstrerror(err_num)
예제 #8
0
파일: lfc_del_dir.py 프로젝트: dmwm/DLSAPI
def deleteOneDir(pDir, pForce, pExtLinks, pExtLfns, pVerbose):
    """
    Tries to delete the specified directory. If "pForce" is true, then the
    directory will be removed even if it is not empty and all the entries in
    the directory (including subdirectories) will be removed as well, in a
    recursive way. Otherwise, the deletion will be performed only if the
    directory is empty.

    If pExtLinks == pExtLfns == False, the function will remove only the
    specified directory contents and will not to  touch external sym links
    pointing to LFNs in the directory, nor entries pointed to by links located
    in the directory.

    If pExtLinks == True, then links located in other directories that point
    to LFNs in the specified directory are also removed.

    If pExtLfns == True, then LFNs (and their replicas and all their sym links)
    located in other directories and pointed to by links in the specified
    directory are also removed.
   """

    dir = pDir
    force = pForce
    extLinks = pExtLinks
    extLfns = pExtLfns
    verbose = pVerbose

    rc = 0
    err = 0

    subdirlist = [
    ]  # Subdirectories to remove when we are done with current one

    [dir, parentdir] = lfc_del_lfn.checkHomeDir(dir)
    fulldir = parentdir + dir

    if (verbose):
        print "--Deleting Dir: " + dir

    err = lfc.lfc_chdir(fulldir)
    if (err < 0):
        sys.stderr.write("Error changing dir to: " + fulldir + " : " +
                         lfc.sstrerror(lfc.cvar.serrno) + "\n")
        return err

    dir_p = lfc.lfc_DIR()
    dir_entry = lfc.lfc_direnstatg()
    dir_p = lfc.lfc_opendirg(".", "")
    if (dir_p < 0):
        sys.stderr.write("Error opening specified dir: " + dir + ": " +
                         lfc.sstrerror(lfc.cvar.serrno) + "\n")
        return -1

    # Read first entry
    lfc.lfc_rewinddir(dir_p)
    dir_entry = lfc.lfc_readdirg(dir_p)

    if (force):
        # Remove all LFNs in a loop
        S_IFDIR = 0x4000
        S_IFLNK = 0xA000

        while (dir_entry):
            lfn = dir_entry.d_name
            if (dir_entry.filemode & S_IFDIR):
                # This entry is a directory
                subdirlist.append(lfn)
            else:
                if ((dir_entry.filemode & S_IFLNK) == S_IFLNK):
                    # This entry is a sym link
                    if (extLfns):
                        # Remove the replicas and all the links (including main LFN)
                        err = lfc_del_lfn.deleteOneEntry(
                            lfn, fulldir, False, True, verbose)
                    else:
                        # Remove only the sym link (no replicas)
                        err = lfc_del_lfn.deleteOneLFN(lfn, verbose)
                else:
                    # This entry is a main LFN
                    #              # First check if the file has been alredy deleted (links, etc...)
                    #              fstat = lfc.lfc_filestatg()
                    #              if(lfc.lfc_statg(lfn, "", fstat)<0):
                    #                if(verbose):
                    #                   print "--Warning. Skipping deletion of non-accessible file:",lfn,":",\
                    #                          lfc.sstrerror(lfc.cvar.serrno)
                    #              else:
                    if (extLinks):
                        # Remove the replicas and all the links that point to this LFN
                        err = lfc_del_lfn.deleteOneEntry(
                            lfn, fulldir, False, True, verbose)
                    else:
                        # Remove only this LFN and replicas (but no external sym links)
                        err = lfc_del_lfn.deleteOneEntry(
                            lfn, fulldir, False, False, verbose)

            if (err): rc = err
            dir_entry = lfc.lfc_readdirg(dir_p)

    else:
        if (dir_entry):
            sys.stderr.write("Error: Directory " + dir +
                             " not empty! Consider using -r.\n")
            return -1

    # Close the directory
    if (lfc.lfc_closedir(dir_p) < 0):
        sys.stderr.write("Error closing dir: " + dir + " : " +
                         lfc.sstrerror(lfc.cvar.serrno) + "\n")

    # Remove all subdirectories in the list
    for subdir in subdirlist:
        err = deleteOneDir(fulldir + '/' + subdir, force, extLinks, extLfns,
                           verbose)
        if (err): rc = err

    # Finally, remove also the top directory itself
    err = lfc.lfc_chdir("..")
    if (err < 0):
        sys.stderr.write("Error changing dir to \"..\" : " +
                         lfc.sstrerror(lfc.cvar.serrno) + "\n")
        return err

    if (verbose):
        print "--lfc.lfc_unlink(\"" + dir + "\")"
    err = lfc.lfc_rmdir(dir)
    if (err < 0):
        sys.stderr.write("Error removing dir: " + dir + ": " +
                         lfc.sstrerror(lfc.cvar.serrno) + "\n")
        return err

# Return the error code


#   return rc
    return err
예제 #9
0
def readdirg_lr(*args):
  
  if len(args) < 1:
    folder = ''
  else:
    folder = args[0]
  
  if (folder == '') or (folder[0] != '/'):
    if 'LFC_HOME' in os.environ:
      folder = os.environ['LFC_HOME'] + '/' + folder
    else:
      sys.exit('Relative folder path requires LFC_HOME to be set and exported')
  
  #---------------------------------------------------------------------------
  # Open the folder
  #---------------------------------------------------------------------------
  dir = lfc.lfc_opendirg(folder, '')
  if dir == None:
    err_num    = lfc.cvar.serrno
    err_string = lfc.sstrerror(err_num)
    sys.exit('Error ' + str(err_num) + ' on folder ' + folder + ': ' + err_string)
  
  listp = lfc.lfc_list()
  
  #---------------------------------------------------------------------------
  # Loop on the entries of the folder
  #---------------------------------------------------------------------------
  while 1:
    entry = lfc.lfc_readdirg(dir)
    if entry == None:
      break
    if   entry.filemode & 040000:
      marker = '/'
    elif entry.filemode & 020000:     # not entry.guid:
      marker = '@'
    else:
      marker = ''
    print ('%06o' % entry.filemode) + '  ' + entry.d_name + marker
    
    #-------------------------------------------------------------------------
    # If the entry is a regular file, list its replicas using its GUID
    #-------------------------------------------------------------------------
    if not (entry.filemode & 060000):     # Exclude folders and symbolic links
      flag  = lfc.CNS_LIST_BEGIN
      num_replicas = 0
      
      while 1:
        res = lfc.lfc_listreplica('', entry.guid, flag, listp)
        if res == None:
          break
        else:
          flag = lfc.CNS_LIST_CONTINUE
          print '   ==>', res.sfn
          num_replicas += 1
      
      lfc.lfc_listreplica('', entry.guid, lfc.CNS_LIST_END, listp)
      print 'Found ' + str(num_replicas) + ' replica(s)'
    
    print
  
  lfc.lfc_closedir(dir)
예제 #10
0
def readdirg_lr_recurse_timestamp(*args):
  
  if len(args) < 1:
    folder = ''
    prefix = ''
  else:
    folder = args[0]
    prefix = folder + '/'
  
  if (folder == '') or (folder[0] != '/'):
    if 'LFC_HOME' in os.environ:
      folder = os.environ['LFC_HOME'] + '/' + prefix
    else:
      sys.exit('Relative folder path requires LFC_HOME to be set and exported')
  
  time_format = '%Y/%m/%d %H:%M:%S %Z'
  
  #---------------------------------------------------------------------------
  # Open the folder
  #---------------------------------------------------------------------------
  dir = lfc.lfc_opendirg(folder, '')
  if dir == None:
    err_num    = lfc.cvar.serrno
    err_string = lfc.sstrerror(err_num)
    sys.exit('Error ' + str(err_num) + ' on folder ' + folder + ': ' + err_string)
  
  listp = lfc.lfc_list()
  
  #---------------------------------------------------------------------------
  # Loop on the entries of the folder
  #---------------------------------------------------------------------------
  while 1:
    entry = lfc.lfc_readdirg(dir)
    if entry == None:
      break
    name = prefix + entry.d_name
    sys.stdout.write(time.strftime(time_format) + '  ' + name + "\n")
    sys.stdout.flush()
    
    #-------------------------------------------------------------------------
    # If the entry is a regular file, list its replicas using its GUID
    #-------------------------------------------------------------------------
    if not (entry.filemode & 060000):     # Exclude folders and symbolic links
      flag  = lfc.CNS_LIST_BEGIN
      
      while 1:
        res = lfc.lfc_listreplica('', entry.guid, flag, listp)
        if res == None:
          break
        else:
          flag = lfc.CNS_LIST_CONTINUE
          sys.stdout.write(time.strftime(time_format) + '     ==> ' + res.sfn + "\n")
          sys.stdout.flush()
      
      lfc.lfc_listreplica('', entry.guid, lfc.CNS_LIST_END, listp)
    
    #-------------------------------------------------------------------------
    # If the entry is a folder, recurse
    #-------------------------------------------------------------------------
    if entry.filemode & 040000:
      readdirg_lr_recurse_timestamp(name)
  
  lfc.lfc_closedir(dir)
예제 #11
0
def readdirg_gr(*args):
  
  if len(args) < 1:
    folder = ''
  else:
    folder = args[0]
  
  if (folder == '') or (folder[0] != '/'):
    if 'LFC_HOME' in os.environ:
      folder = os.environ['LFC_HOME'] + '/' + folder
    else:
      sys.exit('Relative folder path requires LFC_HOME to be set and exported')
  
  #---------------------------------------------------------------------------
  # Open the folder
  #---------------------------------------------------------------------------
  dir = lfc.lfc_opendirg(folder, '')
  if dir == None:
    err_num    = lfc.cvar.serrno
    err_string = lfc.sstrerror(err_num)
    sys.exit('Error ' + str(err_num) + ' on folder ' + folder + ': ' + err_string)
  
  files = []
  listp = lfc.lfc_list()
  
  #---------------------------------------------------------------------------
  # Loop on the entries of the folder to build the list of files
  #---------------------------------------------------------------------------
  while 1:
    entry = lfc.lfc_readdirg(dir)
    if entry == None:
      break
    if   entry.filemode & 040000:
      marker = '/'
    elif entry.filemode & 020000:     # not entry.guid:
      marker = '@'
    else:
      marker = ''
    files.append({'name': entry.d_name + marker,
                  'mode': entry.filemode,
                  'guid': entry.guid})
  
  lfc.lfc_closedir(dir)
  
  #---------------------------------------------------------------------------
  # Loop on the list of files
  #---------------------------------------------------------------------------
  lfc.lfc_startsess('', '')
  
  for myfile in files:
    print ('%06o' % myfile['mode']) + '  ' + myfile['name']
    
    #-------------------------------------------------------------------------
    # If the entry is a regular file, list its replicas using its GUID
    #-------------------------------------------------------------------------
    if not (myfile['mode'] & 060000):     # Exclude folders and symbolic links
      (res, replicas) = lfc.lfc_getreplica('', myfile['guid'], '')
      if res == 0:
        for replica in replicas:
          print '   ==>', replica.sfn
        print 'Found ' + str(len(replicas)) + ' replica(s)'
    
    print
  
  lfc.lfc_endsess()
예제 #12
0
def readdirg_lr_recurse(*args):
  
  if len(args) < 1:
    folder = ''
    prefix = ''
  else:
    folder = args[0]
    prefix = folder + '/'
  
  if (folder == '') or (folder[0] != '/'):
    if 'LFC_HOME' in os.environ:
      folder = os.environ['LFC_HOME'] + '/' + prefix
    else:
      sys.exit('Relative folder path requires LFC_HOME to be set and exported')
  
  #---------------------------------------------------------------------------
  # Open the folder
  #---------------------------------------------------------------------------
  dir = lfc.lfc_opendirg(folder, '')
  if dir == None:
    err_num    = lfc.cvar.serrno
    err_string = lfc.sstrerror(err_num)
    sys.exit('Error ' + str(err_num) + ' on folder ' + folder + ': ' + err_string)
  
  listp = lfc.lfc_list()
  
  #---------------------------------------------------------------------------
  # Append the entries of the folder to the current list
  #---------------------------------------------------------------------------
  files = []
  
  while 1:
    entry = lfc.lfc_readdirg(dir)
    if entry == None:
      break
    files.append({'name': prefix + entry.d_name,
                  'mode': entry.filemode,
                  'guid': entry.guid})
  
  lfc.lfc_closedir(dir)
  
  #---------------------------------------------------------------------------
  # Loop on the entries of the current file list
  #---------------------------------------------------------------------------
  for myfile in files:
    print myfile['name']
    
    #-------------------------------------------------------------------------
    # If the entry is a regular file, list its replicas using its GUID
    #-------------------------------------------------------------------------
    if not (myfile['mode'] & 060000):     # Exclude folders and symbolic links
      flag  = lfc.CNS_LIST_BEGIN
      
      while 1:
        res = lfc.lfc_listreplica('', myfile['guid'], flag, listp)
        if res == None:
          break
        else:
          flag = lfc.CNS_LIST_CONTINUE
          print '   ==>', res.sfn
      
      lfc.lfc_listreplica('', myfile['guid'], lfc.CNS_LIST_END, listp)
    
    #-------------------------------------------------------------------------
    # If the entry is a folder, recurse
    #-------------------------------------------------------------------------
    if myfile['mode'] & 040000:
      readdirg_lr_recurse(myfile['name'])
예제 #13
0
파일: lfc_del_dir.py 프로젝트: bbockelm/DLS
def deleteOneDir(pDir, pForce, pExtLinks, pExtLfns, pVerbose):
   """
    Tries to delete the specified directory. If "pForce" is true, then the
    directory will be removed even if it is not empty and all the entries in
    the directory (including subdirectories) will be removed as well, in a
    recursive way. Otherwise, the deletion will be performed only if the
    directory is empty.

    If pExtLinks == pExtLfns == False, the function will remove only the
    specified directory contents and will not to  touch external sym links
    pointing to LFNs in the directory, nor entries pointed to by links located
    in the directory.

    If pExtLinks == True, then links located in other directories that point
    to LFNs in the specified directory are also removed.

    If pExtLfns == True, then LFNs (and their replicas and all their sym links)
    located in other directories and pointed to by links in the specified
    directory are also removed.
   """

   dir = pDir
   force = pForce
   extLinks = pExtLinks 
   extLfns = pExtLfns
   verbose = pVerbose

   rc = 0
   err = 0

   subdirlist = [] # Subdirectories to remove when we are done with current one

   [dir, parentdir] = lfc_del_lfn.checkHomeDir(dir)
   fulldir = parentdir+dir

   if(verbose):
      print "--Deleting Dir: "+dir
      
   err = lfc.lfc_chdir(fulldir)
   if(err < 0):
      sys.stderr.write("Error changing dir to: "+fulldir+" : "+lfc.sstrerror(lfc.cvar.serrno)+"\n")
      return err

   dir_p=lfc.lfc_DIR()
   dir_entry=lfc.lfc_direnstatg()
   dir_p=lfc.lfc_opendirg(".", "")
   if(dir_p < 0):
      sys.stderr.write("Error opening specified dir: "+dir+": "+lfc.sstrerror(lfc.cvar.serrno)+"\n")
      return -1

   # Read first entry
   lfc.lfc_rewinddir(dir_p)
   dir_entry=lfc.lfc_readdirg(dir_p)

   if(force):
      # Remove all LFNs in a loop
      S_IFDIR = 0x4000   
      S_IFLNK = 0xA000  

      while(dir_entry):
         lfn = dir_entry.d_name
         if(dir_entry.filemode & S_IFDIR):
         # This entry is a directory
            subdirlist.append(lfn)
         else:
           if((dir_entry.filemode & S_IFLNK) == S_IFLNK):
           # This entry is a sym link
              if(extLfns):
                 # Remove the replicas and all the links (including main LFN)
                 err = lfc_del_lfn.deleteOneEntry(lfn, fulldir, False, True, verbose)
              else:
                 # Remove only the sym link (no replicas)
                 err = lfc_del_lfn.deleteOneLFN(lfn, verbose)
           else:
           # This entry is a main LFN
#              # First check if the file has been alredy deleted (links, etc...)
#              fstat = lfc.lfc_filestatg()
#              if(lfc.lfc_statg(lfn, "", fstat)<0):
#                if(verbose):
#                   print "--Warning. Skipping deletion of non-accessible file:",lfn,":",\
#                          lfc.sstrerror(lfc.cvar.serrno)
#              else:
                 if(extLinks):
                    # Remove the replicas and all the links that point to this LFN
                    err = lfc_del_lfn.deleteOneEntry(lfn, fulldir, False, True, verbose)
                 else:
                    # Remove only this LFN and replicas (but no external sym links)
                    err = lfc_del_lfn.deleteOneEntry(lfn, fulldir, False, False, verbose)
            
         if(err): rc = err
         dir_entry=lfc.lfc_readdirg(dir_p)
      
   else:
      if(dir_entry):
         sys.stderr.write("Error: Directory "+dir+" not empty! Consider using -r.\n")
         return -1

   # Close the directory
   if(lfc.lfc_closedir(dir_p) < 0):
      sys.stderr.write("Error closing dir: "+dir+" : "+lfc.sstrerror(lfc.cvar.serrno)+"\n")

   # Remove all subdirectories in the list 
   for subdir in subdirlist:
      err = deleteOneDir(fulldir+'/'+subdir, force, extLinks, extLfns, verbose)
      if(err): rc=err
 
   # Finally, remove also the top directory itself 
   err = lfc.lfc_chdir("..")
   if(err < 0):
      sys.stderr.write("Error changing dir to \"..\" : "+lfc.sstrerror(lfc.cvar.serrno)+"\n")
      return err

   if(verbose):
      print "--lfc.lfc_unlink(\""+dir+"\")"
   err = lfc.lfc_rmdir(dir)
   if(err<0):
      sys.stderr.write("Error removing dir: "+dir+": "+lfc.sstrerror(lfc.cvar.serrno)+"\n")
      return err

 # Return the error code
#   return rc
   return err
예제 #14
0
def readdirg_grs(*args):
  
  if len(args) < 1:
    folder = ''
  else:
    folder = args[0]
  
  if (folder == '') or (folder[0] != '/'):
    if 'LFC_HOME' in os.environ:
      folder = os.environ['LFC_HOME'] + '/' + folder
    else:
      sys.exit('Relative folder path requires LFC_HOME to be set and exported')
  
  #---------------------------------------------------------------------------
  # Open the folder
  #---------------------------------------------------------------------------
  dir = lfc.lfc_opendirg(folder, '')
  if dir == None:
    err_num    = lfc.cvar.serrno
    err_string = lfc.sstrerror(err_num)
    sys.exit('Error ' + str(err_num) + ' on folder ' + folder + ': ' + err_string)
  
  files = []
  guids = []
  listp = lfc.lfc_list()
  
  #---------------------------------------------------------------------------
  # Loop on the entries of the folder to build :
  # - the list of all files with Name, Filemode and GUID,
  # - the list of GUID for all regular files.
  #---------------------------------------------------------------------------
  while 1:
    entry = lfc.lfc_readdirg(dir)
    if entry == None:
      break
    if   entry.filemode & 040000:
      marker = '/'
    elif entry.filemode & 020000:     # not entry.guid:
      marker = '@'
    else:
      marker = ''
    files.append({'name': entry.d_name + marker,
                  'mode': entry.filemode,
                  'guid': entry.guid})
    if not (entry.filemode & 060000):     # Exclude folders and symbolic links
      guids.append(entry.guid)
  
  lfc.lfc_closedir(dir)
  
  #---------------------------------------------------------------------------
  # Get all replicas at once and build dictionaries per GUID
  #---------------------------------------------------------------------------
  guidSizes    = {}
  guidReplicas = {}
  (res, rep_entries) = lfc.lfc_getreplicas(guids, '')
  if res != 0:
    print 'lfc_getreplicas :  Error ' + str(res) + "\n"
  else:
    for entry in rep_entries:
      if entry.errcode == 0:
        if entry.guid in guidReplicas:
          guidReplicas[entry.guid].append(entry.sfn) 
        else:
          guidSizes[entry.guid]    = entry.filesize
          guidReplicas[entry.guid] = [entry.sfn]
  
  #---------------------------------------------------------------------------
  # Loop on the list of files to print their Filemode, Name and Replicas
  #---------------------------------------------------------------------------
  for myfile in files:
    print ('%06o' % myfile['mode']) + '  ' + myfile['name']
    guid = myfile['guid']
    if guid in guidReplicas:
      nb_replicas = 0
      for replica in guidReplicas[guid]:
        if replica != '':
          nb_replicas += 1
          print '   ==>', replica
      print 'Found ' + str(nb_replicas) + ' replica(s)     Size=' +  \
            str(guidSizes[guid])
    print