File: reindent-rst.py

package info (click to toggle)
python2.6 2.6.6-8
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 65,276 kB
  • ctags: 106,965
  • sloc: ansic: 389,033; python: 375,783; asm: 9,734; sh: 4,934; makefile: 4,120; lisp: 2,933; objc: 775; xml: 62
file content (29 lines) | stat: -rwxr-xr-x 841 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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())