diff options
| author | Quentin Young <qlyoung@cumulusnetworks.com> | 2018-03-27 15:21:15 -0400 |
|---|---|---|
| committer | Quentin Young <qlyoung@cumulusnetworks.com> | 2018-03-27 15:21:15 -0400 |
| commit | 42732e05a9a97a18f496eb559a77062a1b4b420e (patch) | |
| tree | 03c8b87c2b26cb43b7b241f5ab0db6583c4b5a30 /tools/indent.py | |
| parent | 056d8355967b372566a98432f07e4cff66ab17b0 (diff) | |
| parent | 2d6e6d36d7e847b997f29097268dc529bd154d10 (diff) | |
Merge branch 'master' into stylechecker
Diffstat (limited to 'tools/indent.py')
| -rwxr-xr-x | tools/indent.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/tools/indent.py b/tools/indent.py new file mode 100755 index 0000000000..139049b670 --- /dev/null +++ b/tools/indent.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# 2017 by David Lamparter, placed in public domain + +import sys, re, subprocess, os + +# find all DEFUNs +defun_re = re.compile( + r'^((DEF(UN(|_ATTR|_CMD_(ELEMENT|FUNC_(DECL|TEXT))|_DEPRECATED|_NOSH|_HIDDEN|SH(|_ATTR|_DEPRECATED|_HIDDEN))?|PY)|ALIAS)\s*\(.*?)^(?=\s*\{)', + re.M | re.S) +define_re = re.compile( + r'((^#\s*define[^\n]+[^\\]\n)+)', + re.M | re.S) +# find clang-format control that we just inserted +clean_re = re.compile( + r'^.*/\* \$FRR indent\$ \*/\s*\n\s*/\* clang-format (on|off) \*/\s*\n', + re.M) + +def wrap_file(fn): + with open(fn, 'r') as fd: + text = fd.read() + + repl = r'/* $FRR indent$ */\n/* clang-format off */\n' + \ + r'\1' + \ + r'/* $FRR indent$ */\n/* clang-format on */\n' + + # around each DEFUN, insert an indent-on/off comment + text = defun_re.sub(repl, text) + text = define_re.sub(repl, text) + + ci = subprocess.Popen(['clang-format'], stdin = subprocess.PIPE, stdout = subprocess.PIPE) + stdout, ign = ci.communicate(text) + ci.wait() + if ci.returncode != 0: + raise IOError('clang-format returned %d' % (ci.returncode)) + + # remove the bits we inserted above + final = clean_re.sub('', stdout) + + tmpname = fn + '.indent' + with open(tmpname, 'w') as ofd: + ofd.write(final) + os.rename(tmpname, fn) + +if __name__ == '__main__': + for fn in sys.argv[1:]: + wrap_file(fn) |
