]> git.proxmox.com Git - mirror_frr.git/blob - tools/indent.py
Merge pull request #7262 from idryzhov/hide-bgpd-test
[mirror_frr.git] / tools / indent.py
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 )
12 define_re = re.compile(r"((^#\s*define[^\n]+[^\\]\n)+)", re.M | re.S)
13 # find clang-format control that we just inserted
14 clean_re = re.compile(
15 r"^.*/\* \$FRR indent\$ \*/\s*\n\s*/\* clang-format (on|off) \*/\s*\n", re.M
16 )
17
18
19 def wrap_file(fn):
20 with open(fn, "r") as fd:
21 text = fd.read()
22
23 repl = (
24 r"/* $FRR indent$ */\n/* clang-format off */\n"
25 + r"\1"
26 + r"/* $FRR indent$ */\n/* clang-format on */\n"
27 )
28
29 # around each DEFUN, insert an indent-on/off comment
30 text = defun_re.sub(repl, text)
31 text = define_re.sub(repl, text)
32
33 ci = subprocess.Popen(
34 ["clang-format"], stdin=subprocess.PIPE, stdout=subprocess.PIPE
35 )
36 stdout, ign = ci.communicate(text)
37 ci.wait()
38 if ci.returncode != 0:
39 raise IOError("clang-format returned %d" % (ci.returncode))
40
41 # remove the bits we inserted above
42 final = clean_re.sub("", stdout)
43
44 tmpname = fn + ".indent"
45 with open(tmpname, "w") as ofd:
46 ofd.write(final)
47 os.rename(tmpname, fn)
48
49
50 if __name__ == "__main__":
51 for fn in sys.argv[1:]:
52 wrap_file(fn)