diff --git a/scripts/process-text-files.py b/scripts/process-text-files.py index bb14c37..96e6214 100644 --- a/scripts/process-text-files.py +++ b/scripts/process-text-files.py @@ -76,6 +76,19 @@ class Cmd(jwutils.Cmd): return data return r + @staticmethod + def _cleanup_spaces(data, src, target, context=None): + lines = data.splitlines() + last = len(lines) - 1 + r = '' + while last >= 0 and len(lines[last].strip()) == 0: + last -= 1 + i = 0 + while i <= last: + r += lines[i].rstrip() + '\n' + i += 1 + return r + @staticmethod def _replace_cpp_symbol(data, src, target, context=None): stopc = "^a-zA-Z0-9_" @@ -313,6 +326,7 @@ class CmdIndentMakefileEquals(Cmd): return p def process(self, args, files): + slog(NOTICE, "Beautifying", len(files), "makefiles:") context = dict() right_align_match = 2 context["indent"] = args.equal_pos - right_align_match @@ -323,11 +337,30 @@ class CmdIndentMakefileEquals(Cmd): context["skip-short"] = args.skip_short context["min-assignments"] = args.min_assignments replacements = {"blah": "blub"} # just a dummy to use _replace_in_file, TODO: obviate the need - slog(NOTICE, "indenting equal signs in", len(files), "makefiles:") for dir, name in files: path = dir + '/' + name + if self._replace_in_file(path, replacements, func=self._cleanup_spaces): + slog(NOTICE, "+ purged spaces :", path) if self._replace_in_file(path, replacements, func=self._indent_pattern, context=context): - slog(NOTICE, "+ changed", path) + slog(NOTICE, "+ aligned equals :", path) + +class CmdCleanupSpaces(Cmd): + + def __init__(self): + super(CmdCleanupSpaces, self).__init__("cleanup-spaces", "Remove trailing empty lines") + + def add_parser(self, parsers): + p = super(CmdCleanupSpaces, self).add_parser(parsers) + return p + + def process(self, args, files): + slog(NOTICE, "Cleaning up unnecessary space in", len(files), "files:") + context = dict() + replacements = {"blah": "blub"} # just a dummy to use _replace_in_file, TODO: obviate the need + for dir, name in files: + path = dir + '/' + name + if self._replace_in_file(path, replacements, func=self._cleanup_spaces, context=context): + slog(NOTICE, "+ purged spaces :", path) class CmdAddCppNamespace(Cmd):