blob: 501516b0d44a9ed42e256971dc02e8310c9ea123 [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.
Patrick Williamsc124f4f2015-09-15 14:41:29 -05004#
Brad Bishopc342db32019-05-15 21:57:59 -04005# SPDX-License-Identifier: GPL-2.0-or-later
Patrick Williamsc124f4f2015-09-15 14:41:29 -05006#
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
13import os
14import sys
15import stat
16
17class 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 Williamsc0f7c042017-02-23 20:41:17 -060041 def __lt__(this, that):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050042 if that is None:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060043 return False
Patrick Williamsc124f4f2015-09-15 14:41:29 -050044 if not isinstance(that, Record):
45 raise TypeError
46 if len(this.records) > 0 and len(that.records) == 0:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060047 return False
Patrick Williamsc124f4f2015-09-15 14:41:29 -050048 if this.size > that.size:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060049 return False
50 return True
Patrick Williamsc124f4f2015-09-15 14:41:29 -050051
52 def show(self, minsize):
53 total = 0
54 if self.size <= minsize:
55 return 0
Patrick Williamsc0f7c042017-02-23 20:41:17 -060056 print("%10d %s" % (self.size, self.path))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050057 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
64def 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 Williamsc0f7c042017-02-23 20:41:17 -060070 print("Displayed %d/%d bytes (%.2f%%)" % \
71 (total, rootfs.size, 100 * float(total) / rootfs.size))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050072
73
74if __name__ == "__main__":
75 main()