From ca6e130e62a717b99db393f7262c9b34cb3d127d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Janek=20Warcho=C5=82?= Date: Tue, 24 Sep 2013 00:52:10 +0200 Subject: [PATCH] scripts: improve strip-whitespace.py previous version barfed when it was called on a directory (which could happen for example when called with a wildcard). Also, add comment. --- scripts/auxiliar/strip-whitespace.py | 34 ++++++++++++++++++---------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/scripts/auxiliar/strip-whitespace.py b/scripts/auxiliar/strip-whitespace.py index 10da1162c5..1d4a6e4ff3 100755 --- a/scripts/auxiliar/strip-whitespace.py +++ b/scripts/auxiliar/strip-whitespace.py @@ -1,16 +1,26 @@ #!/usr/bin/env python -import sys -for fname in sys.argv[1:]: - fd = open(fname,mode='U') # open in universal newline mode - lines = [] - for line in fd.readlines(): - lines.append( line.rstrip() ) - fd.close() +# This script removes trailing whitespace from files. +# It doesn't remove trailing newlines. +# As a side-effect, it converts line endings to Unix-style (LF). - fd = open(fname,mode='w') - fd.seek(0) - for line in lines: - fd.write(line+'\n') - fd.close() +import os, sys +# Iterate through all arguments. When the script is called +# with a wildcard (for example 'remove-trailing-whitespace.py *'), +# it's the *shell* that will expand the wildcard, and pass all +# resulting paths as arguments to the script. + +for pathname in sys.argv[1:]: + if os.path.isfile(pathname): + fd = open(pathname,mode='U') # open in universal newline mode + lines = [] + for line in fd.readlines(): + lines.append( line.rstrip() ) + fd.close() + + fd = open(pathname,mode='w') + fd.seek(0) + for line in lines: + fd.write(line+'\n') + fd.close() -- 2.39.2