Skip to content

Commit

Permalink
Merge pull request #479 from atollk/issue_477
Browse files Browse the repository at this point in the history
Some setinfo fixes
  • Loading branch information
althonos committed Apr 30, 2021
2 parents ac8a91a + b1511d2 commit 2d0ffc3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed

- Fixed performance bugs in `fs.copy.copy_dir_if_newer`. Test cases were adapted to catch those bugs in the future.
- Fixed precision bug for timestamps in `fs.OSFS.setinfo`.


## [2.4.13] - 2021-03-27
Expand Down
3 changes: 3 additions & 0 deletions fs/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ def is_writeable(self, namespace, key):
When creating an `Info` object, you can add a ``_write`` key to
each raw namespace that lists which keys are writable or not.
In general, this means they are compatible with the `setinfo`
function of filesystem objects.
Arguments:
namespace (str): A namespace identifier.
key (str): A key within the namespace.
Expand Down
8 changes: 4 additions & 4 deletions fs/osfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,10 +665,10 @@ def setinfo(self, path, info):
if "details" in info:
details = info["details"]
if "accessed" in details or "modified" in details:
_accessed = typing.cast(int, details.get("accessed"))
_modified = typing.cast(int, details.get("modified", _accessed))
accessed = int(_modified if _accessed is None else _accessed)
modified = int(_modified)
_accessed = typing.cast(float, details.get("accessed"))
_modified = typing.cast(float, details.get("modified", _accessed))
accessed = float(_modified if _accessed is None else _accessed)
modified = float(_modified)
if accessed is not None or modified is not None:
with convert_os_errors("setinfo", path):
os.utime(sys_path, (accessed, modified))
Expand Down
26 changes: 16 additions & 10 deletions fs/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import io
import itertools
import json
import math
import os
import time
import unittest
Expand Down Expand Up @@ -1171,15 +1170,21 @@ def test_removetree_root(self):

def test_setinfo(self):
self.fs.create("birthday.txt")
now = math.floor(time.time())
now = time.time()

change_info = {"details": {"accessed": now + 60, "modified": now + 60 * 60}}
self.fs.setinfo("birthday.txt", change_info)
new_info = self.fs.getinfo("birthday.txt", namespaces=["details"]).raw
if "accessed" in new_info.get("_write", []):
self.assertEqual(new_info["details"]["accessed"], now + 60)
if "modified" in new_info.get("_write", []):
self.assertEqual(new_info["details"]["modified"], now + 60 * 60)
new_info = self.fs.getinfo("birthday.txt", namespaces=["details"])
can_write_acccess = new_info.is_writeable("details", "accessed")
can_write_modified = new_info.is_writeable("details", "modified")
if can_write_acccess:
self.assertAlmostEqual(
new_info.get("details", "accessed"), now + 60, places=4
)
if can_write_modified:
self.assertAlmostEqual(
new_info.get("details", "modified"), now + 60 * 60, places=4
)

with self.assertRaises(errors.ResourceNotFound):
self.fs.setinfo("nothing", {})
Expand All @@ -1188,10 +1193,11 @@ def test_settimes(self):
self.fs.create("birthday.txt")
self.fs.settimes("birthday.txt", accessed=datetime(2016, 7, 5))
info = self.fs.getinfo("birthday.txt", namespaces=["details"])
writeable = info.get("details", "_write", [])
if "accessed" in writeable:
can_write_acccess = info.is_writeable("details", "accessed")
can_write_modified = info.is_writeable("details", "modified")
if can_write_acccess:
self.assertEqual(info.accessed, datetime(2016, 7, 5, tzinfo=pytz.UTC))
if "modified" in writeable:
if can_write_modified:
self.assertEqual(info.modified, datetime(2016, 7, 5, tzinfo=pytz.UTC))

def test_touch(self):
Expand Down
3 changes: 2 additions & 1 deletion tests/test_osfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ def test_copy_preserve_time(self):
self.assertTrue(self.fs.exists("bar/file.txt"))

dst_info = self.fs.getinfo("bar/file.txt", namespaces)
self.assertEqual(dst_info.modified, src_info.modified)
delta = dst_info.modified - src_info.modified
self.assertAlmostEqual(delta.total_seconds(), 0, places=2)

@unittest.skipUnless(osfs.sendfile, "sendfile not supported")
@unittest.skipIf(
Expand Down

0 comments on commit 2d0ffc3

Please sign in to comment.