-
Notifications
You must be signed in to change notification settings - Fork 9
/
cat.py
executable file
·420 lines (299 loc) · 11.6 KB
/
cat.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#!/usr/bin/env python2
# coding: utf-8
import errno
import logging
import os
import stat
import time
import __main__
from pykit import config
from pykit import portlock
from pykit import utfjson
from . import fsutil
logger = logging.getLogger(__name__)
no_data_timeout = 3600
read_size = 1024 * 1024 * 16
file_check_time_range = (0.5, 5.0)
stat_dir = config.cat_stat_dir or '/tmp'
SEEK_START = 'start'
SEEK_END = 'end'
class CatError(Exception):
pass
class NoData(CatError):
pass
class NoSuchFile(CatError):
pass
class LockTimeout(CatError):
pass
class FakeLock(object):
def __enter__(self):
return self
def __exit__(self, *args, **kwargs):
return
class Cat(object):
def __init__(self, fn,
handler=None,
file_end_handler=None,
exclusive=True,
id=None,
strip=False,
read_chunk_size=read_size):
self.fn = fn
self.handler = handler
self.file_end_handler = file_end_handler
self.exclusive = exclusive
if id is None:
if hasattr(__main__, '__file__'):
id = __main__.__file__
id = os.path.basename(id)
if id == '<stdin>':
id = '__stdin__'
else:
id = '__instant_command__'
self.id = id
self.strip = strip
read_chunk_size = int(read_chunk_size)
if read_chunk_size <= 0:
read_chunk_size = read_size
self.read_chunk_size = read_chunk_size
self.running = None
self.bufferred = None # (offset, content)
if (self.handler is not None
and callable(self.handler)):
self.handler = [self.handler]
def cat(self, timeout=None, default_seek=None):
for line in self.iterate(timeout=timeout, default_seek=default_seek):
for h in self.handler:
try:
h(line)
except Exception as e:
logger.exception(repr(e)
+ ' while handling {line}'.format(line=repr(line)))
def iterate(self, timeout=None, default_seek=None):
self.running = True
try:
for x in self._iter(timeout, default_seek):
yield x
finally:
self.running = False
def _iter(self, timeout, default_seek):
if timeout == 0:
# timeout at once after one read, if there is no more data in file.
# set to -1 to prevent possible slight time shift.
timeout = -1
if self.exclusive:
lck = portlock.Portlock(self.lock_name(), timeout=0.1)
else:
lck = FakeLock()
try:
with lck:
for x in self._nolock_iter(timeout, default_seek):
yield x
except portlock.PortlockTimeout:
raise LockTimeout(self.id, self.fn, 'other Cat() has been holding this lock')
def _nolock_iter(self, timeout, default_seek):
if timeout is None:
timeout = no_data_timeout
expire_at = time.time() + timeout
while True:
# NOTE: Opening a file and waiting for new data in it does not work.
#
# It has to check for file overriding on fs periodically.
# Or it may happens that it keeps waiting for new data on a deleted
# but opened file, while new data is actually written into a new
# file with the same path.
#
# Thus we check if file changed for about 5 times(by reopening it).
read_timeout = (expire_at - time.time()) / 5.0
if read_timeout < file_check_time_range[0]:
read_timeout = file_check_time_range[0]
if read_timeout > file_check_time_range[1]:
read_timeout = file_check_time_range[1]
f = self.wait_open_file(timeout=expire_at - time.time())
with f:
try:
for x in self.iter_to_file_end(
f, read_timeout, default_seek):
yield x
# re-new expire_at if there is any data read.
expire_at = time.time() + timeout
if time.time() > expire_at:
# caller expect to return at once when it has read once,
# timeout < 0
# When timeout, it means no more data will be appended.
# Thus the bufferred must be a whole line, even there is
# not a trailing '\n' presents
if self.bufferred is not None:
l = self.bufferred[1]
self.bufferred = None
yield l
return
except NoData as e:
# NoData raises only when there is no data yield.
logger.info(repr(e) + ' while cat: {fn}'.format(fn=self.fn))
if time.time() > expire_at:
# raise last NoData
raise
def wait_open_file(self, timeout):
expire_at = time.time() + timeout
sleep_time = 0.01
max_sleep_time = 1
f = self._try_open_file()
if f is not None:
return f
logger.info('file not found: {fn}'.format(fn=self.fn))
while time.time() < expire_at:
f = self._try_open_file()
if f is not None:
return f
sl = min([sleep_time, expire_at - time.time()])
logger.debug('file not found: {fn}, sleep for {sl}'.format(fn=self.fn, sl=sl))
time.sleep(sl)
sleep_time = min([sleep_time * 1.5, max_sleep_time])
logger.warn('file not found'
' while waiting for it to be present: {fn}'.format(fn=self.fn))
raise NoSuchFile(self.fn)
def iter_to_file_end(self, f, read_timeout, default_seek):
offset = self.get_last_offset(f, default_seek)
f.seek(offset)
logger.info('scan {fn} from offset: {offset}'.format(
fn=self.fn,
offset=offset))
for line in self.iter_lines(f, read_timeout):
logger.debug('yield:' + repr(line))
yield line
if self.file_end_handler is not None:
self.file_end_handler()
def wait_for_new_data(self, f, timeout):
# Before full_chunk_expire_at, wait for a full chunk data to be ready to
# maximize throughput.
# If time exceeds full_chunk_expire_at, return True if there is any data ready.
# insufficient chunk data return before read_timeout
full_chunk_timeout = min([timeout * 0.5, 1])
full_chunk_expire_at = time.time() + full_chunk_timeout
expire_at = time.time() + timeout
while True:
if f.tell() + self.read_chunk_size < _file_size(f):
return
if (f.tell() < _file_size(f)
and time.time() > full_chunk_expire_at):
return
if time.time() >= expire_at:
raise NoData()
time.sleep(0.05)
def stat_path(self):
return os.path.join(stat_dir, self.lock_name())
def lock_name(self):
name = os.path.realpath(self.fn)
name = 'fsutil_cat_lock_!' + self.id + '!'.join(name.split('/'))
return name
def read_last_stat(self):
cont = fsutil.read_file(self.stat_path())
if cont.startswith('{'):
return utfjson.load(cont)
# old format: TODO remove it
last = cont.strip().split(' ')
if len(last) != 3:
raise IOError('InvalidRecordFormat', last)
(lastino, lastsize, lastoff) = last
lastino = int(lastino)
lastoff = int(lastoff)
return {
"inode": lastino,
"offset": lastoff,
}
def write_last_stat(self, f, offset):
st = os.fstat(f.fileno())
ino = st[stat.ST_INO]
last = {
"inode": ino,
"offset": offset,
}
fsutil.write_file(self.stat_path(), utfjson.dump(last), fsync=False)
logger.info('position written fn=%s inode=%d offset=%d' % (
self.fn, ino, offset))
def get_last_offset(self, f, default_seek):
st = os.fstat(f.fileno())
ino = st[stat.ST_INO]
size = st[stat.ST_SIZE]
max_residual = None
if default_seek is None or default_seek == SEEK_START:
default_offset = 0
elif default_seek == SEEK_END:
default_offset = size
elif default_seek < 0:
max_residual = 0 - default_seek
default_offset = max(size - max_residual, 0)
else:
default_offset = default_seek
stat_file = self.stat_path()
if not os.path.isfile(stat_file):
return default_offset
try:
last = self.read_last_stat()
except (IOError, ValueError):
# damaged stat file
return default_offset
if max_residual is not None:
if size - last['offset'] > max_residual:
last['offset'] = size - max_residual
if last['inode'] != ino or last['offset'] > size:
return default_offset
return last['offset']
def iter_lines(self, f, read_timeout):
# raise NoData for the first time
self.wait_for_new_data(f, read_timeout)
while True:
offset = f.tell()
fsize = _file_size(f)
if offset >= fsize:
break
try:
while True:
# On Mac 17.5.0, x86_64, python 2.7.15/2.7.16:
# The second time calling f.readlines() returns empty list,
# even when in another thread something is appended.
# Thus we have to use f.readline(), manually deal with every
# line.
_line = f.readline(self.read_chunk_size)
if _line == "":
break
if self.bufferred is not None:
offset = self.bufferred[0]
_line = self.bufferred[1] + _line
self.bufferred = None
if not _line.endswith(('\r', '\n')):
self.bufferred = (offset, _line)
offset += len(_line)
continue
line = _line
if self.strip:
line = line.strip('\r\n')
offset += len(_line)
yield line
try:
# try to read a full chunk
self.wait_for_new_data(f, read_timeout)
except NoData:
pass
finally:
# `yield` might be interrupted by its caller. But we still need
# to record how much data has been returned to upper level.
self.write_last_stat(f, offset)
def _try_open_file(self):
try:
f = open(self.fn)
logger.info('file found and opened {fn}'.format(fn=self.fn))
return f
except IOError as e:
if e.errno == errno.ENOENT:
pass
return None
def reset_stat(self):
stat_path = self.stat_path()
if not os.path.isfile(stat_path):
return
os.remove(stat_path)
def _file_size(f):
st = os.fstat(f.fileno())
return st[stat.ST_SIZE]