def ftp_get(ftp, local_path, remote_path, script): """ Recursively retrieve files, starting at path """ if not hasattr(ftp, 'attr_name'): if script.get('files') != None: pattern = re.compile(script.get('files')) else: pattern = re.compile('') local_path = retrieve_local_path(local_path) if script.get('port') is None: ftp = FTP(script.get('host'), \ script.get('user'), script.get('password')) else: ftp = FTP() ftp.connect(script.get('host'), script.get('port')) ftp.login(script.get('user'), script.get('password')) if script.get('namefmt') != None: sndcmd = 'site namefmt ' + str(script.get('namefmt')) ftp.sendcmd(sndcmd) try: for entry in ftp.mlsd(remote_path, facts=["type"]): if entry[1].get('type') == 'dir': new_remote_path = remote_path + '/' + entry[0] new_local_path = local_path + '/' + entry[0] if not os.path.exists(new_local_path) \ and script.get('test') is False: os.makedirs(new_local_path) for new_entry in ftp_get(ftp, new_local_path, \ new_remote_path, script): yield new_entry if entry[1].get('type') == 'file' \ and (pattern.search(entry[0]) != None \ or script.get('files') is None): local_file = os.path.join(local_path, entry[0]) remote_file = remote_path + '/' + entry[0] yield remote_file if script.get('test') is False: with open(local_file, 'wb') as file_get: ftp.retrbinary('RETR %s' % \ remote_file, \ lambda data: file_get.write(data)) if script.get('delete') is True: ftp.delete(remote_file) except: pass ftp.quit()
def ftp_put(script): """ Sends local files matching file mask to remote server Parameter script is a dictionary object Yields each of the found files """ # Position to the local folder if script.get('local') != None: local = retrieve_local_path(script.get('local')) os.chdir(local) else: local = os.getcwd() local_root = len(local) # Remote positioning remote = script.get('remote') or '' # Build the list of files to send entries = os.listdir(local) pattern = re.compile(script.get('files') or '') # And start sending for dirname, dirnames, filenames in os.walk(local): remote_dir = dirname[local_root: len(dirname)] remote_full_path = remote + remote_dir os.chdir(dirname) if remote_dir != '': if script.get('port') is None: ftp = FTP(script.get('host'), \ script.get('user'), script.get('password')) else: ftp = FTP() ftp.connect(script.get('host'), script.get('port')) ftp.login(script.get('user'), script.get('password')) if script.get('namefmt') != None: sndcmd = 'site namefmt ' + str(script.get('namefmt')) ftp.sendcmd(sndcmd) try: ftp.cwd(remote_full_path) except: if script.get('test') is False: ftp.mkd(remote_full_path) yield remote_full_path ftp.quit() for filename in filenames: if pattern.search(filename) != None \ or script.get('files') is None: try: if script.get('port') is None: ftp = FTP(script.get('host'), \ script.get('user'), script.get('password')) else: ftp = FTP() ftp.connect(script.get('host'), script.get('port')) ftp.login(script.get('user'), script.get('password')) ftp.cwd(remote_full_path) if script.get('test') is False: ftp.storbinary('STOR %s' % \ filename, open(filename, 'rb'), 1024) if script.get('delete') is True: os.remove(filename) yield remote_full_path + '/' + filename ftp.quit() except: pass