diff options
author | Eric S. Raymond <esr@thyrsus.com> | 2008-11-06 21:34:44 +0000 |
---|---|---|
committer | Eric S. Raymond <esr@thyrsus.com> | 2008-11-06 21:34:44 +0000 |
commit | bf5e0604c4596f83eaf0fbaf33e6572d6beceabe (patch) | |
tree | 17934fb11dcba7210cbb5852451a44048c68f283 | |
parent | b806d7a25a444cd098b50960def66c8ea68c54e4 (diff) | |
download | deheader-bf5e0604c4596f83eaf0fbaf33e6572d6beceabe.tar.gz deheader-bf5e0604c4596f83eaf0fbaf33e6572d6beceabe.tar.bz2 deheader-bf5e0604c4596f83eaf0fbaf33e6572d6beceabe.tar.xz deheader-bf5e0604c4596f83eaf0fbaf33e6572d6beceabe.zip |
Refactoring step.
-rwxr-xr-x | deheader | 60 |
1 files changed, 32 insertions, 28 deletions
@@ -30,16 +30,16 @@ def trim(line): else: return `line` -def extract_includes(sourcefile, ignores): +def extract_includes(sourcefile, ignores, verbosity): "Extract a list of #include lines used in a given sourcfile." includes = [] for line in open(sourcefile): if line.startswith("#include"): - if verbose: + if verbosity: print "deheader: %s requires %s" % (sourcefile, trim(line)) for stopper in ignores: if stopper.search(line): - if verbose: + if verbosity: print "deheader: ignoring %s" % (trim(line)) break else: @@ -47,6 +47,7 @@ def extract_includes(sourcefile, ignores): return includes def compile(source, msg="", verbosity=0): + "Test-compile a sourcefile. Return the status and the compilation time" (stem, suffix) = os.path.splitext(source) derived = stem + ".o" if os.path.exists(derived): @@ -68,7 +69,32 @@ def compile(source, msg="", verbosity=0): % (sourcefile, msg, explain, end-start) return (status, end - start) -def c_analyze(source, includes): +def deheader(sourcefile, ignores, 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)) + if st != 0: + return + # Now do the analysis + includes = extract_includes(sourcefile, ignores, verbose) + if sourcefile.endswith(".c") or sourcefile.endswith(".cpp"): + unneeded = c_analyze(sourcefile, includes, verbose) + else: + print >>sys.stderr, "deheader: can't analyze %s" % sourcefile + return + if unneeded: + for line in unneeded: + print "deheader: remove %s from %s" % (trim(line), sourcefile) + if remove: + os.rename(sourcefile, original) + for header in includes: + ofp = open(sourcefile, "w") + for line in open(original): + if line not in unneeded: + ofp.write(line) + ofp.close() + +def c_analyze(source, includes, verbosity): "Given a C file and a list of includes, return those that can be omitted." # We'll remove headers in reverse order, because later unnecessary # headers might depend on earlier ones @@ -85,7 +111,7 @@ def c_analyze(source, includes): if line != header: ofp.write(line) ofp.close() - (st, t) = compile(sourcefile, " without %s" % trim(header), verbose) + (st, t) = compile(sourcefile, " without %s" % trim(header), verbosity) if st == 0: unneeded.append(header) includes.remove(header) @@ -116,26 +142,4 @@ if __name__ == "__main__": verbose += 1 for sourcefile in arguments: - # 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)) - if st != 0: - continue - # Now do the analysis - includes = extract_includes(sourcefile, ignores) - if sourcefile.endswith(".c") or sourcefile.endswith(".cpp"): - unneeded = c_analyze(sourcefile, includes) - else: - print >>sys.stderr, "deheader: can't analyze %s" % sourcefile - continue - if unneeded: - for line in unneeded: - print "deheader: remove %s from %s" % (trim(line), sourcefile) - if remove: - os.rename(sourcefile, original) - for header in includes: - ofp = open(sourcefile, "w") - for line in open(original): - if line not in unneeded: - ofp.write(line) - ofp.close() + deheader(sourcefile, ignores, verbose) |