Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 2 | # |
| 3 | # Copyright (c) 2011, Intel Corporation. |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 4 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 5 | # SPDX-License-Identifier: GPL-2.0-or-later |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 6 | # |
| 7 | # Display details of the root filesystem size, broken up by directory. |
| 8 | # Allows for limiting by size to focus on the larger files. |
| 9 | # |
| 10 | # Author: Darren Hart <dvhart@linux.intel.com> |
| 11 | # |
| 12 | |
| 13 | import os |
| 14 | import sys |
| 15 | import stat |
| 16 | |
| 17 | class Record: |
| 18 | def create(path): |
| 19 | r = Record(path) |
| 20 | |
| 21 | s = os.lstat(path) |
| 22 | if stat.S_ISDIR(s.st_mode): |
| 23 | for p in os.listdir(path): |
| 24 | pathname = path + "/" + p |
| 25 | ss = os.lstat(pathname) |
| 26 | if not stat.S_ISLNK(ss.st_mode): |
| 27 | r.records.append(Record.create(pathname)) |
| 28 | r.size += r.records[-1].size |
| 29 | r.records.sort(reverse=True) |
| 30 | else: |
| 31 | r.size = os.lstat(path).st_size |
| 32 | |
| 33 | return r |
| 34 | create = staticmethod(create) |
| 35 | |
| 36 | def __init__(self, path): |
| 37 | self.path = path |
| 38 | self.size = 0 |
| 39 | self.records = [] |
| 40 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 41 | def __lt__(this, that): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 42 | if that is None: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 43 | return False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 44 | if not isinstance(that, Record): |
| 45 | raise TypeError |
| 46 | if len(this.records) > 0 and len(that.records) == 0: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 47 | return False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 48 | if this.size > that.size: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 49 | return False |
| 50 | return True |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 51 | |
| 52 | def show(self, minsize): |
| 53 | total = 0 |
| 54 | if self.size <= minsize: |
| 55 | return 0 |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 56 | print("%10d %s" % (self.size, self.path)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 57 | for r in self.records: |
| 58 | total += r.show(minsize) |
| 59 | if len(self.records) == 0: |
| 60 | total = self.size |
| 61 | return total |
| 62 | |
| 63 | |
| 64 | def main(): |
| 65 | minsize = 0 |
| 66 | if len(sys.argv) == 2: |
| 67 | minsize = int(sys.argv[1]) |
| 68 | rootfs = Record.create(".") |
| 69 | total = rootfs.show(minsize) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 70 | print("Displayed %d/%d bytes (%.2f%%)" % \ |
| 71 | (total, rootfs.size, 100 * float(total) / rootfs.size)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 72 | |
| 73 | |
| 74 | if __name__ == "__main__": |
| 75 | main() |