]> git.proxmox.com Git - mirror_frr.git/blame - tools/indent.py
tools: Handle new lines for json_object_to_json_string_ext()
[mirror_frr.git] / tools / indent.py
CommitLineData
888ac268
DL
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# 2017 by David Lamparter, placed in public domain
4
5import sys, re, subprocess, os
6
7# find all DEFUNs
8defun_re = re.compile(
701a0192 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)
12define_re = re.compile(r"((^#\s*define[^\n]+[^\\]\n)+)", re.M | re.S)
888ac268
DL
13# find clang-format control that we just inserted
14clean_re = re.compile(
701a0192 15 r"^.*/\* \$FRR indent\$ \*/\s*\n\s*/\* clang-format (on|off) \*/\s*\n", re.M
16)
17
888ac268
DL
18
19def wrap_file(fn):
701a0192 20 with open(fn, "r") as fd:
888ac268
DL
21 text = fd.read()
22
701a0192 23 repl = (
24 r"/* $FRR indent$ */\n/* clang-format off */\n"
25 + r"\1"
26 + r"/* $FRR indent$ */\n/* clang-format on */\n"
27 )
888ac268
DL
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
701a0192 33 ci = subprocess.Popen(
34 ["clang-format"], stdin=subprocess.PIPE, stdout=subprocess.PIPE
35 )
888ac268
DL
36 stdout, ign = ci.communicate(text)
37 ci.wait()
38 if ci.returncode != 0:
701a0192 39 raise IOError("clang-format returned %d" % (ci.returncode))
888ac268
DL
40
41 # remove the bits we inserted above
701a0192 42 final = clean_re.sub("", stdout)
888ac268 43
701a0192 44 tmpname = fn + ".indent"
45 with open(tmpname, "w") as ofd:
888ac268
DL
46 ofd.write(final)
47 os.rename(tmpname, fn)
48
701a0192 49
50if __name__ == "__main__":
888ac268
DL
51 for fn in sys.argv[1:]:
52 wrap_file(fn)