| 1 |
|
| 2 |
|
| 3 |
import argparse |
| 4 |
import btrfs |
| 5 |
import sys |
| 6 |
|
| 7 |
|
| 8 |
def arg_parser(): |
| 9 |
parser = argparse.ArgumentParser(add_help=False) |
| 10 |
parser.add_argument( |
| 11 |
'-b', '--raw', |
| 12 |
action='store_true', |
| 13 |
help="raw numbers in bytes", |
| 14 |
) |
| 15 |
parser.add_argument( |
| 16 |
'-h', '--human-readable', |
| 17 |
action='store_true', |
| 18 |
help="human friendly numbers, base 1024 (default)", |
| 19 |
) |
| 20 |
parser.add_argument( |
| 21 |
'-H', |
| 22 |
action='store_true', dest='human_si', |
| 23 |
help="human friendly numbers, base 1000", |
| 24 |
) |
| 25 |
parser.add_argument( |
| 26 |
'--iec', |
| 27 |
action='store_true', |
| 28 |
help="use 1024 as a base (KiB, MiB, GiB, TiB)", |
| 29 |
) |
| 30 |
parser.add_argument( |
| 31 |
'--si', |
| 32 |
action='store_true', |
| 33 |
help="use 1000 as a base (kB, MB, GB, TB)", |
| 34 |
) |
| 35 |
parser.add_argument( |
| 36 |
'-k', |
| 37 |
'--kbytes', |
| 38 |
action='store_true', |
| 39 |
help="show sizes in KiB, or kB with --si", |
| 40 |
) |
| 41 |
parser.add_argument( |
| 42 |
'-m', |
| 43 |
'--mbytes', |
| 44 |
action='store_true', |
| 45 |
help="show sizes in MiB, or MB with --si", |
| 46 |
) |
| 47 |
parser.add_argument( |
| 48 |
'-g', |
| 49 |
'--gbytes', |
| 50 |
action='store_true', |
| 51 |
help="show sizes in GiB, or GB with --si", |
| 52 |
) |
| 53 |
parser.add_argument( |
| 54 |
'-t', |
| 55 |
'--tbytes', |
| 56 |
action='store_true', |
| 57 |
help="show sizes in TiB, or TB with --si", |
| 58 |
) |
| 59 |
parser.add_argument( |
| 60 |
'path', |
| 61 |
nargs=1, |
| 62 |
) |
| 63 |
parser.add_argument( |
| 64 |
'junk', |
| 65 |
nargs='*', |
| 66 |
help=argparse.SUPPRESS, |
| 67 |
) |
| 68 |
return parser |
| 69 |
|
| 70 |
|
| 71 |
def main(): |
| 72 |
parser = arg_parser() |
| 73 |
args = parser.parse_args() |
| 74 |
unit = None |
| 75 |
binary = True |
| 76 |
if args.raw: |
| 77 |
unit = '' |
| 78 |
if args.human_readable: |
| 79 |
unit = None |
| 80 |
if args.human_si: |
| 81 |
unit = None |
| 82 |
binary = False |
| 83 |
if args.iec: |
| 84 |
binary = True |
| 85 |
if args.si: |
| 86 |
binary = False |
| 87 |
if args.kbytes: |
| 88 |
unit = 'K' |
| 89 |
if args.mbytes: |
| 90 |
unit = 'M' |
| 91 |
if args.gbytes: |
| 92 |
unit = 'G' |
| 93 |
if args.tbytes: |
| 94 |
unit = 'T' |
| 95 |
if len(args.junk) > 0: |
| 96 |
parser.print_help(sys.stderr) |
| 97 |
return |
| 98 |
|
| 99 |
with btrfs.FileSystem(args.path[0]) as fs: |
| 100 |
for space in fs.space_info(): |
| 101 |
print("{}, {}: total={}, used={}".format( |
| 102 |
btrfs.utils.space_type_description(space.flags), |
| 103 |
btrfs.utils.space_profile_description(space.flags), |
| 104 |
btrfs.utils.pretty_size(space.total_bytes, unit, binary), |
| 105 |
btrfs.utils.pretty_size(space.used_bytes, unit, binary))) |
| 106 |
|
| 107 |
|
| 108 |
if __name__ == '__main__': |
| 109 |
main() |