From f52c0cdb4aec4975263347dfdb90e80e24d77670 Mon Sep 17 00:00:00 2001 From: urbanware-org Date: Mon, 17 Dec 2018 21:59:27 +0100 Subject: [PATCH] Initial upload --- LICENSE.md | 27 +++++++++++++++++++++++ README.md | 47 ++++++++++++++++++++++++++++++++++++++++ space-remover.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 LICENSE.md create mode 100644 README.md create mode 100755 space-remover.py diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..6523312 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,27 @@ +# License + +***space-remover*** - Clean up unnecessary spaces in multiple files + +Copyright © 2018 by Ralf Kilian + +Distributed under the *MIT License*: + +``` +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +``` diff --git a/README.md b/README.md new file mode 100644 index 0000000..f33fbea --- /dev/null +++ b/README.md @@ -0,0 +1,47 @@ +# *space-remover* + +**Table of contents** +* [Definition](#definition) +* [Details](#details) +* [Usage](#usage) +* [Contact](#contact) + +---- + +## Definition + +Simple *Python* script to remove trailing spaces and tabs from files. + +[Top](#space-remover) + +## Details + +There often are code or config files that contain trailing spaces and tabs at the end of lines as well as in blank lines. This script will remove them without removing any blank lines themselves. + +The code was written on *Linux* and has only been tested on that platform, yet. + +[Top](#space-remover) + +## Usage + +Please backup the data before using this script. + +So, to remove all unnecessary spaces inside all *Python* files inside the directory `/tmp/project` as well as its sub-directories, type: + +```bash +$ find /tmp/project -type f | grep "\.py$" > /tmp/foobar.tmp +$ while read line; do + ./space-remover.py "$line" +$ done < /tmp/foobar.tmp +$ rm -f /tmp/foobar.tmp +``` + +[Top](#space-remover) + +## Contact + +Any suggestions, questions, bugs to report or feedback to give? + +You can contact me by sending an email to . + +[Top](#space-remover) diff --git a/space-remover.py b/space-remover.py new file mode 100755 index 0000000..7ed112e --- /dev/null +++ b/space-remover.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python + +# ============================================================================ +# space-remover - Script to remove trailing spaces and tabs from files +# Copyright (C) 2018 by Ralf Kilian +# Distributed under the MIT License (https://opensource.org/licenses/MIT) +# +# GitHub: https://github.com/urbanware-org/space-remover +# GitLab: https://gitlab.com/urbanware-org/space-remover +# ============================================================================ + +__version__ = "1.1.0" + +import os +import shutil +import sys +import tempfile + +temp = "spacerem_" + str(os.getpid()) + ".tmp" + +file_in = sys.argv[1] +file_out = os.path.join(tempfile.gettempdir(), temp) + +if not os.path.exists(file_in): + print "error: The file '%s' does not exist" % file_in + sys.exit(1) + +if os.path.isfile(file_in): + fh_input = open(file_in, "r") + fh_output = open(file_out, "w") + + for line in fh_input: + # Remove tabs at the end of lines + while "\t\n" in line: + line = line.replace("t\n", "\n") + while "\t\r\n" in line: + line = line.replace("\t\r\n", "\r\n") + + # Remove spaces at the end of lines + while " \n" in line: + line = line.replace(" \n", "\n") + while " \r\n" in line: + line = line.replace(" \r\n", "\r\n") + + # Write output + fh_output.write(line) + + fh_input.close() + fh_output.close() + + shutil.move(file_out, file_in) +else: + print "error: The path '%s' is not a file" % file_in + sys.exit(1) + +# EOF