]>
git.proxmox.com Git - mirror_frr.git/blob - python/firstheader.py
1 # check that the first header included in C files is either
4 # Copyright (C) 2020 David Lamparter for NetDEF, Inc.
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by the Free
8 # Software Foundation; either version 2 of the License, or (at your option)
11 # This program is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 # You should have received a copy of the GNU General Public License along
17 # with this program; see the file COPYING; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 argp
= argparse
.ArgumentParser(description
="include fixer")
27 argp
.add_argument("--autofix", action
="store_const", const
=True)
28 argp
.add_argument("--warn-empty", action
="store_const", const
=True)
29 argp
.add_argument("--pipe", action
="store_const", const
=True)
31 include_re
= re
.compile('^#\s*include\s+["<]([^ ">]+)[">]', re
.M
)
34 lambda fn
: fn
.startswith("tools/"),
45 files
= subprocess
.check_output(["git", "ls-files"]).decode("ASCII")
46 for fn
in files
.splitlines():
47 if not fn
.endswith(".c"):
49 if max([i(fn
) for i
in ignore
]):
52 with
open(fn
, "r") as fd
:
55 m
= include_re
.search(data
)
58 sys
.stderr
.write("no #include in %s?\n" % (fn
))
60 if m
.group(1) in ["config.h", "zebra.h", "lib/zebra.h"]:
64 sys
.stderr
.write("%s: %s - fixing\n" % (fn
, m
.group(0)))
65 if fn
.startswith("pceplib/"):
66 insert
= '#ifdef HAVE_CONFIG_H\n#include "config.h"\n#endif\n\n'
68 insert
= "#include <zebra.h>\n\n"
72 data
= data
[:pos
] + insert
+ data
[pos
:]
73 with
open(fn
+ ".new", "w") as fd
:
75 os
.rename(fn
+ ".new", fn
)
77 sys
.stderr
.write("%s: %s\n" % (fn
, m
.group(0)))
82 # for "vim `firstheader.py`"
88 if __name__
== "__main__":
89 args
= argp
.parse_args()