blob: c13d70f7a6eb43aa93ea51ff25c1b4c0c1ee3b9a (
plain)
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
|
import collections
import os
import re
import time
def watch(folders, on_change, pattern=None, sleep_time=0.1):
pattern = re.compile(pattern) if pattern else None
watched = collections.defaultdict(lambda: -1.0)
def walk():
walked = []
for folder in folders:
for current, _, files in os.walk(folder):
for f in files:
if pattern and not pattern.search(f):
continue
path = os.path.join(current, f)
info = os.stat(path)
new_time = info.st_mtime
if new_time > watched[path] > 0:
on_change(path, new_time, False)
watched[path] = new_time
walked.append(path)
# Look for deleted files
for w in [x for x in watched.keys() if x not in walked]:
del watched[w]
on_change(w, -1, True)
while True:
walk()
time.sleep(sleep_time)
|