blob: ddccc5a8c8f77b9a078fbb785fa9bc6fdf840f47 [file] [log] [blame]
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001#!/usr/bin/env python3
Patrick Williamsc124f4f2015-09-15 14:41:29 -05002#
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
27import os
28import sys
29import stat
30
31class 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 Williamsc0f7c042017-02-23 20:41:17 -060055 def __lt__(this, that):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050056 if that is None:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060057 return False
Patrick Williamsc124f4f2015-09-15 14:41:29 -050058 if not isinstance(that, Record):
59 raise TypeError
60 if len(this.records) > 0 and len(that.records) == 0:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060061 return False
Patrick Williamsc124f4f2015-09-15 14:41:29 -050062 if this.size > that.size:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060063 return False
64 return True
Patrick Williamsc124f4f2015-09-15 14:41:29 -050065
66 def show(self, minsize):
67 total = 0
68 if self.size <= minsize:
69 return 0
Patrick Williamsc0f7c042017-02-23 20:41:17 -060070 print("%10d %s" % (self.size, self.path))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050071 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
78def 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 Williamsc0f7c042017-02-23 20:41:17 -060084 print("Displayed %d/%d bytes (%.2f%%)" % \
85 (total, rootfs.size, 100 * float(total) / rootfs.size))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050086
87
88if __name__ == "__main__":
89 main()