cmd/reserve_disk_space.py

54 lines
1.5 KiB
Python
Raw Normal View History

2021-05-09 01:27:18 +00:00
import argparse
import sys
from voussoirkit import betterhelp
from voussoirkit import bytestring
2022-01-20 05:01:43 +00:00
from voussoirkit import pathclass
2021-05-09 01:27:18 +00:00
from voussoirkit import vlogging
log = vlogging.getLogger(__name__, 'reserve_disk_space')
def reserve_disk_space_argparse(args):
2022-01-20 05:01:43 +00:00
reserve = bytestring.parsebytes(args.reserve)
drive = pathclass.Path(args.drive)
2021-05-09 01:27:18 +00:00
try:
2022-01-20 05:01:43 +00:00
free = drive.assert_disk_space(reserve)
free = bytestring.bytestring(free)
reserve = bytestring.bytestring(reserve)
2021-05-09 01:27:18 +00:00
log.info('There is %s available out of %s.', free, reserve)
return 0
2022-01-20 05:01:43 +00:00
except pathclass.NotEnoughSpace as exc:
2021-05-09 01:27:18 +00:00
free = bytestring.bytestring(exc.free)
reserve = bytestring.bytestring(exc.reserve)
log.fatal('Only %s available out of %s.', free, reserve)
return 1
@vlogging.main_decorator
2021-05-09 01:27:18 +00:00
def main(argv):
2022-02-13 03:50:00 +00:00
parser = argparse.ArgumentParser(
description='''
Exits with status of 0 if the disk has the requested amount of space, 1 if not.
''',
)
parser.add_argument(
'reserve',
type=str,
help='''
A string like "50g" or "100 gb"
''',
)
parser.add_argument(
'drive',
nargs='?',
default='.',
help='''
Filepath to the drive you want to check.
''',
)
2021-05-09 01:27:18 +00:00
parser.set_defaults(func=reserve_disk_space_argparse)
2022-02-13 03:50:00 +00:00
return betterhelp.go(parser, argv)
2021-05-09 01:27:18 +00:00
if __name__ == '__main__':
raise SystemExit(main(sys.argv[1:]))