#!/usr/bin/env python import sys import os.path import string import SOAPpy import ftplib # Translates dimensions via web services. # Requires url for DimensionsService.wsdl.xml. class SamTranslateDimensions: def __init__(self, wsdl): self._proxy = SOAPpy.WSDL.Proxy(wsdl) def translateDimensions(self, dimensionsString): """ Returns dataset info with a list of files satisfying specified constraints. """ # Using either ordered or named arguments should work. #return self._proxy.translateDimensions(dimensionsString) return self._proxy.translateDimensions(dimensionsString=dimensionsString) # Retrieves list of replica locations via web services. # Requires url for DataFileService.wsdl.xml. class SamLocate: def __init__(self, wsdl): self._proxy = SOAPpy.WSDL.Proxy(wsdl) def getLocationList(self, fileName): """ Returns list of replica locations. """ # Using either ordered or named arguments should work. #return self._proxy.locate(fileName).locationList return self._proxy.locate(fileName=fileName).locationList # Client usage. def usage(): clientName = os.path.basename(sys.argv[0]) print "Usage:" print " %s --dim=" % clientName # Main code. if __name__ == "__main__": passwd = "xxxxx" dimensionsString = None for a in sys.argv[1:]: arg = string.split(a, "=") if arg[0] == "--dim": dimensionsString = string.join(arg[1:]) else: print "Ignoring unrecognized argument: %s" % a if not dimensionsString: usage() sys.exit(1) # Do the TranslateDimensions to get the list of files wsdl='http://www-numi.fnal.gov/sam_web_services/wsdl/DimensionsService.wsdl.xml' try: wsClient = SamTranslateDimensions(wsdl) datasetInfo = wsClient.translateDimensions(dimensionsString) fileList = datasetInfo.fileList datasetSizeInBytes = datasetInfo.datasetSizeInBytes print "SAM Query was: %s" % (dimensionsString) # Need to get file locations wsdl='http://www-numi.fnal.gov/sam_web_services/wsdl/DataFileService.wsdl.xml' for file in fileList: print file wsClient = SamLocate(wsdl) locationList = wsClient.getLocationList(file) # Determine pnfs location. Don't assume that pnfs location is first one on list for loc in locationList: if string.find(loc,"'/pnfs/minos") == 0: temp = string.split(loc,"/",3) temp2 = string.split(temp[3],',') path=temp2[0] # fetch them using ftp ftp=ftplib.FTP() try: ftp.connect("fndca1.fnal.gov",24126) except socket.error,msg: print "Error %s " % msg sys.exit(1) else: try: ftp.login("mindata",passwd) except ftplib.error_perm,msg: print "Error %s" % msg sys.exit(1) else: ftp.cwd(path) cmd="RETR %s" % file try: ftp.retrbinary(cmd,open(file,"w").write) except ftplib.error_perm,msg: print "Transfer Error %s " % msg continue else: print "Copied %s to local file %s" % (file,file) ftp.quit() print "Dataset size: %s bytes" % (datasetSizeInBytes) except SOAPpy.Error, ex: print "Caught SOAP exception: %s" % ex.faultstring except: print "Caught %s exception: %s" % (sys.exc_info()[0], sys.exc_info()[1])