Skip to content
Snippets Groups Projects
Commit 683e3143 authored by Campbell Barton's avatar Campbell Barton
Browse files

trailing_space_clean: remove BOM as well as trailing space

parent 82e4b979
Branches
Tags
No related merge requests found
...@@ -16,6 +16,8 @@ SOURCE_EXT = ( ...@@ -16,6 +16,8 @@ SOURCE_EXT = (
".py", ".py",
# Text (also CMake) # Text (also CMake)
".txt", ".cmake", ".rst", ".txt", ".cmake", ".rst",
# MS-Windows Scripts.
".bat", ".cmd",
) )
...@@ -47,26 +49,36 @@ def path_expand(paths, filename_check=None): ...@@ -47,26 +49,36 @@ def path_expand(paths, filename_check=None):
def rstrip_file(filename): def rstrip_file(filename):
reports = []
with open(filename, "r", encoding="utf-8") as fh: with open(filename, "r", encoding="utf-8") as fh:
data_src = fh.read() data_src = fh.read()
# Strip trailing space.
data_dst = [] data_dst = []
for l in data_src.rstrip().splitlines(True): for l in data_src.rstrip().splitlines(True):
data_dst.append(l.rstrip() + "\n") data_dst.append(l.rstrip() + "\n")
data_dst = "".join(data_dst) data_dst = "".join(data_dst)
# Remove BOM.
if data_dst and (data_dst[0] == '\ufeff'):
data_dst = data_dst[1:]
len_strip = len(data_src) - len(data_dst) len_strip = len(data_src) - len(data_dst)
if len_strip != 0: if len_strip != 0:
reports.append("STRIP=%d" % len_strip)
if len_strip:
with open(filename, "w", encoding="utf-8") as fh: with open(filename, "w", encoding="utf-8") as fh:
fh.write(data_dst) fh.write(data_dst)
return len_strip return tuple(reports)
def main(): def main():
for f in path_expand(PATHS, is_source): for f in path_expand(PATHS, is_source):
len_strip = rstrip_file(f) report = rstrip_file(f)
if len_strip != 0: if report:
print(f"Strip ({len_strip}): {f}") print("Strip (%s): %s" % (', '.join(report), f))
if __name__ == "__main__": if __name__ == "__main__":
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment