Benjamin Peterson | ded31c4 | 2009-03-30 15:04:16 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Make a reST file compliant to our pre-commit hook. |
| 4 | # Currently just remove trailing whitespace. |
| 5 | |
Georg Brandl | 17b9e27 | 2009-03-31 22:46:50 +0000 | [diff] [blame] | 6 | |
Benjamin Peterson | ded31c4 | 2009-03-30 15:04:16 +0000 | [diff] [blame] | 7 | import sys, re, shutil |
| 8 | |
| 9 | ws_re = re.compile(r'\s+(\r?\n)$') |
| 10 | |
| 11 | def main(argv=sys.argv): |
| 12 | rv = 0 |
| 13 | for filename in argv[1:]: |
| 14 | try: |
| 15 | with open(filename, 'rb') as f: |
| 16 | lines = f.readlines() |
| 17 | new_lines = [ws_re.sub(r'\1', line) for line in lines] |
| 18 | if new_lines != lines: |
Georg Brandl | 17b9e27 | 2009-03-31 22:46:50 +0000 | [diff] [blame] | 19 | print('Fixing %s...' % filename) |
Benjamin Peterson | ded31c4 | 2009-03-30 15:04:16 +0000 | [diff] [blame] | 20 | shutil.copyfile(filename, filename + '.bak') |
| 21 | with open(filename, 'wb') as f: |
| 22 | f.writelines(new_lines) |
Georg Brandl | 17b9e27 | 2009-03-31 22:46:50 +0000 | [diff] [blame] | 23 | except Exception as err: |
| 24 | print('Cannot fix %s: %s' % (filename, err)) |
Benjamin Peterson | ded31c4 | 2009-03-30 15:04:16 +0000 | [diff] [blame] | 25 | rv = 1 |
| 26 | return rv |
| 27 | |
| 28 | if __name__ == '__main__': |
| 29 | sys.exit(main()) |