import getopt, sys, datetime import paramiko, ftplib ######################################################### # Commandline Args # ######################################################### def usage(): print "-------------------------------------------------------" print "-Usage: -" print "-------------------------------------------------------" print "- --config_file= : Path to upload config file -" print "- -if no path is specified look -" print "- in the working directory. -" print "- -" print "- --help : Print this message -" print "- -" print "- --datestamp= : Optional, defaults to today -" print "- -replaces *datestamp in the -" print "- the upload config file -" print "-------------------------------------------------------" try: options, arguments = getopt.getopt(sys.argv[1:],"h",['config_file=', 'datestamp=', 'help']) except getopt.GetoptError, err: print str(err) usage() sys.exit(2) for option, value in options: if option in ("-h","--h","-help","--help"): usage() sys.exit() elif option in ("-config_file","--config_file"): config_file = value elif option in ("-datestamp","--datestamp"): datestamp = value for argument in arguments: if argument in ("h", "help"): usage() sys.exit() else: print "Unrecognized commandline argument. Type help for usage." sys.exit() if len(options) <= 0: print "Requires a config file to run." usage() sys.exit() ######################################################### # Read Config Function # ######################################################### def read_config_file(config_file, datestamp=datetime.datetime.today().strftime('%Y%m%d')): ''' Parse our config file that gets passed in as a commandline parameter. All config keys must be of the form key=value. White space is ignored. All config keys must be seperated by a newline character. Called during initialization. ''' upload_config = {} config_file = open(config_file, 'r') config_file = config_file.read() for config in config_file.split("\n"): if '=' in config: cleaned_key = config.split('=')[0].strip().upper() cleaned_value = config.split('=')[1].strip() if '*datestamp*' in cleaned_value: cleaned_value = cleaned_value.replace('*datestamp*',datestamp) upload_config[cleaned_key] = cleaned_value else: pass return upload_config ######################################################### # FTP File Transfer Function # ######################################################### def putfile_ftp(file, put_path, site, user, password, port=21, verbose=True): if verbose: print 'Uploading', file local = open(file, 'rb') remote = ftplib.FTP(site) remote.login(user, password) remote.storbinary(put_path, local, 1024) remote.quit() local.close() if verbose: print 'Upload done.' ######################################################### # SFTP File Transfer Function # ######################################################### def putfile_sftp(file, put_path, site, username, password, port=22, verbose=True): transport = paramiko.Transport((site, port)) transport.connect(username = username, password = password) sftp = paramiko.SFTPClient.from_transport(transport) sftp.put(file, put_path) sftp.close() transport.close() ######################################################### # Main # ######################################################### #if datestamp != None: # upload_config = read_config_file(config_file, datestamp) #else: # upload_config = read_config_file(config_file) try: datestamp except NameError: upload_config = read_config_file(config_file) else: upload_config = read_config_file(config_file, datestamp) print upload_config if upload_config["PROTOCOL_PUT"] in ['FTP','ftp']: print "Protocol = FTP" putfile_ftp(upload_config["GET_FILE_PATH"] + upload_config["GET_FILE_NAME"], upload_config["PUT_FILE_PATH"] + upload_config["PUT_FILE_NAME"], upload_config["PUT_HOST"], upload_config["USERNAME_PUT"], upload_config["PASSWORD_PUT"], upload_config["PORT_PUT"] ) elif upload_config["PROTOCOL_PUT"] in ['SFTP','sftp']: print "Protocol = SFTP" putfile_sftp(upload_config["GET_FILE_PATH"] + upload_config["GET_FILE_NAME"], upload_config["PUT_FILE_PATH"] + upload_config["PUT_FILE_NAME"], upload_config["PUT_HOST"], upload_config["USERNAME_PUT"], upload_config["PASSWORD_PUT"], int(upload_config["PORT_PUT"]) )