Skip to content

Commit 12cd2f4

Browse files
authored
Add support for MDTM command in FTPFS (#462)
2 parents 2d0ffc3 + 4feb242 commit 12cd2f4

File tree

7 files changed

+69
-10
lines changed

7 files changed

+69
-10
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1212

1313
- Added `fs.copy.copy_file_if`, `fs.copy.copy_dir_if`, and `fs.copy.copy_fs_if`.
1414
Closes [#458](https://github.com/PyFilesystem/pyfilesystem2/issues/458).
15+
- Added `fs.base.FS.getmodified`.
16+
17+
### Changed
18+
19+
- FTP servers that do not support the MLST command now try to use the MDTM command to
20+
retrieve the last modification timestamp of a resource.
21+
Closes [#456](https://github.com/PyFilesystem/pyfilesystem2/pull/456).
1522

1623
### Fixed
1724

docs/source/interface.rst

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ The following is a complete list of methods on PyFilesystem objects.
2020
* :meth:`~fs.base.FS.getdetails` Get details info namespace for a resource.
2121
* :meth:`~fs.base.FS.getinfo` Get info regarding a file or directory.
2222
* :meth:`~fs.base.FS.getmeta` Get meta information for a resource.
23+
* :meth:`~fs.base.FS.getmodified` Get info regarding the last modified time of a resource.
2324
* :meth:`~fs.base.FS.getospath` Get path with encoding expected by the OS.
2425
* :meth:`~fs.base.FS.getsize` Get the size of a file.
2526
* :meth:`~fs.base.FS.getsyspath` Get the system path of a resource, if one exists.

fs/base.py

+17
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,23 @@ def readtext(
697697

698698
gettext = _new_name(readtext, "gettext")
699699

700+
def getmodified(self, path):
701+
# type: (Text) -> Optional[datetime]
702+
"""Get the timestamp of the last modifying access of a resource.
703+
704+
Arguments:
705+
path (str): A path to a resource.
706+
707+
Returns:
708+
datetime: The timestamp of the last modification.
709+
710+
The *modified timestamp* of a file is the point in time
711+
that the file was last changed. Depending on the file system,
712+
it might only have limited accuracy.
713+
714+
"""
715+
return self.getinfo(path, namespaces=["details"]).modified
716+
700717
def getmeta(self, namespace="standard"):
701718
# type: (Text) -> Mapping[Text, object]
702719
"""Get meta information regarding a filesystem.

fs/copy.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -463,9 +463,8 @@ def _copy_is_necessary(
463463

464464
elif condition == "newer":
465465
try:
466-
namespace = ("details",)
467-
src_modified = src_fs.getinfo(src_path, namespace).modified
468-
dst_modified = dst_fs.getinfo(dst_path, namespace).modified
466+
src_modified = src_fs.getmodified(src_path)
467+
dst_modified = dst_fs.getmodified(dst_path)
469468
except ResourceNotFound:
470469
return True
471470
else:
@@ -477,9 +476,8 @@ def _copy_is_necessary(
477476

478477
elif condition == "older":
479478
try:
480-
namespace = ("details",)
481-
src_modified = src_fs.getinfo(src_path, namespace).modified
482-
dst_modified = dst_fs.getinfo(dst_path, namespace).modified
479+
src_modified = src_fs.getmodified(src_path)
480+
dst_modified = dst_fs.getmodified(dst_path)
483481
except ResourceNotFound:
484482
return True
485483
else:

fs/ftpfs.py

+20
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from .path import basename
4141
from .path import normpath
4242
from .path import split
43+
from .time import epoch_to_datetime
4344
from . import _ftp_parse as ftp_parse
4445

4546
if typing.TYPE_CHECKING:
@@ -572,6 +573,12 @@ def supports_mlst(self):
572573
"""bool: whether the server supports MLST feature."""
573574
return "MLST" in self.features
574575

576+
@property
577+
def supports_mdtm(self):
578+
# type: () -> bool
579+
"""bool: whether the server supports the MDTM feature."""
580+
return "MDTM" in self.features
581+
575582
def create(self, path, wipe=False):
576583
# type: (Text, bool) -> bool
577584
_path = self.validatepath(path)
@@ -692,8 +699,21 @@ def getmeta(self, namespace="standard"):
692699
if namespace == "standard":
693700
_meta = self._meta.copy()
694701
_meta["unicode_paths"] = "UTF8" in self.features
702+
_meta["supports_mtime"] = "MDTM" in self.features
695703
return _meta
696704

705+
def getmodified(self, path):
706+
# type: (Text) -> Optional[datetime.datetime]
707+
if self.supports_mdtm:
708+
_path = self.validatepath(path)
709+
with self._lock:
710+
with ftp_errors(self, path=path):
711+
cmd = "MDTM " + _encode(_path, self.ftp.encoding)
712+
response = self.ftp.sendcmd(cmd)
713+
mtime = self._parse_ftp_time(response.split()[1])
714+
return epoch_to_datetime(mtime)
715+
return super(FTPFS, self).getmodified(path)
716+
697717
def listdir(self, path):
698718
# type: (Text) -> List[Text]
699719
_path = self.validatepath(path)

tests/test_ftpfs.py

+17
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,23 @@ def test_getmeta_unicode_path(self):
261261
del self.fs.features["UTF8"]
262262
self.assertFalse(self.fs.getmeta().get("unicode_paths"))
263263

264+
def test_getinfo_modified(self):
265+
self.assertIn("MDTM", self.fs.features)
266+
self.fs.create("bar")
267+
mtime_detail = self.fs.getinfo("bar", ("basic", "details")).modified
268+
mtime_modified = self.fs.getmodified("bar")
269+
# Microsecond and seconds might not actually be supported by all
270+
# FTP commands, so we strip them before comparing if it looks
271+
# like at least one of the two values does not contain them.
272+
replacement = {}
273+
if mtime_detail.microsecond == 0 or mtime_modified.microsecond == 0:
274+
replacement["microsecond"] = 0
275+
if mtime_detail.second == 0 or mtime_modified.second == 0:
276+
replacement["second"] = 0
277+
self.assertEqual(
278+
mtime_detail.replace(**replacement), mtime_modified.replace(**replacement)
279+
)
280+
264281
def test_opener_path(self):
265282
self.fs.makedir("foo")
266283
self.fs.writetext("foo/bar", "baz")

tests/test_memoryfs.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,13 @@ def test_copy_preserve_time(self):
7272
self.fs.makedir("bar")
7373
self.fs.touch("foo/file.txt")
7474

75-
namespaces = ("details", "modified")
76-
src_info = self.fs.getinfo("foo/file.txt", namespaces)
75+
src_datetime = self.fs.getmodified("foo/file.txt")
7776

7877
self.fs.copy("foo/file.txt", "bar/file.txt", preserve_time=True)
7978
self.assertTrue(self.fs.exists("bar/file.txt"))
8079

81-
dst_info = self.fs.getinfo("bar/file.txt", namespaces)
82-
self.assertEqual(dst_info.modified, src_info.modified)
80+
dst_datetime = self.fs.getmodified("bar/file.txt")
81+
self.assertEqual(dst_datetime, src_datetime)
8382

8483

8584
class TestMemoryFile(unittest.TestCase):

0 commit comments

Comments
 (0)