mirror of
https://github.com/psy0rz/zfs_autobackup.git
synced 2025-04-13 22:47:12 +03:00
extracted clibase class (for zfs-check tool)
This commit is contained in:
parent
60560b884b
commit
f0d00aa4e8
90
zfs_autobackup/CliBase.py
Normal file
90
zfs_autobackup/CliBase.py
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
import argparse
|
||||||
|
import os.path
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from .LogConsole import LogConsole
|
||||||
|
|
||||||
|
|
||||||
|
class CliBase(object):
|
||||||
|
"""Base class for all cli programs
|
||||||
|
Overridden in subclasses that add stuff for the specific programs."""
|
||||||
|
|
||||||
|
# also used by setup.py
|
||||||
|
VERSION = "3.2-alpha2"
|
||||||
|
HEADER = "{} v{} - (c)2022 E.H.Eefting (edwin@datux.nl)".format(os.path.basename(sys.argv[0]), VERSION)
|
||||||
|
|
||||||
|
def __init__(self, argv, print_arguments=True):
|
||||||
|
|
||||||
|
self.parser=self.get_parser()
|
||||||
|
self.args = self.parse_args(argv)
|
||||||
|
|
||||||
|
# helps with investigating failed regression tests:
|
||||||
|
if print_arguments:
|
||||||
|
print("ARGUMENTS: " + " ".join(argv))
|
||||||
|
|
||||||
|
def parse_args(self, argv):
|
||||||
|
"""parses the arguments and does additional checks, might print warnings or notes
|
||||||
|
Overridden in subclasses with extra checks.
|
||||||
|
"""
|
||||||
|
|
||||||
|
args = self.parser.parse_args(argv)
|
||||||
|
|
||||||
|
if args.help:
|
||||||
|
self.parser.print_help()
|
||||||
|
sys.exit(255)
|
||||||
|
|
||||||
|
if args.version:
|
||||||
|
print(self.HEADER)
|
||||||
|
sys.exit(255)
|
||||||
|
|
||||||
|
# auto enable progress?
|
||||||
|
if sys.stderr.isatty() and not args.no_progress:
|
||||||
|
args.progress = True
|
||||||
|
|
||||||
|
if args.debug_output:
|
||||||
|
args.debug = True
|
||||||
|
|
||||||
|
if args.test:
|
||||||
|
args.verbose = True
|
||||||
|
|
||||||
|
if args.debug:
|
||||||
|
args.verbose = True
|
||||||
|
|
||||||
|
self.log = LogConsole(show_debug=args.debug, show_verbose=args.verbose, color=sys.stdout.isatty())
|
||||||
|
|
||||||
|
self.verbose(self.HEADER)
|
||||||
|
self.verbose("")
|
||||||
|
|
||||||
|
return args
|
||||||
|
|
||||||
|
def get_parser(self):
|
||||||
|
"""build up the argument parser
|
||||||
|
Overridden in subclasses that add extra arguments
|
||||||
|
"""
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(description=self.HEADER, add_help=False,
|
||||||
|
epilog='Full manual at: https://github.com/psy0rz/zfs_autobackup')
|
||||||
|
|
||||||
|
return parser
|
||||||
|
|
||||||
|
def verbose(self, txt):
|
||||||
|
self.log.verbose(txt)
|
||||||
|
|
||||||
|
def warning(self, txt):
|
||||||
|
self.log.warning(txt)
|
||||||
|
|
||||||
|
def error(self, txt):
|
||||||
|
self.log.error(txt)
|
||||||
|
|
||||||
|
def debug(self, txt):
|
||||||
|
self.log.debug(txt)
|
||||||
|
|
||||||
|
def progress(self, txt):
|
||||||
|
self.log.progress(txt)
|
||||||
|
|
||||||
|
def clear_progress(self):
|
||||||
|
self.log.clear_progress()
|
||||||
|
|
||||||
|
def set_title(self, title):
|
||||||
|
self.log.verbose("")
|
||||||
|
self.log.verbose("#### " + title)
|
@ -1,16 +1,11 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import os.path
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from .LogConsole import LogConsole
|
from .CliBase import CliBase
|
||||||
|
|
||||||
|
|
||||||
class ZfsAuto(object):
|
class ZfsAuto(CliBase):
|
||||||
"""Common Base class, this class is always used subclassed. Look at ZfsAutobackup and ZfsAutoverify ."""
|
"""Common Base class for ZfsAutobackup and ZfsAutoverify ."""
|
||||||
|
|
||||||
# also used by setup.py
|
|
||||||
VERSION = "3.2-alpha2"
|
|
||||||
HEADER = "{} v{} - (c)2021 E.H.Eefting (edwin@datux.nl)".format(os.path.basename(sys.argv[0]), VERSION)
|
|
||||||
|
|
||||||
def __init__(self, argv, print_arguments=True):
|
def __init__(self, argv, print_arguments=True):
|
||||||
|
|
||||||
@ -19,46 +14,15 @@ class ZfsAuto(object):
|
|||||||
self.property_name = None
|
self.property_name = None
|
||||||
self.exclude_paths = None
|
self.exclude_paths = None
|
||||||
|
|
||||||
# helps with investigating failed regression tests:
|
super(ZfsAuto, self).__init__(argv, print_arguments)
|
||||||
if print_arguments:
|
|
||||||
print("ARGUMENTS: " + " ".join(argv))
|
|
||||||
|
|
||||||
self.args = self.parse_args(argv)
|
|
||||||
|
|
||||||
def parse_args(self, argv):
|
def parse_args(self, argv):
|
||||||
"""parse common arguments, setup logging, check and adjust parameters"""
|
"""parse common arguments, setup logging, check and adjust parameters"""
|
||||||
|
|
||||||
parser=self.get_parser()
|
args = super(ZfsAuto, self).parse_args(argv)
|
||||||
args = parser.parse_args(argv)
|
|
||||||
|
|
||||||
if args.help:
|
|
||||||
parser.print_help()
|
|
||||||
sys.exit(255)
|
|
||||||
|
|
||||||
if args.version:
|
|
||||||
print(self.HEADER)
|
|
||||||
sys.exit(255)
|
|
||||||
|
|
||||||
# auto enable progress?
|
|
||||||
if sys.stderr.isatty() and not args.no_progress:
|
|
||||||
args.progress = True
|
|
||||||
|
|
||||||
if args.debug_output:
|
|
||||||
args.debug = True
|
|
||||||
|
|
||||||
if args.test:
|
|
||||||
args.verbose = True
|
|
||||||
|
|
||||||
if args.debug:
|
|
||||||
args.verbose = True
|
|
||||||
|
|
||||||
self.log = LogConsole(show_debug=args.debug, show_verbose=args.verbose, color=sys.stdout.isatty())
|
|
||||||
|
|
||||||
self.verbose(self.HEADER)
|
|
||||||
self.verbose("")
|
|
||||||
|
|
||||||
if args.backup_name == None:
|
if args.backup_name == None:
|
||||||
parser.print_usage()
|
self.parser.print_usage()
|
||||||
self.log.error("Please specify BACKUP-NAME")
|
self.log.error("Please specify BACKUP-NAME")
|
||||||
sys.exit(255)
|
sys.exit(255)
|
||||||
|
|
||||||
@ -102,8 +66,7 @@ class ZfsAuto(object):
|
|||||||
|
|
||||||
def get_parser(self):
|
def get_parser(self):
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description=self.HEADER, add_help=False,
|
parser = super(ZfsAuto, self).get_parser()
|
||||||
epilog='Full manual at: https://github.com/psy0rz/zfs_autobackup')
|
|
||||||
|
|
||||||
#positional arguments
|
#positional arguments
|
||||||
parser.add_argument('backup_name', metavar='BACKUP-NAME', default=None, nargs='?',
|
parser.add_argument('backup_name', metavar='BACKUP-NAME', default=None, nargs='?',
|
||||||
@ -159,28 +122,6 @@ class ZfsAuto(object):
|
|||||||
|
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
def verbose(self, txt):
|
|
||||||
self.log.verbose(txt)
|
|
||||||
|
|
||||||
def warning(self, txt):
|
|
||||||
self.log.warning(txt)
|
|
||||||
|
|
||||||
def error(self, txt):
|
|
||||||
self.log.error(txt)
|
|
||||||
|
|
||||||
def debug(self, txt):
|
|
||||||
self.log.debug(txt)
|
|
||||||
|
|
||||||
def progress(self, txt):
|
|
||||||
self.log.progress(txt)
|
|
||||||
|
|
||||||
def clear_progress(self):
|
|
||||||
self.log.clear_progress()
|
|
||||||
|
|
||||||
def set_title(self, title):
|
|
||||||
self.log.verbose("")
|
|
||||||
self.log.verbose("#### " + title)
|
|
||||||
|
|
||||||
def print_error_sources(self):
|
def print_error_sources(self):
|
||||||
self.error(
|
self.error(
|
||||||
"No source filesystems selected, please do a 'zfs set autobackup:{0}=true' on the source datasets "
|
"No source filesystems selected, please do a 'zfs set autobackup:{0}=true' on the source datasets "
|
||||||
|
Loading…
x
Reference in New Issue
Block a user