-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathfsinspector.py
More file actions
52 lines (44 loc) · 1.39 KB
/
fsinspector.py
File metadata and controls
52 lines (44 loc) · 1.39 KB
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
48
49
50
51
52
import logging
import os
import time
from shutil import rmtree
logger = logging.getLogger("behaving")
class FSInspector(object):
def __init__(self, path, timeout=5):
self.path = path
self.timeout = timeout
def messages_for_user(self, user):
user_path = os.path.join(self.path, user)
try:
root, dirs, paths = next(os.walk(user_path))
except StopIteration:
return []
if paths:
paths.sort()
messages = []
for path in paths:
path = os.path.join(user_path, path)
with open(path, "r") as f:
messages.append(f.read())
return messages
def user_messages(self, user, f=None):
messages = []
start = time.time()
while time.time() - start < self.timeout:
messages = list(filter(f, self.messages_for_user(user)))
if messages:
break
time.sleep(0.2)
return messages
def clear(self):
dirs = os.listdir(self.path)
for dir_path in dirs:
fn = os.path.join(self.path, dir_path)
try:
if os.path.isdir(fn):
rmtree(os.path.join(self.path, dir_path))
else:
os.unlink(fn)
except OSError:
logger.error("Could not delete %s", fn)
exit(1)