]>
git.proxmox.com Git - mirror_frr.git/blob - python/firstheader.py
1 # SPDX-License-Identifier: GPL-2.0-or-later
2 # check that the first header included in C files is either
5 # Copyright (C) 2020 David Lamparter for NetDEF, Inc.
13 argp
= argparse
.ArgumentParser(description
="include fixer")
14 argp
.add_argument("--autofix", action
="store_const", const
=True)
15 argp
.add_argument("--warn-empty", action
="store_const", const
=True)
16 argp
.add_argument("--pipe", action
="store_const", const
=True)
18 include_re
= re
.compile('^#\s*include\s+["<]([^ ">]+)[">]', re
.M
)
21 lambda fn
: fn
.startswith("tools/"),
32 files
= subprocess
.check_output(["git", "ls-files"]).decode("ASCII")
33 for fn
in files
.splitlines():
34 if not fn
.endswith(".c"):
36 if max([i(fn
) for i
in ignore
]):
39 with
open(fn
, "r") as fd
:
42 m
= include_re
.search(data
)
45 sys
.stderr
.write("no #include in %s?\n" % (fn
))
47 if m
.group(1) in ["config.h", "zebra.h", "lib/zebra.h"]:
51 sys
.stderr
.write("%s: %s - fixing\n" % (fn
, m
.group(0)))
52 if fn
.startswith("pceplib/"):
53 insert
= '#ifdef HAVE_CONFIG_H\n#include "config.h"\n#endif\n\n'
55 insert
= "#include <zebra.h>\n\n"
59 data
= data
[:pos
] + insert
+ data
[pos
:]
60 with
open(fn
+ ".new", "w") as fd
:
62 os
.rename(fn
+ ".new", fn
)
64 sys
.stderr
.write("%s: %s\n" % (fn
, m
.group(0)))
69 # for "vim `firstheader.py`"
75 if __name__
== "__main__":
76 args
= argp
.parse_args()