| 1 |
From: =?utf-8?q?Pierre-Elliott_B=C3=A9cue?= <[email protected]> |
| 2 |
Date: Sat, 20 Jul 2019 20:34:15 +0200 |
| 3 |
Subject: Sleep before modifying first_path otherwise the mtime isn't updated |
| 4 |
|
| 5 |
It seems that the mtime handling by python acts weirdly. |
| 6 |
|
| 7 |
First, as this snippet shows, the mtime is lower than the initial time |
| 8 |
although the file got modified after. |
| 9 |
|
| 10 |
""" |
| 11 |
import os |
| 12 |
import time |
| 13 |
|
| 14 |
def test_mtime(): |
| 15 |
_s = time.time() |
| 16 |
with open("/tmp/test", "w") as testfile: |
| 17 |
testfile.write("") |
| 18 |
_t = os.path.getmtime("/tmp/test") |
| 19 |
print(repr(_s)) |
| 20 |
print(repr(_t)) |
| 21 |
|
| 22 |
In [4]: test_mtime() |
| 23 |
1563311822.154947 |
| 24 |
1563311822.153427 |
| 25 |
""" |
| 26 |
|
| 27 |
Second, as this snippet shows, it doesn't update in each and every |
| 28 |
condition. |
| 29 |
|
| 30 |
""" |
| 31 |
In [35]: def test_mtime(): |
| 32 |
...: with open("/tmp/test_mtime", "w") as myfile: |
| 33 |
...: myfile.write("") |
| 34 |
...: print(repr(os.path.getmtime("/tmp/test_mtime"))) |
| 35 |
...: time.sleep(1) |
| 36 |
...: with open("/tmp/test_mtime", "a") as myfile: |
| 37 |
...: myfile.write("") |
| 38 |
...: os.sync() |
| 39 |
...: time.sleep(1) |
| 40 |
...: print(repr(os.path.getmtime("/tmp/test_mtime"))) |
| 41 |
...: |
| 42 |
|
| 43 |
In [36]: test_mtime() |
| 44 |
1563349885.8835335 |
| 45 |
1563349885.8835335 |
| 46 |
""" |
| 47 |
|
| 48 |
Sleeping right before the file modification in the test seems enough, |
| 49 |
but it's a messy solution. The bug has been reported upstream, and |
| 50 |
upstream should consider relying definitely on pyinotify instead of |
| 51 |
trying to do something with a custom class. |
| 52 |
--- |
| 53 |
tests/test_watcher.py | 2 ++ |
| 54 |
1 file changed, 2 insertions(+) |
| 55 |
|
| 56 |
--- a/tests/test_watcher.py |
| 57 |
+++ b/tests/test_watcher.py |
| 58 |
@@ -144,6 +144,8 @@ |
| 59 |
assert watcher.examine() == (second_path, None) |
| 60 |
assert watcher.examine() == (None, None) |
| 61 |
|
| 62 |
+ # Without a sufficient wait, the mtime isn't updated... |
| 63 |
+ time.sleep(1) |
| 64 |
with open(first_path, 'a') as f: |
| 65 |
f.write('foo') |
| 66 |
assert watcher.examine() == (first_path, None) |