def execute(self, *args): # essential parameters check valid = EssentialParameters( self.__class__.__name__, [self._host, self._user, self._src_dir, self._src_pattern], ) valid() os.makedirs(self._dest_dir, exist_ok=True) # fetch src ftp_util = FtpUtil( self._host, self._user, self._password, self._timeout, self._retry_count, self._port, self._tls, ) files = ftp_util.list_files(self._src_dir, self._dest_dir, re.compile(self._src_pattern)) if self._quit is True and len(files) == 0: self._logger.info( "No file was found. After process will not be processed") return StepStatus.SUCCESSFUL_TERMINATION # cache downloaded file names ObjectStore.put(self._step, files)
def test_list_files_ok(self): # use public ftp host = "test.rebex.net" ftp_util = FtpUtil(host, "demo", "password") src_dir = "/" pattern = "(.*).txt" os.makedirs(self.__dest_dir) ftp_util.list_files(src_dir, self.__dest_dir, re.compile(pattern)) exists_downloaded_file = os.path.exists( os.path.join(self.__dest_dir, "readme.txt")) shutil.rmtree(self.__dest_dir) assert exists_downloaded_file is True
def test_list_files_ng(self): # use public ftp host = "test.rebex.net" # Invalid password timeout = 5 retry_count = 1 ftp_util = FtpUtil(host, "demo", "pass", timeout, retry_count) src_dir = "/" pattern = "(.*).txt" os.makedirs(self.__dest_dir) with pytest.raises(IOError) as execinfo: ftp_util.list_files(src_dir, self.__dest_dir, re.compile(pattern)) shutil.rmtree(self.__dest_dir) assert "FTP failed." in str(execinfo.value)
def execute(self, *args): files = ObjectStore.get(self._symbol) if files is not None and len(files) > 0: self._logger.info("Delete files %s" % files) ftp_util = FtpUtil( super().get_step_argument("host"), super().get_step_argument("user"), super().get_step_argument("password"), super().get_step_argument("timeout"), super().get_step_argument("retry_count"), super().get_step_argument("port"), super().get_step_argument("tls"), ) for file in files: ftp_util.remove_specific_file( super().get_step_argument("src_dir"), file) else: self._logger.info("No files to delete.")