borgmatic/atticmatic/command.py

50 lines
1.4 KiB
Python
Raw Normal View History

2014-10-31 05:34:03 +00:00
from __future__ import print_function
from argparse import ArgumentParser
from subprocess import CalledProcessError
import sys
from atticmatic.attic import create_archive, prune_archives
from atticmatic.config import parse_configuration
DEFAULT_CONFIG_FILENAME = '/etc/atticmatic/config'
DEFAULT_EXCLUDES_FILENAME = '/etc/atticmatic/excludes'
def parse_arguments(*arguments):
'''
Parse the command-line arguments from sys.argv and return them as an ArgumentParser instance.
'''
2014-10-31 05:34:03 +00:00
parser = ArgumentParser()
parser.add_argument(
'-c', '--config',
2014-10-31 05:34:03 +00:00
dest='config_filename',
default=DEFAULT_CONFIG_FILENAME,
2014-10-31 05:34:03 +00:00
help='Configuration filename',
)
parser.add_argument(
'--excludes',
dest='excludes_filename',
default=DEFAULT_EXCLUDES_FILENAME,
2014-10-31 05:34:03 +00:00
help='Excludes filename',
)
parser.add_argument(
'-v', '--verbose',
2014-10-31 05:34:03 +00:00
action='store_true',
help='Display verbose progress information',
)
return parser.parse_args(arguments)
def main():
2014-10-31 05:34:03 +00:00
try:
args = parse_arguments(*sys.argv[1:])
2014-10-31 05:34:03 +00:00
location_config, retention_config = parse_configuration(args.config_filename)
create_archive(args.excludes_filename, args.verbose, **location_config)
prune_archives(args.verbose, location_config['repository'], retention_config)
except (ValueError, IOError, CalledProcessError) as error:
2014-10-31 05:34:03 +00:00
print(error, file=sys.stderr)
sys.exit(1)