mirror of https://github.com/fail2ban/fail2ban
ENH: _copy_lines_between_files -- read all needed, and only then write/flush at once
I think this is the one which should resolve https://github.com/fail2ban/fail2ban/issues/103pull/154/head
parent
ba042c6e3a
commit
7064a411c2
|
@ -105,20 +105,23 @@ def _copy_lines_between_files(fin, fout, n=None, skip=0, mode='a', terminal_line
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
if isinstance(fin, str): # pragma: no branch - only used with str in test cases
|
if isinstance(fin, str): # pragma: no branch - only used with str in test cases
|
||||||
fin = open(fin, 'r')
|
fin = open(fin, 'r')
|
||||||
if isinstance(fout, str):
|
|
||||||
fout = open(fout, mode)
|
|
||||||
# Skip
|
# Skip
|
||||||
for i in xrange(skip):
|
for i in xrange(skip):
|
||||||
_ = fin.readline()
|
_ = fin.readline()
|
||||||
# Read/Write
|
# Read
|
||||||
i = 0
|
i = 0
|
||||||
|
lines = []
|
||||||
while n is None or i < n:
|
while n is None or i < n:
|
||||||
l = fin.readline()
|
l = fin.readline()
|
||||||
if terminal_line is not None and l == terminal_line:
|
if terminal_line is not None and l == terminal_line:
|
||||||
break
|
break
|
||||||
fout.write(l)
|
lines.append(l)
|
||||||
fout.flush()
|
|
||||||
i += 1
|
i += 1
|
||||||
|
# Write: all at once and flush
|
||||||
|
if isinstance(fout, str):
|
||||||
|
fout = open(fout, mode)
|
||||||
|
fout.write('\n'.join(lines))
|
||||||
|
fout.flush()
|
||||||
# to give other threads possibly some time to crunch
|
# to give other threads possibly some time to crunch
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
return fout
|
return fout
|
||||||
|
|
Loading…
Reference in New Issue