]> git.donarmstrong.com Git - lilypond.git/commitdiff
scripts: improve strip-whitespace.py
authorJanek Warchoł <lemniskata.bernoullego@gmail.com>
Mon, 23 Sep 2013 22:52:10 +0000 (00:52 +0200)
committerJanek Warchoł <lemniskata.bernoullego@gmail.com>
Tue, 15 Oct 2013 21:16:47 +0000 (23:16 +0200)
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

index 10da1162c568cf6808baad254fe082c861d1ce5c..1d4a6e4ff378baa75e276c0276c4ec6d4969f5d4 100755 (executable)
@@ -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()