[Python-checkins] r70656 - python/trunk/Tools/scripts/reindent-rst.py

georg.brandl python-checkins at python.org
Sat Mar 28 20:33:34 CET 2009


Author: georg.brandl
Date: Sat Mar 28 20:33:33 2009
New Revision: 70656

Log:
Add a script to fixup rst files if the pre-commit hook rejects them.


Added:
   python/trunk/Tools/scripts/reindent-rst.py   (contents, props changed)

Added: python/trunk/Tools/scripts/reindent-rst.py
==============================================================================
--- (empty file)
+++ python/trunk/Tools/scripts/reindent-rst.py	Sat Mar 28 20:33:33 2009
@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+
+# Make a reST file compliant to our pre-commit hook.
+# Currently just remove trailing whitespace.
+
+from __future__ import with_statement
+import sys, re, shutil
+
+ws_re = re.compile(r'\s+(\r?\n)$')
+
+def main(argv=sys.argv):
+    rv = 0
+    for filename in argv[1:]:
+        try:
+            with open(filename, 'rb') as f:
+                lines = f.readlines()
+            new_lines = [ws_re.sub(r'\1', line) for line in lines]
+            if new_lines != lines:
+                print 'Fixing %s...' % filename
+            shutil.copyfile(filename, filename + '.bak')
+            with open(filename, 'wb') as f:
+                f.writelines(new_lines)
+        except Exception, err:
+            print 'Cannot fix %s: %s' % (filename, err)
+            rv = 1
+    return rv
+
+if __name__ == '__main__':
+    sys.exit(main())


More information about the Python-checkins mailing list