-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScanner.py
213 lines (180 loc) · 7.59 KB
/
Scanner.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from pythreader import PyThread, synchronized, Primitive
from threading import Event
from tools import runCommand
import time, fnmatch, re, traceback, sys
from logs import Logged
class FileDescriptor(object):
def __init__(self, server, location, path, name, size):
self.Server = server
self.Location = location
self.Path = path
self.Name = name
self.Size = size
assert path.startswith(location)
relpath = path[len(location):]
while relpath and relpath[0] == "/":
relpath = relpath[1:]
self.Relpath = relpath # path relative to the location root, with leading slash removed
def __str__(self):
return "%s:%s" % (self.Server, self.Path)
__repr__ = __str__
class ScanManager(Primitive, Logged):
def __init__(self, manager, history_db, config, held):
Primitive.__init__(self)
Logged.__init__(self, "ScanManager")
self.ServersLocations = config.ScanServersLocations # [ (server, location), ... ]
self.StaggerInterval = float(config.ScanInterval)/len(self.ServersLocations)
self.Scanners = {} # [(server, location) -> Scanner]
self.Manager = manager
self.Config = config
self.Held = held
self.HistoryDB = history_db
@synchronized
def start(self):
nscanners = len(self.ServersLocations)
dt = self.StaggerInterval/len(self.ServersLocations)
for i, (server, location) in enumerate(self.ServersLocations):
if i > 0: time.sleep(dt)
s = Scanner(self.Manager, self.HistoryDB, server, location, self.Config)
self.Scanners[(server, location)] = s
if self.Held: s.hold()
s.start()
@synchronized
def hold(self):
self.Held = True
for s in self.Scanners.values():
s.hold()
@synchronized
def release(self):
self.Held = False
for s in self.Scanners.values():
s.release()
@synchronized
def needScan(self):
for s in self.Scanners.values():
s.needScan()
@synchronized
def listFiles(self, timeout):
out = [
(server, location) + scanner.listFiles(timeout)
for (server, location), scanner in self.Scanners.items()
]
return sorted(out)
class Scanner(PyThread, Logged):
PrescaleMultiplier = 10000
def __init__(self, manager, history_db, server, location, config):
PyThread.__init__(self)
Logged.__init__(self, name=f"Scanner({server}:{location})")
self.Manager = manager
self.HistoryDB = history_db
self.Server, self.Location = server, location
self.FilenamePatterns = config.FilenamePatterns
self.lsCommandTemplate = config.lsCommandTemplate\
.replace("$server", self.Server)
self.ScanInterval = config.ScanInterval
self.DirectoryRE = re.compile(config.DirectoryRE) if config.DirectoryRE else None
self.ParseRE = re.compile(config.ParseRE)
self.FileRE = re.compile(config.FileRE)
self.PathRE = re.compile(config.PathRE)
self.SizeRE = re.compile(config.SizeRE)
self.Recursive = config.ScanRecursive
self.PrescaleFactor = int(config.ScanPrescale*self.PrescaleMultiplier) # 100 means send all files
self.PrescaleSalt = config.PrescaleSalt
self.OperationTimeout = config.ScannerOperationTimeout
self.Held = False
self.Stop = False
self.debug("initiated")
def stop(self):
self.Stop = True
self.wakeup()
def passesPrescale(self, fn):
return (hash(fn + self.PrescaleSalt) % self.PrescaleMultiplier) < self.PrescaleFactor
@synchronized
def scan(self):
status, error, file_descs = self.listFilesUnder(self.Location)
if status:
self.log("Scanner subprocess status code:", status, " error:", error)
return [], error
meta_names = {desc.Name[:-5] for desc in file_descs
if desc.Name.endswith(".json")
}
self.log("found files (data and metadata):", len(file_descs), " metadata:", len(meta_names))
out = [desc for desc in file_descs
if desc.Name in meta_names
and any(fnmatch.fnmatch(desc.Name, pattern) for pattern in self.FilenamePatterns)
]
return out, None
def listFilesUnder(self, location):
out = []
status, error, files, dirs = self.listFilesAndDirs(location, self.OperationTimeout)
#self.debug("Files: %s" % (files,))
#self.debug("Dirs: %s" % (dirs,))
if status == 0:
out += files
if self.Recursive:
for path in dirs:
st, err, files = self.listFilesUnder(path)
if st == 0:
out += files
return status, error, out
def listFilesAndDirs(self, location, timeout):
lscommand = self.lsCommandTemplate.replace("$location", location).replace("$server", self.Server)
files = []
dirs = []
error = ""
status, out = runCommand(lscommand, timeout)
if status:
error = out
self.log("Error in ls (%s): %s" % (lscommand, error,))
else:
lines = [x.strip() for x in out.split("\n")]
for l in lines:
l = l.strip()
if l:
m = self.ParseRE.match(l)
if m:
t = m["type"]
path = m["path"]
if t in "f-":
size = int(m["size"])
name = path.rsplit("/",1)[-1]
path = path if path.startswith(location) else location + "/" + path
files.append(FileDescriptor(self.Server, location, path, name, size))
elif t == "d":
path = path if path.startswith(location) else location + "/" + path
dirs.append(path)
else:
print(f"Unknown directory entry type '{t}' in: {l} -- ignored")
else:
print("can not parse ls line:", l)
return status, error, files, dirs
@synchronized
def listFiles(self, timeout):
status, error, files = self.listFilesUnder(self.Location)
return status, error, files
def needScan(self):
self.wakeup()
def hold(self):
self.log("held")
self.Held = True
def release(self):
self.log("released")
self.Held = False
self.wakeup()
def run(self):
while not self.Stop:
if not self.Held and not self.Stop:
try:
descs, error = self.scan()
except Exception as e:
descs = []
error = "scan() error: " + traceback.format_exc()
self.error(error)
else:
if error:
self.error(error)
else:
nnew = self.Manager.addFiles(descs)
self.log("found matching data+metadata pairs:", len(descs), "files, new:", nnew)
self.HistoryDB.add_scanner_record(self.Server, self.Location, time.time(), len(descs), nnew)
self.sleep(self.ScanInterval)