Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 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 | |
| 55 | def __cmp__(this, that): |
| 56 | if that is None: |
| 57 | return 1 |
| 58 | if not isinstance(that, Record): |
| 59 | raise TypeError |
| 60 | if len(this.records) > 0 and len(that.records) == 0: |
| 61 | return -1 |
| 62 | if len(this.records) == 0 and len(that.records) > 0: |
| 63 | return 1 |
| 64 | if this.size < that.size: |
| 65 | return -1 |
| 66 | if this.size > that.size: |
| 67 | return 1 |
| 68 | return 0 |
| 69 | |
| 70 | def show(self, minsize): |
| 71 | total = 0 |
| 72 | if self.size <= minsize: |
| 73 | return 0 |
| 74 | print "%10d %s" % (self.size, self.path) |
| 75 | for r in self.records: |
| 76 | total += r.show(minsize) |
| 77 | if len(self.records) == 0: |
| 78 | total = self.size |
| 79 | return total |
| 80 | |
| 81 | |
| 82 | def main(): |
| 83 | minsize = 0 |
| 84 | if len(sys.argv) == 2: |
| 85 | minsize = int(sys.argv[1]) |
| 86 | rootfs = Record.create(".") |
| 87 | total = rootfs.show(minsize) |
| 88 | print "Displayed %d/%d bytes (%.2f%%)" % \ |
| 89 | (total, rootfs.size, 100 * float(total) / rootfs.size) |
| 90 | |
| 91 | |
| 92 | if __name__ == "__main__": |
| 93 | main() |