import os,fnmatch import tempfile,subprocess from optparse import OptionParser ### GLOBALS LOCAL_DIRECTORY = "/home/sdobbs/transfer_test" # set this to the base directory FILE_GLOB = "dana_rest_*.hddm" # transfer files that match this REMOTE_HOST = "numep-grid6.research.northwestern.edu" REMOTE_DIRECTORY = "/mnt/xrootd/gluex/dc2/transfer_test" LOG_FILENAME = "transfer.log" LOG_ERROR_FILENAME = "transfer-err.log" VERBOSE = False if __name__ == "__main__": ### handle runtime options parser = OptionParser() parser.add_option("-v", "--verbose", action="store_true", dest="verbose", help="print verbose output to screen") parser.add_option("-m", "--match", dest="glob", help="transfer files that match a particular pattern (supports *,?,[])", metavar="GLOB") (options, args) = parser.parse_args() if options.verbose: VERBOSE = options.verbose if options.glob: FILE_GLOB = options.glob ### build list of files filelist = [] for root, dirs, files in os.walk(LOCAL_DIRECTORY): for f in fnmatch.filter(files, FILE_GLOB): filelist.append( os.path.join(root,f) ) ### open files for logging logfile = open(LOG_FILENAME, "a") logerrfile = open(LOG_ERROR_FILENAME, "a") ### transfer file for filepath in filelist: filename = filepath.split("/")[-1] cmd = "globus-url-copy file:///" + filepath + " gsiftp://" + REMOTE_HOST + REMOTE_DIRECTORY + "/" + filename print >>logfile, cmd if VERBOSE: print cmd process = subprocess.Popen(cmd.split(), shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (cmdstdout, cmdstderr) = process.communicate() if process.returncode != 0: print "Error transferring %s! (code %d)" % (filename,process.returncode) print cmdstderr print >>logerrfile, "Error transferring %s! (code %d)" % (filename,process.returncode) print >>logerrfile, cmdstderr ### cleanup logfile.close() logerrfile.close()