]> git.proxmox.com Git - mirror_frr.git/blame - tools/indent.py
lib, zebra: Ensure that the ifp->node exists
[mirror_frr.git] / tools / indent.py
CommitLineData
888ac268
DL
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
47a3a827 3# SPDX-License-Identifier: NONE
888ac268
DL
4# 2017 by David Lamparter, placed in public domain
5
6import sys, re, subprocess, os
7
8# find all DEFUNs
9defun_re = re.compile(
701a0192 10 r"^((DEF(UN(|_ATTR|_CMD_(ELEMENT|FUNC_(DECL|TEXT))|_DEPRECATED|_NOSH|_HIDDEN|SH(|_ATTR|_DEPRECATED|_HIDDEN))?|PY|PY_ATTR|PY_HIDDEN)|ALIAS)\s*\(.*?)^(?=\s*\{)",
11 re.M | re.S,
12)
13define_re = re.compile(r"((^#\s*define[^\n]+[^\\]\n)+)", re.M | re.S)
888ac268
DL
14# find clang-format control that we just inserted
15clean_re = re.compile(
701a0192 16 r"^.*/\* \$FRR indent\$ \*/\s*\n\s*/\* clang-format (on|off) \*/\s*\n", re.M
17)
18
888ac268
DL
19
20def wrap_file(fn):
701a0192 21 with open(fn, "r") as fd:
888ac268
DL
22 text = fd.read()
23
701a0192 24 repl = (
25 r"/* $FRR indent$ */\n/* clang-format off */\n"
26 + r"\1"
27 + r"/* $FRR indent$ */\n/* clang-format on */\n"
28 )
888ac268
DL
29
30 # around each DEFUN, insert an indent-on/off comment
31 text = defun_re.sub(repl, text)
32 text = define_re.sub(repl, text)
33
701a0192 34 ci = subprocess.Popen(
35 ["clang-format"], stdin=subprocess.PIPE, stdout=subprocess.PIPE
36 )
888ac268
DL
37 stdout, ign = ci.communicate(text)
38 ci.wait()
39 if ci.returncode != 0:
701a0192 40 raise IOError("clang-format returned %d" % (ci.returncode))
888ac268
DL
41
42 # remove the bits we inserted above
701a0192 43 final = clean_re.sub("", stdout)
888ac268 44
701a0192 45 tmpname = fn + ".indent"
46 with open(tmpname, "w") as ofd:
888ac268
DL
47 ofd.write(final)
48 os.rename(tmpname, fn)
49
701a0192 50
51if __name__ == "__main__":
888ac268
DL
52 for fn in sys.argv[1:]:
53 wrap_file(fn)