attic.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from __future__ import print_function
  2. from datetime import datetime
  3. import os
  4. import platform
  5. import re
  6. import subprocess
  7. import sys
  8. def create_archive(excludes_filename, verbose, source_directories, repository):
  9. '''
  10. Given an excludes filename, a vebosity flag, a space-separated list of source directories, and
  11. a local or remote repository path, create an attic archive.
  12. '''
  13. sources = tuple(source_directories.split(' '))
  14. command = (
  15. 'attic', 'create',
  16. '--exclude-from', excludes_filename,
  17. '{repo}::{hostname}-{timestamp}'.format(
  18. repo=repository,
  19. hostname=platform.node(),
  20. timestamp=datetime.now().isoformat(),
  21. ),
  22. ) + sources + (
  23. ('--verbose', '--stats') if verbose else ()
  24. )
  25. try:
  26. subprocess.check_output(command, stderr=subprocess.STDOUT)
  27. except subprocess.CalledProcessError, error:
  28. print(error.output.strip(), file=sys.stderr)
  29. if re.search('Error: Repository .* does not exist', error.output):
  30. raise RuntimeError('To create a repository, run: attic init --encryption=keyfile {}'.format(repository))
  31. raise error
  32. def make_prune_flags(retention_config):
  33. '''
  34. Given a retention config dict mapping from option name to value, tranform it into an iterable of
  35. command-line name-value flag pairs.
  36. For example, given a retention config of:
  37. {'keep_weekly': 4, 'keep_monthly': 6}
  38. This will be returned as an iterable of:
  39. (
  40. ('--keep-weekly', '4'),
  41. ('--keep-monthly', '6'),
  42. )
  43. '''
  44. return (
  45. ('--' + option_name.replace('_', '-'), str(retention_config[option_name]))
  46. for option_name, value in retention_config.items()
  47. )
  48. def prune_archives(verbose, repository, retention_config):
  49. '''
  50. Given a verbosity flag, a local or remote repository path, and a retention config dict, prune
  51. attic archives according the the retention policy specified in that configuration.
  52. '''
  53. command = (
  54. 'attic', 'prune',
  55. repository,
  56. ) + tuple(
  57. element
  58. for pair in make_prune_flags(retention_config)
  59. for element in pair
  60. ) + (('--verbose',) if verbose else ())
  61. subprocess.check_call(command)
  62. def check_archives(verbose, repository):
  63. '''
  64. Given a verbosity flag and a local or remote repository path, check the contained attic archives
  65. for consistency.
  66. '''
  67. command = (
  68. 'attic', 'check',
  69. repository,
  70. ) + (('--verbose',) if verbose else ())
  71. # Attic's check command spews to stdout even without the verbose flag. Suppress it.
  72. stdout = None if verbose else open(os.devnull, 'w')
  73. subprocess.check_call(command, stdout=stdout)