def wrapper(*args, **kwargs): retry_count = MyGlobals.ftpRetriesCount while retry_count: try: return func(*args, **kwargs) except socket.timeout: print( 'Timeout occurred while attempting to {}. Retry count left: {}/{}' .format(action_description, retry_count, MyGlobals.ftpRetriesCount)) retry_count -= 1 except OSError as errorMsg: error_msg_str = str(errorMsg).lower() timeout_str = 'timed out' if timeout_str not in error_msg_str: print('Failed to {}.\nError:\n{}'.format( action_description, errorMsg)) return None print( 'Timeout occurred while attempting to {}. Retry count left: {}/{}' .format(action_description, retry_count, MyGlobals.ftpRetriesCount)) retry_count -= 1 except BaseException as errorMsg: print("Failed to {}.\nError:\n{}".format( action_description, errorMsg)) return None MyGlobals.sleep_for_a_while( MyGlobals.sleepForBetweenActions_Default) print('Timeouts over - returning') return None
def _download_ftp_dir(ftp_con, ftp_src, dest): print('Downloading dir: "{}" to: {}'.format(ftp_src, dest)) if not prepare_ftp_and_os_indexes(ftp_con, ftp_src, dest): return False file_list = get_file_list(ftp_con) if file_list is None or file_list is False: return False ftp_src_dir_name = MyGlobals.get_dir_name(ftp_src) print('Dir {} Files:\n -- {}\n\n'.format(ftp_src_dir_name, '\n -- '.join(file_list))) cur_ind = 0 end_ind = len(file_list) download_process_ok = True while (download_process_ok and cur_ind < end_ind): MyGlobals.sleep_for_a_while(MyGlobals.sleepBetweenDownloads_Default) cur_target_name = file_list[cur_ind] # Testing if it's a directory - then recurse try: target_path = ftp_src + "/" + cur_target_name # Check if a directory - if so then ftp-cd to it: if MyGlobals.isVerbose: print("Checking if {} is a directory".format(target_path)) is_dir = ftp_check_is_dir(ftp_con, target_path) if is_dir is None: print( 'Failed checking if {} is a directory'.format(target_path)) return False elif is_dir: if not change_ftp_dir(ftp_con, target_path): return False else: raise ftplib.error_perm( "target_path={} is a file".format(target_path)) download_process_ok = download_process_ok and _download_ftp_dir( ftp_con, target_path, dest + '/' + cur_target_name) if download_process_ok and not change_ftp_dir(ftp_con, ftp_src): return False # If got flag: --remove_src then Attempt to remove the dir after downloading it successfully if download_process_ok and MyGlobals.isRemoveSrc: download_process_ok = download_process_ok and ftp_delete_dir( ftp_con, target_path) except ftplib.error_perm: # It's a file: download_process_ok = download_process_ok and download_ftp_file( ftp_con, cur_target_name, dest) cur_ind += 1 return download_process_ok