''' supersid_upload version: 1.1 Copyright: Stanford Solar Center - 2008 ''' #------------------------------------------------------------------------------- import sys import os import random import time from time import strftime import glob import ConfigParser from ftplib import FTP #------------------------------------------------------------------------------- # constants: _LOCAL_DATA_PATH = '../Data/' #------------------------------------------------------------------------------- # To better handling the network connection, condition variabilities,... # Let's keep "Upload" task separated from supersid's main operation, here class UploadConfig(): ''' UploadConfig uses only these parameters from "supersid.cfg" [PARAMETERS] automatic_upload = yes ftp_server = sid-ftp.stanford.edu ftp_directory = /incoming/NEW/ ''' def __init__(self, filename="../Config/supersid.cfg"): config_parser = ConfigParser.ConfigParser() config_parser.read(filename) try: section = 'PARAMETERS' parameter = 'automatic_upload' self.automatic_upload = config_parser.get(section, parameter) parameter = 'ftp_server' self.ftp_server = config_parser.get(section, parameter) parameter = 'ftp_directory' self.ftp_directory = config_parser.get(section, parameter) self.config_ok = 1 except: print "'",parameter ,"' is not found in 'supersid.cfg'. Please check." self.config_ok = 0 #------------------------------------------------------------------------------- def upload(filelist): config = UploadConfig() if(config.config_ok == 0): print "supersid_upload: Fail to read supersid.cfg" return if(config.automatic_upload.upper() != 'YES'): return try: ftp = FTP(config.ftp_server, 'anonymous', '') ftp.cwd(config.ftp_directory) filenames = filelist.split(",") for filename in filenames: file_handle = open(_LOCAL_DATA_PATH + filename,'rb') # file to send ftp.storbinary('STOR '+filename, file_handle) # Send the file file_handle.close() # Close file and FTP ftp.quit() print "Uploaded: ", filelist except: print "Fail to upload these files: ", filelist print "ftp_server: ", config.ftp_server print "ftp_directory: ", config.ftp_directory #------------------------------------------------------------------------------- ''' For running supersid_upload.py directly from command line ''' def do_main(filelist = '', delay_max = 0): # Randomly wait for (0-900 seconds) if(delay_max != "0"): seconds = int (random.random() * int(delay_max)) #seconds = int(delay_max) time.sleep(seconds) print "After delay for", seconds, "seconds." # If no filelist specified, get all "*.csv" in the last 24 hours if(filelist == ""): # Get yestedary files ie: "../Data/*2009-03-21.csv" file_specs = _LOCAL_DATA_PATH + "*" + strftime("%Y-%m-%d", time.gmtime((time.time()-(24*60*60)))) + ".csv" items = glob.glob(file_specs) filelist = '' for f in items: filelist = filelist + (os.path.split(f))[1] + "," filelist = filelist.strip(",") print "Files to upload : ", filelist upload(filelist) #------------------------------------------------------------------------------- if __name__ == '__main__': # Route all stdout to log sys.stdout = open("supersid_upload.log","w") filelist = '' delay_max = '900' if (len(sys.argv) == 2): filelist = sys.argv[1] if (len(sys.argv) == 3): filelist = sys.argv[1] delay_max = sys.argv[2] #print "filelist = ", filelist #print "delay_max = ", delay_max do_main(filelist, delay_max) #-------------------------------------------------------------------------------