]>
git.proxmox.com Git - mirror_frr.git/blob - 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(_NOSH|_HIDDEN)?|PY)\s*\(.*?)^(?=\s*\{)',
11 define_re
= re
.compile(
12 r
'((^#\s*define[^\n]+[^\\]\n)+)',
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',
20 with
open(fn
, 'r') as fd
:
23 repl
= r
'/* $FRR indent$ */\n/* clang-format off */\n' + \
25 r
'/* $FRR indent$ */\n/* clang-format on */\n'
27 # around each DEFUN, insert an indent-on/off comment
28 text
= defun_re
.sub(repl
, text
)
29 text
= define_re
.sub(repl
, text
)
31 ci
= subprocess
.Popen(['clang-format'], stdin
= subprocess
.PIPE
, stdout
= subprocess
.PIPE
)
32 stdout
, ign
= ci
.communicate(text
)
34 if ci
.returncode
!= 0:
35 raise IOError('clang-format returned %d' % (ci
.returncode
))
37 # remove the bits we inserted above
38 final
= clean_re
.sub('', stdout
)
40 tmpname
= fn
+ '.indent'
41 with
open(tmpname
, 'w') as ofd
:
43 os
.rename(tmpname
, fn
)
45 if __name__
== '__main__':
46 for fn
in sys
.argv
[1:]: