diff options
-rwxr-xr-x | deheader | 34 |
1 files changed, 30 insertions, 4 deletions
@@ -2,7 +2,7 @@ """\ deheader -- find (optionally remove) unneeded includes in C or C++ sourcefiles. -Usage: deheader [-h] [-v] [-r] [-i str] sourcefiles +Usage: deheader [-h] [-v] [-r] [-i str] [sourcefiles] -h, --help Emit this help message and quit. -i, --ignore Ignore (don't remove) headers matching the argument regexp -r, --remove Remove the final set of unneeded headers @@ -13,6 +13,11 @@ on which #includes can be omitted from them -- the test, for each foo.c or foo.cc or foo.cpp, is simply whether 'rm foo.o; make foo.o' returns a zero status. Optionally, with the -r option, the unneeded headers are removed. +If a sourcefile argument is a directory, the report is generates on all source +files beneath it. Subdirectories beginning with a dot are ignored. If no +arguments are given, the program runs as if the name of the current directory +had been passed to it. + The original sourcefile is moved to a name with an .orig suffix and restored on interrupt or after processing with its original timestamp, unless the -r option was given and headers removed. @@ -20,6 +25,25 @@ on interrupt or after processing with its original timestamp, unless the import sys, os, tempfile, getopt, time, re +def c_source(filename): + "Predicate: return true idf the filename appears to be C or C++ source." + return filename.endswith(".c") or filename.endswith(".cpp") + +def sourcefiles(roots): + "Get the names of sourcefiles at or beneath specified locations." + files = [] + for root in roots: + if not os.path.isdir(root): + if c_source(root): + files.append(root) + else: + for root, dirs, files in os.walk(root): + dirs = filter(lambda x: not x.startswith("."), dirs) + for name in files: + if c_source(name): + files.append(os.path.join(root, name)) + return files + def trim(line): "Get file reference from an #include, retaining <> if a system header." trimmed = line[9:].strip() @@ -76,7 +100,7 @@ def compile(source, msg="", verbosity=0): % (sourcefile, msg, explain, end-start) return (status, end - start) -def deheader(sourcefile, ignores, verbose): +def deheader(sourcefile, ignores, remove, verbose): # Sanity check against broken sourcefiles; we want this to # complain visibly if the sourcefile won't build at all. (st, t) = compile(sourcefile, min(1, verbose)) @@ -148,6 +172,8 @@ if __name__ == "__main__": elif switch in ('-v', '--verbose'): verbose += 1 ignore = re.compile("|".join(ignores)) + if not arguments: + arguments = ["."] - for sourcefile in arguments: - deheader(sourcefile, ignore, verbose) + for sourcefile in sourcefiles(arguments): + deheader(sourcefile, ignore, remove, verbose) |