[AIRFLOW-1167] Support microseconds in FTPHook modification time Closes #2268 from NielsZeilemaker/fix-ftp-hook
Project: http://git-wip-us.apache.org/repos/asf/incubator-airflow/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-airflow/commit/c5d6c3a3 Tree: http://git-wip-us.apache.org/repos/asf/incubator-airflow/tree/c5d6c3a3 Diff: http://git-wip-us.apache.org/repos/asf/incubator-airflow/diff/c5d6c3a3 Branch: refs/heads/v1-8-test Commit: c5d6c3a3cbd5ad28470e2d7e8434f439aae14a75 Parents: 1b2b34e Author: Niels Zeilemaker <[email protected]> Authored: Fri May 5 09:17:40 2017 +0200 Committer: Maxime Beauchemin <[email protected]> Committed: Thu Jun 8 08:36:20 2017 -0700 ---------------------------------------------------------------------- airflow/contrib/hooks/ftp_hook.py | 7 ++++++- tests/contrib/hooks/test_ftp_hook.py | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-airflow/blob/c5d6c3a3/airflow/contrib/hooks/ftp_hook.py ---------------------------------------------------------------------- diff --git a/airflow/contrib/hooks/ftp_hook.py b/airflow/contrib/hooks/ftp_hook.py index 2f2e4c2..dbd353b 100644 --- a/airflow/contrib/hooks/ftp_hook.py +++ b/airflow/contrib/hooks/ftp_hook.py @@ -226,7 +226,12 @@ class FTPHook(BaseHook): def get_mod_time(self, path): conn = self.get_conn() ftp_mdtm = conn.sendcmd('MDTM ' + path) - return datetime.datetime.strptime(ftp_mdtm[4:], '%Y%m%d%H%M%S') + time_val = ftp_mdtm[4:] + # time_val optionally has microseconds + try: + return datetime.datetime.strptime(time_val, "%Y%m%d%H%M%S.%f") + except ValueError: + return datetime.datetime.strptime(time_val, '%Y%m%d%H%M%S') class FTPSHook(FTPHook): http://git-wip-us.apache.org/repos/asf/incubator-airflow/blob/c5d6c3a3/tests/contrib/hooks/test_ftp_hook.py ---------------------------------------------------------------------- diff --git a/tests/contrib/hooks/test_ftp_hook.py b/tests/contrib/hooks/test_ftp_hook.py index ab6f459..c81593d 100644 --- a/tests/contrib/hooks/test_ftp_hook.py +++ b/tests/contrib/hooks/test_ftp_hook.py @@ -77,6 +77,24 @@ class TestFTPHook(unittest.TestCase): self.conn_mock.rename.assert_called_once_with(from_path, to_path) self.conn_mock.quit.assert_called_once_with() + + def test_mod_time(self): + self.conn_mock.sendcmd.return_value = '213 20170428010138' + + path = '/path/file' + with fh.FTPHook() as ftp_hook: + ftp_hook.get_mod_time(path) + + self.conn_mock.sendcmd.assert_called_once_with('MDTM ' + path) + + def test_mod_time_micro(self): + self.conn_mock.sendcmd.return_value = '213 20170428010138.003' + + path = '/path/file' + with fh.FTPHook() as ftp_hook: + ftp_hook.get_mod_time(path) + + self.conn_mock.sendcmd.assert_called_once_with('MDTM ' + path) if __name__ == '__main__':
