From: Janek WarchoĊ‚ Date: Mon, 23 Sep 2013 22:52:10 +0000 (+0200) Subject: scripts: improve strip-whitespace.py X-Git-Tag: release/2.17.29-1~17 X-Git-Url: https://git.donarmstrong.com/?p=lilypond.git;a=commitdiff_plain;h=ca6e130e62a717b99db393f7262c9b34cb3d127d 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. --- 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()