command.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from __future__ import print_function
  2. from argparse import ArgumentParser
  3. from subprocess import CalledProcessError
  4. import sys
  5. from atticmatic.attic import 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. create_archive(args.excludes_filename, args.verbose, **location_config)
  37. prune_archives(args.verbose, location_config['repository'], retention_config)
  38. except (ValueError, IOError, CalledProcessError) as error:
  39. print(error, file=sys.stderr)
  40. sys.exit(1)