]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/env python | |
2 | # -*- coding: utf-8 -*- | |
3 | # 2017 by David Lamparter, placed in public domain | |
4 | ||
5 | import sys, re, subprocess, os | |
6 | ||
7 | # find all DEFUNs | |
8 | defun_re = re.compile( | |
9 | r'^((DEF(UN(|_ATTR|_CMD_(ELEMENT|FUNC_(DECL|TEXT))|_DEPRECATED|_NOSH|_HIDDEN|SH(|_ATTR|_DEPRECATED|_HIDDEN))?|PY|PY_ATTR|PY_HIDDEN)|ALIAS)\s*\(.*?)^(?=\s*\{)', | |
10 | re.M | re.S) | |
11 | define_re = re.compile( | |
12 | r'((^#\s*define[^\n]+[^\\]\n)+)', | |
13 | re.M | re.S) | |
14 | # find clang-format control that we just inserted | |
15 | clean_re = re.compile( | |
16 | r'^.*/\* \$FRR indent\$ \*/\s*\n\s*/\* clang-format (on|off) \*/\s*\n', | |
17 | re.M) | |
18 | ||
19 | def wrap_file(fn): | |
20 | with open(fn, 'r') as fd: | |
21 | text = fd.read() | |
22 | ||
23 | repl = r'/* $FRR indent$ */\n/* clang-format off */\n' + \ | |
24 | r'\1' + \ | |
25 | r'/* $FRR indent$ */\n/* clang-format on */\n' | |
26 | ||
27 | # around each DEFUN, insert an indent-on/off comment | |
28 | text = defun_re.sub(repl, text) | |
29 | text = define_re.sub(repl, text) | |
30 | ||
31 | ci = subprocess.Popen(['clang-format'], stdin = subprocess.PIPE, stdout = subprocess.PIPE) | |
32 | stdout, ign = ci.communicate(text) | |
33 | ci.wait() | |
34 | if ci.returncode != 0: | |
35 | raise IOError('clang-format returned %d' % (ci.returncode)) | |
36 | ||
37 | # remove the bits we inserted above | |
38 | final = clean_re.sub('', stdout) | |
39 | ||
40 | tmpname = fn + '.indent' | |
41 | with open(tmpname, 'w') as ofd: | |
42 | ofd.write(final) | |
43 | os.rename(tmpname, fn) | |
44 | ||
45 | if __name__ == '__main__': | |
46 | for fn in sys.argv[1:]: | |
47 | wrap_file(fn) |