diff options
Diffstat (limited to 'Tools/scripts/byext.py')
-rwxr-xr-x | Tools/scripts/byext.py | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/Tools/scripts/byext.py b/Tools/scripts/byext.py index 71e4ba1..b79ff37 100755 --- a/Tools/scripts/byext.py +++ b/Tools/scripts/byext.py @@ -5,6 +5,7 @@ import os import sys + class Stats: def __init__(self): @@ -28,12 +29,11 @@ class Stats: sys.stderr.write("Can't list %s: %s\n" % (dir, err)) self.addstats("<dir>", "unlistable", 1) return - names.sort() - for name in names: + for name in sorted(names): if name.startswith(".#"): - continue # Skip CVS temp files + continue # Skip CVS temp files if name.endswith("~"): - continue# Skip Emacs backup files + continue # Skip Emacs backup files full = os.path.join(dir, name) if os.path.islink(full): self.addstats("<lnk>", "links", 1) @@ -46,26 +46,25 @@ class Stats: head, ext = os.path.splitext(filename) head, base = os.path.split(filename) if ext == base: - ext = "" # E.g. .cvsignore is deemed not to have an extension + ext = "" # E.g. .cvsignore is deemed not to have an extension ext = os.path.normcase(ext) if not ext: ext = "<none>" self.addstats(ext, "files", 1) try: - f = open(filename, "rb") + with open(filename, "rb") as f: + data = f.read() except IOError as err: sys.stderr.write("Can't open %s: %s\n" % (filename, err)) self.addstats(ext, "unopenable", 1) return - data = f.read() - f.close() self.addstats(ext, "bytes", len(data)) if b'\0' in data: self.addstats(ext, "binary", 1) return if not data: self.addstats(ext, "empty", 1) - #self.addstats(ext, "chars", len(data)) + # self.addstats(ext, "chars", len(data)) lines = str(data, "latin-1").splitlines() self.addstats(ext, "lines", len(lines)) del lines @@ -105,17 +104,20 @@ class Stats: for ext in exts: self.stats[ext]["ext"] = ext cols.insert(0, "ext") + def printheader(): for col in cols: print("%*s" % (colwidth[col], col), end=' ') print() + printheader() for ext in exts: for col in cols: value = self.stats[ext].get(col, "") print("%*s" % (colwidth[col], value), end=' ') print() - printheader() # Another header at the bottom + printheader() # Another header at the bottom + def main(): args = sys.argv[1:] @@ -125,5 +127,6 @@ def main(): s.statargs(args) s.report() + if __name__ == "__main__": main() |