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. |
| 4 | # All rights reserved. |
| 5 | # |
| 6 | # This program is free software; you can redistribute it and/or modify |
| 7 | # it under the terms of the GNU General Public License as published by |
| 8 | # the Free Software Foundation; either version 2 of the License, or |
| 9 | # (at your option) any later version. |
| 10 | # |
| 11 | # This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | # GNU General Public License for more details. |
| 15 | # |
| 16 | # You should have received a copy of the GNU General Public License |
| 17 | # along with this program; if not, write to the Free Software |
| 18 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | # |
| 20 | # |
| 21 | # Display details of the root filesystem size, broken up by directory. |
| 22 | # Allows for limiting by size to focus on the larger files. |
| 23 | # |
| 24 | # Author: Darren Hart <dvhart@linux.intel.com> |
| 25 | # |
| 26 | |
| 27 | import os |
| 28 | import sys |
| 29 | import stat |
| 30 | |
| 31 | class Record: |
| 32 | def create(path): |
| 33 | r = Record(path) |
| 34 | |
| 35 | s = os.lstat(path) |
| 36 | if stat.S_ISDIR(s.st_mode): |
| 37 | for p in os.listdir(path): |
| 38 | pathname = path + "/" + p |
| 39 | ss = os.lstat(pathname) |
| 40 | if not stat.S_ISLNK(ss.st_mode): |
| 41 | r.records.append(Record.create(pathname)) |
| 42 | r.size += r.records[-1].size |
| 43 | r.records.sort(reverse=True) |
| 44 | else: |
| 45 | r.size = os.lstat(path).st_size |
| 46 | |
| 47 | return r |
| 48 | create = staticmethod(create) |
| 49 | |
| 50 | def __init__(self, path): |
| 51 | self.path = path |
| 52 | self.size = 0 |
| 53 | self.records = [] |
| 54 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 55 | def __lt__(this, that): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 56 | if that is None: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 57 | return False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 58 | if not isinstance(that, Record): |
| 59 | raise TypeError |
| 60 | if len(this.records) > 0 and len(that.records) == 0: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 61 | return False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 62 | if this.size > that.size: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 63 | return False |
| 64 | return True |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 65 | |
| 66 | def show(self, minsize): |
| 67 | total = 0 |
| 68 | if self.size <= minsize: |
| 69 | return 0 |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 70 | print("%10d %s" % (self.size, self.path)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 71 | for r in self.records: |
| 72 | total += r.show(minsize) |
| 73 | if len(self.records) == 0: |
| 74 | total = self.size |
| 75 | return total |
| 76 | |
| 77 | |
| 78 | def main(): |
| 79 | minsize = 0 |
| 80 | if len(sys.argv) == 2: |
| 81 | minsize = int(sys.argv[1]) |
| 82 | rootfs = Record.create(".") |
| 83 | total = rootfs.show(minsize) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 84 | print("Displayed %d/%d bytes (%.2f%%)" % \ |
| 85 | (total, rootfs.size, 100 * float(total) / rootfs.size)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 86 | |
| 87 | |
| 88 | if __name__ == "__main__": |
| 89 | main() |