mirror of
https://github.com/galaxyproject/galaxy.git
synced 2026-09-24 16:30:27 +08:00
Use byte replacement strategy
This commit is contained in:
@@ -112,8 +112,24 @@ def convert_newlines(fname, in_place=True, tmp_dir=None, tmp_prefix="gxupload",
|
||||
i = 0
|
||||
with io.open(fd, mode="wb") as fp:
|
||||
with io.open(fname, mode="rb") as fi:
|
||||
for i, line in enumerate(fi):
|
||||
fp.write(b"%s\n" % line.rstrip(b"\r\n"))
|
||||
line = b''
|
||||
clean_line = b""
|
||||
last_char = None
|
||||
block = fi.read(block_size)
|
||||
while block:
|
||||
if last_char == "\r" and block.startswith(b"\n"):
|
||||
# last block ended with "\r", new block startswith "\n"
|
||||
# since we replace "\r" with "\n" in the previous iteration we skip the first byte
|
||||
block = block[1:]
|
||||
# splitlines(True) splits at line terminators but keeps them so we can replace them
|
||||
lines = block.splitlines(True)
|
||||
for line in lines:
|
||||
clean_line = line.replace(b"\r\n", b"\n").replace(b"\r", b"\n")
|
||||
fp.write(clean_line)
|
||||
last_char = util.unicodify(line, error='replace')[-1]
|
||||
block = fi.read(block_size)
|
||||
if not clean_line.endswith(b"\n"):
|
||||
fp.write(b"\n")
|
||||
if in_place:
|
||||
shutil.move(temp_name, fname)
|
||||
# Return number of lines in file.
|
||||
|
||||
Reference in New Issue
Block a user