Skip to content

Instantly share code, notes, and snippets.

@tomtebender
Last active March 8, 2026 15:52
Show Gist options
  • Select an option

  • Save tomtebender/859594f860c19e18274553481b561af7 to your computer and use it in GitHub Desktop.

Select an option

Save tomtebender/859594f860c19e18274553481b561af7 to your computer and use it in GitHub Desktop.
Find includes and check if they are there or not
#!/usr/bin/python3
# Copyright © 2026 Tomte Bender <tomte.bender@gmail.com>
# This file is licensed under tomteb's Quick License. If a copy of the license was
# not provided together with this file, you can recieve one at <https://github.com/tomtebender/tbql-license/blob/main/LICENSE>.
import sys, re, os
from os.path import dirname
def walk_includes(fpath):
try:
f = open(fpath, "r")
except:
return None, None
c = f.read()
f.close()
fileinc = re.findall(r"#include \"(.*)\"", c)
otherinc = re.findall(r"#include <(.*)>", c)
return fileinc, otherinc
def __walk_includes_all(fpath, finc: set = set(), oinc: set = set(), ferr: set = set()):
fi, oi = walk_includes(fpath)
if fi == None:
ferr.add(fpath)
else:
finc.add(fpath)
for inc in oi:
oinc.add("<" + inc + ">")
for inc in fi:
inc = dirname(fpath) + "/" + inc
if not (inc in finc):
finc, oinc, ferr = __walk_includes_all(inc, finc, oinc, ferr)
return finc, oinc, ferr
def walk_includes_all(fpath: str):
f,o,e = __walk_includes_all(fpath)
if not (fpath[-2] == ".h"):
try:
f.remove(fpath)
except:
pass
return f,o,e
if __name__ == "__main__":
inbet = ""
fiprefix = "\x1b[1m\x1b[38;2;0;255;0m☺ "
errprefix = "\x1b[38;2;255;0;0m☹ "
oiprefix = "\x1b[0m\x1b[38;2;127;127;127mOther: "
if len(sys.argv) < 2:
print("finc: Invalid amount of arguments!")
print("Usage: finc.py [-n] (path)")
exit(1)
fp = sys.argv[1]
NOFORMAT = fp == "-n"
if NOFORMAT:
inbet = "\n"
fiprefix = ""
errprefix = ""
oiprefix = "other: "
fp = sys.argv[2]
fi, oi, err = walk_includes_all(fp)
if fi != set():
if NOFORMAT:
print("#### included and existent ####")
for file in fi:
print(fiprefix+file)
print(inbet, end="")
if err != set():
if NOFORMAT:
print("#### included but non-existent ####")
for file in err:
print(errprefix+file)
print(inbet, end="")
if oi != set():
print(oiprefix, end="")
print(*oi, sep=" | ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment