command.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from __future__ import print_function
  2. from argparse import ArgumentParser
  3. from subprocess import CalledProcessError
  4. import sys
  5. from atticmatic.attic import check_archives, create_archive, prune_archives
  6. from atticmatic.config import parse_configuration
  7. DEFAULT_CONFIG_FILENAME = '/etc/atticmatic/config'
  8. DEFAULT_EXCLUDES_FILENAME = '/etc/atticmatic/excludes'
  9. def parse_arguments(*arguments):
  10. '''
  11. Parse the given command-line arguments and return them as an ArgumentParser instance.
  12. '''
  13. parser = ArgumentParser()
  14. parser.add_argument(
  15. '-c', '--config',
  16. dest='config_filename',
  17. default=DEFAULT_CONFIG_FILENAME,
  18. help='Configuration filename',
  19. )
  20. parser.add_argument(
  21. '--excludes',
  22. dest='excludes_filename',
  23. default=DEFAULT_EXCLUDES_FILENAME,
  24. help='Excludes filename',
  25. )
  26. parser.add_argument(
  27. '-v', '--verbose',
  28. action='store_true',
  29. help='Display verbose progress information',
  30. )
  31. return parser.parse_args(arguments)
  32. def main():
  33. try:
  34. args = parse_arguments(*sys.argv[1:])
  35. location_config, retention_config = parse_configuration(args.config_filename)
  36. repository = location_config['repository']
  37. create_archive(args.excludes_filename, args.verbose, **location_config)
  38. prune_archives(args.verbose, repository, retention_config)
  39. check_archives(args.verbose, repository)
  40. except (ValueError, IOError, CalledProcessError, RuntimeError) as error:
  41. print(error, file=sys.stderr)
  42. sys.exit(1)