-
Notifications
You must be signed in to change notification settings - Fork 0
/
filewatch.py
47 lines (33 loc) · 1.2 KB
/
filewatch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/python
#-*- coding: latin-1 -*-
"""Watch if a file is changed. Used for automatic reloading."""
import threading
import os
import time
import sys
def dosomething():
print "filechange!"
sys.stdout.flush()
class FileChangeNotifier(threading.Thread):
def __init__(self, filename, callback = dosomething, delay = 2):
threading.Thread.__init__(self)
self.filename = filename
self.callback = callback
self.delay = delay
self.s = os.stat(self.filename)
self.keeprunning = True
def run(self):
while self.keeprunning:
s = os.stat(self.filename)
if s.st_mtime > self.s.st_mtime:
self.s = os.stat(self.filename)
time.sleep(self.delay)
# horrible hack to check if transmission is finished
while(s.st_mtime != self.s.st_mtime):
time.sleep(self.delay)
self.callback()
time.sleep(1)
time.sleep(0.2)
if __name__ == '__main__':
d = FileChangeNotifier('test')
d.start()