def main(): Script.registerSwitch('a', "All", " Also show inactive replicas") Script.parseCommandLine(ignoreErrors=True) lfns = Script.getPositionalArgs() switches = Script.getUnprocessedSwitches() active = True for switch in switches: opt = switch[0].lower() if opt in ("a", "all"): active = False if len(lfns) < 1: Script.showHelp(exitCode=1) from DIRAC.Interfaces.API.Dirac import Dirac dirac = Dirac() exitCode = 0 if len(lfns) == 1: try: with open(lfns[0], 'r') as f: lfns = f.read().splitlines() except BaseException: pass result = dirac.getReplicas(lfns, active=active, printOutput=True) if not result['OK']: print('ERROR: ', result['Message']) exitCode = 2 DIRAC.exit(exitCode)
def main(): Script.registerSwitch("a", "All", " Also show inactive replicas") # Registering arguments will automatically add their description to the help menu Script.registerArgument( ["LFN: Logical File Name or file containing LFNs"]) switches, lfns = Script.parseCommandLine(ignoreErrors=True) active = True for switch in switches: opt = switch[0].lower() if opt in ("a", "all"): active = False from DIRAC.Interfaces.API.Dirac import Dirac dirac = Dirac() exitCode = 0 if len(lfns) == 1: try: with open(lfns[0], "r") as f: lfns = f.read().splitlines() except Exception: pass result = dirac.getReplicas(lfns, active=active, printOutput=True) if not result["OK"]: print("ERROR: ", result["Message"]) exitCode = 2 DIRAC.exit(exitCode)
Script.parseCommandLine(ignoreErrors=True) lfns = Script.getPositionalArgs() switches = Script.getUnprocessedSwitches() active = True for switch in switches: opt = switch[0].lower() if opt in ("a", "all"): active = False if len(lfns) < 1: Script.showHelp() from DIRAC.Interfaces.API.Dirac import Dirac dirac = Dirac() exitCode = 0 if len(lfns) == 1: try: f = open(lfns[0], 'r') lfns = f.read().splitlines() f.close() except: pass result = dirac.getReplicas(lfns, active=active, printOutput=True) if not result['OK']: print 'ERROR: ', result['Message'] exitCode = 2 DIRAC.exit(exitCode)
Script.parseCommandLine( ignoreErrors = True ) lfns = Script.getPositionalArgs() switches = Script.getUnprocessedSwitches() active = True for switch in switches: opt = switch[0].lower() if opt in ( "a", "all" ): active = False if len( lfns ) < 1: Script.showHelp() from DIRAC.Interfaces.API.Dirac import Dirac dirac = Dirac() exitCode = 0 if len( lfns ) == 1: try: f = open( lfns[0], 'r' ) lfns = f.read().splitlines() f.close() except: pass result = dirac.getReplicas( lfns, active = active, printOutput = True ) if not result['OK']: print 'ERROR: ', result['Message'] exitCode = 2 DIRAC.exit( exitCode )
if os.path.exists( fileName ): try: lfnFile = open( fileName ) lfns = [ k.strip() for k in lfnFile.readlines() ] lfnFile.close() except Exception: DIRAC.gLogger.exception( 'Can not open file', fileName ) DIRAC.exit( -1 ) else: lfns = args[1:] dirac = Dirac() res = dirac.getReplicas( lfns[0:], active = True, printOutput = False ) if not res['OK']: DIRAC.gLogger.error( res['Message'] ) DIRAC.exit( -1 ) stagerClient = StorageManagerClient() stageLfns = [] for lfn, replicas in res['Value']['Successful'].items(): if seName in replicas: stageLfns.append( lfn ) if len( stageLfns ) >= 10: # Use a fake JobID = 0 request = stagerClient.setRequest( { seName : stageLfns }, 'WorkloadManagement', 'updateJobFromStager@WorkloadManagement/JobStateUpdate', 0 )
from DIRAC.StorageManagementSystem.Client.StorageManagerClient import StorageManagerClient if os.path.exists(fileName): try: lfnFile = open(fileName) lfns = [k.strip() for k in lfnFile.readlines()] lfnFile.close() except Exception: DIRAC.gLogger.exception('Can not open file', fileName) DIRAC.exit(-1) else: lfns = args[1:] dirac = Dirac() res = dirac.getReplicas(lfns[0:], active=True, printOutput=False) if not res['OK']: DIRAC.gLogger.error(res['Message']) DIRAC.exit(-1) stagerClient = StorageManagerClient() stageLfns = [] for lfn, replicas in res['Value']['Successful'].items(): if seName in replicas: stageLfns.append(lfn) if len(stageLfns) >= 10: # Use a fake JobID = 0 request = stagerClient.setRequest( {seName: stageLfns}, 'WorkloadManagement',
if not oldLFN: leave('The original LFN of the file to be fixed must be specified', exitCode=2) if not os.path.exists(directory): leave("Optional directory %s doesn't exist" % directory, exitCode=2) if not newLFN: gLogger.verbose('No new LFN specified, will replace the existing LFN %s' % (oldLFN)) newLFN = oldLFN gLogger.verbose('Directory for downloading file is set to %s' % directory) replicaInfo = dirac.getReplicas(oldLFN) if not replicaInfo['OK'] or replicaInfo['Value']['Failed']: leave('Could not get replica information for %s' % oldLFN, replicaInfo, exitCode=2) replicas = replicaInfo['Value']['Successful'][oldLFN] storageElements = replicas.keys() if not storageElements: leave('Could not determine SEs for replicas of %s' % oldLFN, exitCode=2) gLogger.info('Existing LFN has replicas at: %s' % ', '.join(storageElements)) oldGUID = dirac.getLfnMetadata(oldLFN) if not oldGUID['OK'] or oldGUID['Value']['Failed']: leave('Could not obtain GUID from LFC for %s - %s' % oldLFN,
class DIRACBackend(GridBackend): """Grid backend using the GFAL command line tools `gfal-*`.""" def __init__(self, **kwargs): GridBackend.__init__(self, catalogue_prefix='', **kwargs) from DIRAC.Core.Base import Script Script.initialize() from DIRAC.FrameworkSystem.Client.ProxyManagerClient import ProxyManagerClient self.pm = ProxyManagerClient() proxy = self.pm.getUserProxiesInfo() if not proxy['OK']: raise BackendException("Proxy error.") from DIRAC.Interfaces.API.Dirac import Dirac self.dirac = Dirac() from DIRAC.Resources.Catalog.FileCatalog import FileCatalog self.fc = FileCatalog() from DIRAC.DataManagementSystem.Client.DataManager import DataManager self.dm = DataManager() self._xattr_cmd = sh.Command('gfal-xattr').bake(_tty_out=False) self._replica_checksum_cmd = sh.Command('gfal-sum').bake(_tty_out=False) self._bringonline_cmd = sh.Command('gfal-legacy-bringonline').bake(_tty_out=False) self._cp_cmd = sh.Command('gfal-copy').bake(_tty_out=False) self._ls_se_cmd = sh.Command('gfal-ls').bake(color='never', _tty_out=False) self._move_cmd = sh.Command('gfal-rename').bake(_tty_out=False) self._mkdir_cmd = sh.Command('gfal-mkdir').bake(_tty_out=False) self._replicate_cmd = sh.Command('dirac-dms-replicate-lfn').bake(_tty_out=False) self._add_cmd = sh.Command('dirac-dms-add-file').bake(_tty_out=False) @staticmethod def _check_return_value(ret): if not ret['OK']: raise BackendException("Failed: %s", ret['Message']) for path, error in ret['Value']['Failed'].items(): if ('No such' in error) or ('Directory does not' in error): raise DoesNotExistException("No such file or directory.") else: raise BackendException(error) def _is_dir(self, lurl): isdir = self.fc.isDirectory(lurl) self._check_return_value(isdir) return isdir['Value']['Successful'][lurl] def _is_file(self, lurl): isfile = self.fc.isFile(lurl) self._check_return_value(isfile) return isfile['Value']['Successful'][lurl] def _get_dir_entry(self, lurl, infodict=None): """Take a lurl and return a DirEntry.""" # If no dctionary with the information is specified, get it from the catalogue try: md = infodict['MetaData'] except TypeError: md = self.fc.getFileMetadata(lurl) if not md['OK']: raise BackendException("Failed to list path '%s': %s", lurl, md['Message']) for path, error in md['Value']['Failed'].items(): if 'No such file' in error: # File does not exist, maybe a directory? md = self.fc.getDirectoryMetadata(lurl) for path, error in md['Value']['Failed'].items(): raise DoesNotExistException("No such file or directory.") else: raise BackendException(md['Value']['Failed'][lurl]) md = md['Value']['Successful'][lurl] return DirEntry(posixpath.basename(lurl), mode=oct(md.get('Mode', -1)), links=md.get('links', -1), gid=md['OwnerGroup'], uid=md['Owner'], size=md.get('Size', -1), modified=str(md.get('ModificationDate', '?'))) def _iter_directory(self, lurl): """Iterate over entries in a directory.""" ret = self.fc.listDirectory(lurl) if not ret['OK']: raise BackendException("Failed to list path '%s': %s", lurl, ret['Message']) for path, error in ret['Value']['Failed'].items(): if 'Directory does not' in error: # Dir does not exist, maybe a File? if self.fc.isFile(lurl): lst = [(lurl, None)] break else: raise DoesNotExistException("No such file or Directory.") else: raise BackendException(ret['Value']['Failed'][lurl]) else: # Sort items by keys, i.e. paths lst = sorted(ret['Value']['Successful'][lurl]['Files'].items() + ret['Value']['Successful'][lurl]['SubDirs'].items()) for item in lst: yield item # = path, dict def _ls(self, lurl, **kwargs): # Translate keyword arguments d = kwargs.pop('directory', False) if d: # Just the requested entry itself yield self._get_dir_entry(lurl) return for path, info in self._iter_directory(lurl): yield self._get_dir_entry(path, info) def _ls_se(self, surl, **kwargs): # Translate keyword arguments d = kwargs.pop('directory', False) args = [] if -d: args.append('-d') args.append('-l') args.append(surl) try: output = self._ls_se_cmd(*args, **kwargs) except sh.ErrorReturnCode as e: if 'No such file' in e.stderr: raise DoesNotExistException("No such file or Directory.") else: raise BackendException(e.stderr) for line in output: fields = line.split() mode, links, gid, uid, size = fields[:5] name = fields[-1] modified = ' '.join(fields[5:-1]) yield DirEntry(name, mode=mode, links=int(links), gid=gid, uid=uid, size=int(size), modified=modified) def _replicas(self, lurl, **kwargs): # Check the lurl actually exists self._ls(lurl, directory=True) rep = self.dirac.getReplicas(lurl) self._check_return_value(rep) rep = rep['Value']['Successful'][lurl] return rep.values() def _exists(self, surl, **kwargs): try: ret = self._ls_se_cmd(surl, '-d', '-l', **kwargs).strip() except sh.ErrorReturnCode as e: if 'No such file' in e.stderr: return False else: if len(e.stderr) == 0: raise BackendException(e.stdout) else: raise BackendException(e.stderr) else: return ret[0] != 'd' # Return `False` for directories def _register(self, surl, lurl, verbose=False, **kwargs): # Register an existing physical copy in the file catalogue se = storage.get_SE(surl).name # See if file already exists in DFC ret = self.fc.getFileMetadata(lurl) try: self._check_return_value(ret) except DoesNotExistException: # Add new file size = next(self._ls_se(surl, directory=True)).size checksum = self.checksum(surl) guid = str(uuid.uuid4()) # The guid does not seem to be important. Make it unique if possible. ret = self.dm.registerFile((lurl, surl, size, se, guid, checksum)) else: # Add new replica ret = self.dm.registerReplica((lurl, surl, se)) self._check_return_value(ret) if verbose: print_("Successfully registered replica %s of %s from %s."%(surl, lurl, se)) return True def _deregister(self, surl, lurl, verbose=False, **kwargs): # DIRAC only needs to know the SE name to deregister a replica se = storage.get_SE(surl).name ret = self.dm.removeReplicaFromCatalog(se, [lurl]) self._check_return_value(ret) if verbose: print_("Successfully deregistered replica of %s from %s."%(lurl, se)) return True def _state(self, surl, **kwargs): try: state = self._xattr_cmd(surl, 'user.status', **kwargs).strip() except sh.ErrorReturnCode as e: if "No such file" in e.stderr: raise DoesNotExistException("No such file or Directory.") state = '?' except sh.SignalException_SIGSEGV: state = '?' return state def _checksum(self, surl, **kwargs): try: checksum = self._replica_checksum_cmd(surl, 'ADLER32', **kwargs).split()[1] except sh.ErrorReturnCode: checksum = '?' except sh.SignalException_SIGSEGV: checksum = '?' except IndexError: checksum = '?' return checksum def _bringonline(self, surl, timeout, verbose=False, **kwargs): if verbose: out = sys.stdout else: out = None # gfal does not notice when files come online, it seems # Just send a single short request, then check regularly if verbose: out = sys.stdout else: out = None end = time.time() + timeout try: self._bringonline_cmd('-t', 10, surl, _out=out, **kwargs) except sh.ErrorReturnCode as e: # The command fails if the file is not online # To be expected after 10 seconds if "No such file" in e.stderr: # Except when the file does not actually exist on the tape storage raise DoesNotExistException("No such file or Directory.") wait = 5 while(True): if verbose: print_("Checking replica state...") if self.is_online(surl): if verbose: print_("Replica brought online.") return True time_left = end - time.time() if time_left <= 0: if verbose: print_("Could not bring replica online.") return False wait *= 2 if time_left < wait: wait = time_left if verbose: print_("Timeout remaining: %d s"%(time_left)) print_("Checking again in: %d s"%(wait)) time.sleep(wait) def _replicate(self, source_surl, destination_surl, lurl, verbose=False, **kwargs): if verbose: out = sys.stdout else: out = None source = storage.get_SE(source_surl).name destination = storage.get_SE(destination_surl).name try: self._replicate_cmd(lurl, destination, source, _out=out, **kwargs) except sh.ErrorReturnCode as e: if 'No such file' in e.stderr: raise DoesNotExistException("No such file or directory.") else: if len(e.stderr) == 0: raise BackendException(e.stdout) else: raise BackendException(e.stderr) return True def _get(self, surl, localpath, verbose=False, **kwargs): if verbose: out = sys.stdout else: out = None try: self._cp_cmd('-f', '--checksum', 'ADLER32', surl, localpath, _out=out, **kwargs) except sh.ErrorReturnCode as e: if 'No such file' in e.stderr: raise DoesNotExistException("No such file or directory.") else: if len(e.stderr) == 0: raise BackendException(e.stdout) else: raise BackendException(e.stderr) return os.path.isfile(localpath) def _put(self, localpath, surl, lurl, verbose=False, **kwargs): if verbose: out = sys.stdout else: out = None se = storage.get_SE(surl).name try: self._add_cmd(lurl, localpath, se, _out=out, **kwargs) except sh.ErrorReturnCode as e: if 'No such file' in e.stderr: raise DoesNotExistException("No such file or directory.") else: if len(e.stderr) == 0: raise BackendException(e.stdout) else: raise BackendException(e.stderr) return True def _remove(self, surl, lurl, last=False, verbose=False, **kwargs): se = storage.get_SE(surl).name if last: # Delete lfn if verbose: print_("Removing all replicas of %s."%(lurl,)) ret = self.dm.removeFile([lurl]) else: if verbose: print_("Removing replica of %s from %s."%(lurl, se)) ret = self.dm.removeReplica(se, [lurl]) if not ret['OK']: raise BackendException('Failed: %s'%(ret['Message'])) for lurl, error in ret['Value']['Failed'].items(): if 'No such file' in error: raise DoesNotExistException("No such file or directory.") else: raise BackendException(error) return True def _rmdir(self, lurl, verbose=False): """Remove the an empty directory from the catalogue.""" rep = self.fc.removeDirectory(lurl) self._check_return_value(rep) return True def _move_replica(self, surl, new_surl, verbose=False, **kwargs): if verbose: out = sys.stdout else: out = None try: folder = posixpath.dirname(new_surl) self._mkdir_cmd(folder, '-p', _out=out, **kwargs) self._move_cmd(surl, new_surl, _out=out, **kwargs) except sh.ErrorReturnCode as e: if 'No such file' in e.stderr: raise DoesNotExistException("No such file or directory.") else: if len(e.stderr) == 0: raise BackendException(e.stdout) else: raise BackendException(e.stderr) return True
from DIRAC.Interfaces.API.Dirac import Dirac from LHCbDIRAC.Core.Utilities.File import getRootFileGUIDs from LHCbDIRAC.DataManagementSystem.Client.DMScript import printDMResult if not files: Script.showHelp() DIRAC.exit(0) existFiles = {} nonExisting = [] dirac = Dirac() for localFile in files: if os.path.exists(localFile): existFiles[os.path.realpath(localFile)] = localFile elif localFile.startswith('/lhcb'): res = dirac.getReplicas(localFile, active=True, preferDisk=True) if res['OK'] and localFile in res['Value']['Successful']: ses = res['Value']['Successful'][localFile].keys() for se in ses: res = dirac.getAccessURL(localFile, se, protocol=['root', 'xroot']) if res['OK'] and localFile in res['Value']['Successful']: existFiles[res['Value']['Successful'][localFile]] = "%s @ %s" % (localFile, se) else: nonExisting.append(localFile) elif localFile.startswith('root:'): existFiles[localFile] = localFile else: nonExisting.append(localFile) fileGUIDs = getRootFileGUIDs(existFiles.keys()) for status in ('Successful', 'Failed'):