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