|
| 1 | +import os |
| 2 | +import sys |
| 3 | +from argparse import ArgumentParser |
| 4 | +from fnmatch import fnmatch |
| 5 | + |
| 6 | + |
| 7 | +def is_excluded(path, pattern): |
| 8 | + if pattern: |
| 9 | + while path != '': |
| 10 | + prefix, tail = os.path.split(path) |
| 11 | + if fnmatch(tail, pattern): |
| 12 | + return True |
| 13 | + else: |
| 14 | + path = prefix |
| 15 | + return False |
| 16 | + else: |
| 17 | + return False |
| 18 | + |
| 19 | + |
| 20 | +def main(args): |
| 21 | + ap = ArgumentParser( |
| 22 | + description='Summarize disk usage of the set of FILEs, recursively for directories.' |
| 23 | + ) |
| 24 | + ap.add_argument('-s', '--summarize', action='store_true', |
| 25 | + help='display only a total for each argument') |
| 26 | + ap.add_argument('--exclude', dest='exclude_pattern', |
| 27 | + metavar='PATTERN', |
| 28 | + help='exclude files that match PATTERN') |
| 29 | + ap.add_argument('FILEs', nargs='*', default=['.'], |
| 30 | + help='files to summarize (default to current working directory') |
| 31 | + |
| 32 | + ns = ap.parse_args(args) |
| 33 | + |
| 34 | + exclude_pattern = ns.exclude_pattern if ns.exclude_pattern else None |
| 35 | + |
| 36 | + sizeof_fmt = globals()['_stash'].libcore.sizeof_fmt |
| 37 | + |
| 38 | + for path in ns.FILEs: |
| 39 | + |
| 40 | + path_base = os.path.dirname(path) |
| 41 | + |
| 42 | + # Use relative path because of the following facts: |
| 43 | + # du A/B --exclude="B" -> no output |
| 44 | + # du A/B --exclude="A" -> normal output |
| 45 | + if is_excluded(os.path.relpath(path, path_base), exclude_pattern): |
| 46 | + continue |
| 47 | + |
| 48 | + if os.path.isdir(path): |
| 49 | + dirs_dict = {} |
| 50 | + # We need to walk the tree from the bottom up so that a directory can have easy |
| 51 | + # access to the size of its subdirectories. |
| 52 | + for root, dirs, files in os.walk(path, topdown=False): |
| 53 | + # This is to make sure the directory is not exclude from its ancestor |
| 54 | + if is_excluded(os.path.relpath(root, path_base), exclude_pattern): |
| 55 | + continue |
| 56 | + |
| 57 | + # Loop through every non directory file in this directory and sum their sizes |
| 58 | + size = sum(os.path.getsize(os.path.join(root, name)) |
| 59 | + for name in files if not is_excluded(name, exclude_pattern)) |
| 60 | + |
| 61 | + # Look at all of the subdirectories and add up their sizes from the `dirs_dict` |
| 62 | + subdir_size = sum(dirs_dict[os.path.join(root, d)] |
| 63 | + for d in dirs if not is_excluded(d, exclude_pattern)) |
| 64 | + |
| 65 | + # store the size of this directory (plus subdirectories) in a dict so we |
| 66 | + # can access it later |
| 67 | + my_size = dirs_dict[root] = size + subdir_size |
| 68 | + |
| 69 | + if ns.summarize and root != path: |
| 70 | + continue |
| 71 | + |
| 72 | + print '%-8s %s' % (sizeof_fmt(my_size), root) |
| 73 | + else: |
| 74 | + print '%-8s %s' % (sizeof_fmt(os.path.getsize(path)), path) |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == '__main__': |
| 78 | + main(sys.argv[1:]) |
0 commit comments