-
Notifications
You must be signed in to change notification settings - Fork 0
/
fdu.py
executable file
·64 lines (56 loc) · 1.91 KB
/
fdu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#! /usr/bin/env python3
import glob
import os.path
import copy
import sys
def human_size(size):
unit = ''
units = ['KB', 'MB', 'GB', 'TB']
while size > 1024 and len(units) > 0:
size /= 1024
unit = units.pop(0)
return "%.2f %s" % (size, unit)
def write(fname, args):
args_ = args
args_.files = ["%s/*" % fname]
size = fdu(args_, silent=True)
fsize = "%s/.SIZE" % fname
with open(fsize, 'w') as f:
f.write("%s\n" % size)
return size
def fdu(args, silent=False):
total_size = 0
for file_ in args.files:
for f in glob.glob(file_):
size = 0
if os.path.isdir(f) and not os.path.ismount(f) and not os.path.islink(f):
if args.update:
size = write(f, args)
else:
try:
with open("%s/.SIZE" % f, 'r') as f_:
size = int(f_.read())
except IOError:
size = write(f, args)
if f[-1] != "/":
f += '/'
else:
try:
size = os.path.getsize(f)
except OSError:
sys.stderr.write("can't get size: %s" % f)
if not silent:
print("%s %s" % (f, human_size(size)))
total_size += size
if not silent:
print("total: %s" % (human_size(total_size)))
return total_size
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-u", "--update", help="recompute every .SIZE", action="store_true")
#parser.add_argument("-p", "--update-parent", help="recompute this .SIZE and all its parent", action="store_true")
#parser.add_argument("-s", "--safe-write", help="use fdatasync", action="store_true")
parser.add_argument('files', metavar='FILE', nargs='*')
args = parser.parse_args()
fdu(args)
# vim: set ts=4 sw=4 expandtab: