#!/usr/bin/env python import string, commands, sys, os, getopt, ftplib, time import DFC # the hosts we can get level3 tcl file - they are all the same! ftp_list = { 'cdfcode.fnal.gov' : '/level3tcl/ProductionSplitter', 'cdfkits.fnal.gov' : '/level3tcl/ProductionSplitter', 'cdfcvs.fnal.gov' : '/level3tcl/ProductionSplitter', 'cdfcodebrowser.fnal.gov' : '/level3tcl/ProductionSplitter', } # the file content tclString = "" def getTcl(inStr): global tclString tclString = tclString + inStr def ts(): return time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(time.time())) try: optlist, args = getopt.getopt(sys.argv[1:], '', ['run=', 'output=', ]) except getopt.GetoptError, e: print ts() + ' GetL3TCL: argument error' sys.stdout.flush() sys.exit(1) run = -1 fn = '' for key, val in optlist: if key == '--run': run = string.atoi(val, 10) elif key == '--output': fn = val if run == -1 or fn == '': print ts() + ' GetL3TCL: no run or output assigned.' sys.stdout.flush() sys.exit(1) TagByRun = DFC.getL3TagByRun(run, -1) if not TagByRun.has_key( run ): print ts() + ' GetL3TCL: get level3 tcl tag failed for run number %d.' % run sys.stdout.flush() sys.exit(1) tag = "ProductionSplitter+%d.tcl" % TagByRun[run] done = 1 for i in ftp_list.keys(): tcl_path = ftp_list[i] + '/' + tag try: ftp_client = ftplib.FTP( i ) welcome = ftp_client.login() except: print ts() + 'Login to host %s failed' % i continue print ts() + ' \n%s\n' % welcome tclString = "" try: ftp_client.retrbinary('RETR %s' % tcl_path, getTcl) except: print ts() + 'get file %s failed from host %s' % (tcl_path, i) continue ftp_client.close() print ts() + 'get file %s successful from host %s' % (tcl_path, i) done = 0 break if done == 0: fp = open(fn, 'wt') fp.write( tclString ) fp.flush() fp.close() sys.stdout.flush() sys.exit(0) else: sys.stdout.flush() sys.exit(1)