diff options
| author | Russ White <russ@riw.us> | 2018-02-27 08:55:38 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-02-27 08:55:38 -0500 |
| commit | b92aee6e7c7b35b02d5089bfb37bcadc4f7b0d54 (patch) | |
| tree | 3cb762beb11c3030d4e76fcfaa579d120bb3e64c /tools/indent.py | |
| parent | 3c5aa3781aa8d0e15da784acec63bf7550bc8c8a (diff) | |
| parent | 7c557e5b3da8b8ba1db9542bc24bbf2a70bd1f1a (diff) | |
Merge pull request #1770 from qlyoung/random-tools
*: move random tools into tools/
Diffstat (limited to 'tools/indent.py')
| -rw-r--r-- | tools/indent.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/tools/indent.py b/tools/indent.py new file mode 100644 index 0000000000..560c13c77d --- /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(_NOSH|_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) |
