modified: src1/input.c
[GalaxyCodeBases.git] / python / etc / fgidu / fgidu.py
blob80be3c855013e7c4383e685b5ae9365ba35a3eb6
1 #!/usr/bin/env python3
3 import os
4 # https://stackoverflow.com/a/62000501/159695
5 def du(path):
6 if os.path.islink(path):
7 return (os.lstat(path).st_size, 0)
8 if os.path.isfile(path):
9 st = os.lstat(path)
10 return (st.st_size, st.st_blocks * 512)
11 apparent_total_bytes = 0
12 total_bytes = 0
13 have = []
14 for dirpath, dirnames, filenames in os.walk(path):
15 apparent_total_bytes += os.lstat(dirpath).st_size
16 total_bytes += os.lstat(dirpath).st_blocks * 512
17 for f in filenames:
18 fp = os.path.join(dirpath, f)
19 if os.path.islink(fp):
20 apparent_total_bytes += os.lstat(fp).st_size
21 continue
22 st = os.lstat(fp)
23 if st.st_ino in have:
24 continue # skip hardlinks which were already counted
25 have.append(st.st_ino)
26 apparent_total_bytes += st.st_size
27 total_bytes += st.st_blocks * 512
28 for d in dirnames:
29 dp = os.path.join(dirpath, d)
30 if os.path.islink(dp):
31 apparent_total_bytes += os.lstat(dp).st_size
32 return (apparent_total_bytes, total_bytes)
34 def humanized_size(num, suffix='B', si=False, decps=1):
35 # decps for decimal places
36 if si:
37 units = ['','K','M','G','T','P','E','Z']
38 last_unit = 'Y'
39 div = 1000.0
40 else:
41 units = ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']
42 last_unit = 'Yi'
43 div = 1024.0
44 for unit in units:
45 if abs(num) < div:
46 #return "%3.1f%s%s" % (num, unit, suffix)
47 return f"%3.{decps}f {unit}{suffix}" % (num)
48 num /= div
49 #return "%.1f%s%s" % (num, last_unit, suffix)
50 return f"%.{decps}f {last_unit}{suffix}" % (num)
52 #s1,s2 = du("/Users/galaxy/t/t/t/t/Received")
53 s1,s2 = du("x")
56 print(s1)
57 print(s2)
58 print(humanized_size(s1,decps=2))
59 print(humanized_size(s2,si=True,decps=4))