Skip to content

Commit

Permalink
Initial upload
Browse files Browse the repository at this point in the history
  • Loading branch information
urbanware-org committed Dec 17, 2018
0 parents commit f52c0cd
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
27 changes: 27 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -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.
```
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 <dev@urbanware.org>.

[Top](#space-remover)
56 changes: 56 additions & 0 deletions space-remover.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f52c0cd

Please sign in to comment.