]>
git.proxmox.com Git - mirror_frr.git/blob - tools/indent.py
2 # -*- coding: utf-8 -*-
3 # 2017 by David Lamparter, placed in public domain
5 import sys
, re
, subprocess
, os
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*\{)",
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
20 with
open(fn
, "r") as fd
:
24 r
"/* $FRR indent$ */\n/* clang-format off */\n"
26 + r
"/* $FRR indent$ */\n/* clang-format on */\n"
29 # around each DEFUN, insert an indent-on/off comment
30 text
= defun_re
.sub(repl
, text
)
31 text
= define_re
.sub(repl
, text
)
33 ci
= subprocess
.Popen(
34 ["clang-format"], stdin
=subprocess
.PIPE
, stdout
=subprocess
.PIPE
36 stdout
, ign
= ci
.communicate(text
)
38 if ci
.returncode
!= 0:
39 raise IOError("clang-format returned %d" % (ci
.returncode
))
41 # remove the bits we inserted above
42 final
= clean_re
.sub("", stdout
)
44 tmpname
= fn
+ ".indent"
45 with
open(tmpname
, "w") as ofd
:
47 os
.rename(tmpname
, fn
)
50 if __name__
== "__main__":
51 for fn
in sys
.argv
[1:]: