summaryrefslogtreecommitdiffstats
path: root/Tools/scripts/reindent-rst.py
blob: 246af3ef81da60dcd812f85fb92e52f1de243cc7 (plain)
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 python3

# Make a reST file compliant to our pre-commit hook.
# Currently just remove trailing whitespace.


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 as err:
            print('Cannot fix %s: %s' % (filename, err))
            rv = 1
    return rv

if __name__ == '__main__':
    sys.exit(main())