Files
allwpilib/styleguide/newline.py
Tyler Veness aafca4ed7f Reduced duplication between formatting scripts with Task base class (#80)
Also added scripts for EOF newline management and for removing trailing whitespace. configure.bat was rewritten to use CRLF line endings. Documentation for the existing scripts was also improved.
2016-07-10 08:33:27 -07:00

36 lines
1.1 KiB
Python

# This task ensures that all source files have exactly one EOF newline.
import os
from task import Task
class Newline(Task):
def run(self, name):
newlines = 0
# Remove all but one EOF newline, or append one if necessary
eol = os.linesep
if name.endswith("bat"):
eol = "\r\n"
with open(name, "r+", newline = eol) as file:
# Get file size
file.seek(0, os.SEEK_END)
size = file.tell()
# Seek to last character in file
file.seek(size - 1)
# While last character is a newline
while file.read(1) == "\n":
newlines = newlines + 1
# Seek to character before newline
file.seek(size - 1 - len(eol) * newlines)
if newlines < 1:
# Append newline to end of file
file.seek(size)
file.write("\n")
elif newlines > 1:
# Truncate all but one newline
file.truncate(size - len(eol) * (newlines - 1))