]> git.puffer.fish Git - matthieu/frr.git/commitdiff
python: add check-first-header tool
authorDavid Lamparter <equinox@diac24.net>
Thu, 23 Apr 2020 18:14:58 +0000 (20:14 +0200)
committerDavid Lamparter <equinox@diac24.net>
Mon, 27 Apr 2020 07:52:41 +0000 (09:52 +0200)
The first #include statement in all FRR .c files should be either
zebra.h or config.h.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
python/firstheader.py [new file with mode: 0644]

diff --git a/python/firstheader.py b/python/firstheader.py
new file mode 100644 (file)
index 0000000..19a85b6
--- /dev/null
@@ -0,0 +1,30 @@
+#
+# check that the first header included in C files is either
+# zebra.h or config.h
+#
+
+import sys, os, re, subprocess
+
+include_re = re.compile('^#\s*include\s+["<]([^ ">]+)[">]', re.M)
+
+errors = 0
+
+files = subprocess.check_output(['git', 'ls-files']).decode('ASCII')
+for fn in files.splitlines():
+    if not fn.endswith('.c'):
+        continue
+    if fn.startswith('tools/'):
+        continue
+    with open(fn, 'r') as fd:
+        data = fd.read()
+        m = include_re.search(data)
+        if m is None:
+            #sys.stderr.write('no #include in %s?\n' % (fn))
+            continue
+        if m.group(1) in ['config.h', 'zebra.h', 'lib/zebra.h']:
+            continue
+        sys.stderr.write('%s: %s\n' % (fn, m.group(0)))
+        errors += 1
+
+if errors:
+    sys.exit(1)