ยปCore Development>Code coverage>Tools/scripts/dutree.py

Python code coverage for Tools/scripts/dutree.py

#countcontent
1n/a#! /usr/bin/env python3
2n/a# Format du output in a tree shape
3n/a
4n/aimport os, sys, errno
5n/a
6n/adef main():
7n/a p = os.popen('du ' + ' '.join(sys.argv[1:]), 'r')
8n/a total, d = None, {}
9n/a for line in p.readlines():
10n/a i = 0
11n/a while line[i] in '0123456789': i = i+1
12n/a size = eval(line[:i])
13n/a while line[i] in ' \t': i = i+1
14n/a filename = line[i:-1]
15n/a comps = filename.split('/')
16n/a if comps[0] == '': comps[0] = '/'
17n/a if comps[len(comps)-1] == '': del comps[len(comps)-1]
18n/a total, d = store(size, comps, total, d)
19n/a try:
20n/a display(total, d)
21n/a except IOError as e:
22n/a if e.errno != errno.EPIPE:
23n/a raise
24n/a
25n/adef store(size, comps, total, d):
26n/a if comps == []:
27n/a return size, d
28n/a if comps[0] not in d:
29n/a d[comps[0]] = None, {}
30n/a t1, d1 = d[comps[0]]
31n/a d[comps[0]] = store(size, comps[1:], t1, d1)
32n/a return total, d
33n/a
34n/adef display(total, d):
35n/a show(total, d, '')
36n/a
37n/adef show(total, d, prefix):
38n/a if not d: return
39n/a list = []
40n/a sum = 0
41n/a for key in d.keys():
42n/a tsub, dsub = d[key]
43n/a list.append((tsub, key))
44n/a if tsub is not None: sum = sum + tsub
45n/a## if sum < total:
46n/a## list.append((total - sum, os.curdir))
47n/a list.sort()
48n/a list.reverse()
49n/a width = len(repr(list[0][0]))
50n/a for tsub, key in list:
51n/a if tsub is None:
52n/a psub = prefix
53n/a else:
54n/a print(prefix + repr(tsub).rjust(width) + ' ' + key)
55n/a psub = prefix + ' '*(width-1) + '|' + ' '*(len(key)+1)
56n/a if key in d:
57n/a show(tsub, d[key][1], psub)
58n/a
59n/aif __name__ == '__main__':
60n/a main()