import os,fnmatch import tempfile,subprocess from optparse import OptionParser from collections import deque import time ### GLOBALS LOCAL_DIRECTORY = "/home/sdobbs/transfer_test" # set this to the base directory FILE_GLOB = "dana_rest_*.hddm" # transfer files that match this NUM_TO_TRANSFER = 3 # number of files to transfer per gridftp invocation 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") parser.add_option("-n", "--num-files", dest="num_files", help="number of files to transfer at once", metavar="NUM_FILES") (options, args) = parser.parse_args() if options.verbose: VERBOSE = options.verbose if options.glob: FILE_GLOB = options.glob if options.num_files: NUM_TO_TRANSFER = options.num_file ### build list of files filelist = deque() 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 files done = False while not done: tf = tempfile.NamedTemporaryFile(mode='w') reportstr = "copying the following files:\n" for i in xrange(NUM_TO_TRANSFER): try: filepath = filelist.popleft() filename = filepath.split("/")[-1] #reportstr = reportstr + filepath + "\n" filepair = "file:///" + filepath + " gsiftp://" + REMOTE_HOST + REMOTE_DIRECTORY + "/" + filename reportstr = reportstr + filepair + "\n" print >>tf, filepair except IndexError: done = True # make sure that the file names are written to disk so that globus-url-copy can read them tf.flush() cmd = "globus-url-copy -f " + tf.name print >>logfile, reportstr if VERBOSE: print reportstr #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 with transfer! (code %d)" % (process.returncode) print cmdstderr print >>logerrfile, "Error with transfer! (code %d)" % (process.returncode) print >>logerrfile, cmdstderr ### cleanup logfile.close() logerrfile.close()