shared.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. from datetime import datetime
  2. import os
  3. import platform
  4. import subprocess
  5. from atticmatic.verbosity import VERBOSITY_SOME, VERBOSITY_LOTS
  6. # Common backend functionality shared by Attic and Borg. As the two backup
  7. # commands diverge, these shared functions will likely need to be replaced
  8. # with non-shared functions within atticmatic.backends.attic and
  9. # atticmatic.backends.borg.
  10. def create_archive(excludes_filename, verbosity, source_directories, repository, command):
  11. '''
  12. Given an excludes filename, a vebosity flag, a space-separated list of source directories, a
  13. local or remote repository path, and a command to run, create an attic archive.
  14. '''
  15. sources = tuple(source_directories.split(' '))
  16. verbosity_flags = {
  17. VERBOSITY_SOME: ('--stats',),
  18. VERBOSITY_LOTS: ('--verbose', '--stats'),
  19. }.get(verbosity, ())
  20. full_command = (
  21. command, 'create',
  22. '--exclude-from', excludes_filename,
  23. '{repo}::{hostname}-{timestamp}'.format(
  24. repo=repository,
  25. hostname=platform.node(),
  26. timestamp=datetime.now().isoformat(),
  27. ),
  28. ) + sources + verbosity_flags
  29. subprocess.check_call(full_command)
  30. def _make_prune_flags(retention_config):
  31. '''
  32. Given a retention config dict mapping from option name to value, tranform it into an iterable of
  33. command-line name-value flag pairs.
  34. For example, given a retention config of:
  35. {'keep_weekly': 4, 'keep_monthly': 6}
  36. This will be returned as an iterable of:
  37. (
  38. ('--keep-weekly', '4'),
  39. ('--keep-monthly', '6'),
  40. )
  41. '''
  42. return (
  43. ('--' + option_name.replace('_', '-'), str(retention_config[option_name]))
  44. for option_name, value in retention_config.items()
  45. )
  46. def prune_archives(verbosity, repository, retention_config, command):
  47. '''
  48. Given a verbosity flag, a local or remote repository path, a retention config dict, and a
  49. command to run, prune attic archives according the the retention policy specified in that
  50. configuration.
  51. '''
  52. verbosity_flags = {
  53. VERBOSITY_SOME: ('--stats',),
  54. VERBOSITY_LOTS: ('--verbose', '--stats'),
  55. }.get(verbosity, ())
  56. full_command = (
  57. command, 'prune',
  58. repository,
  59. ) + tuple(
  60. element
  61. for pair in _make_prune_flags(retention_config)
  62. for element in pair
  63. ) + verbosity_flags
  64. subprocess.check_call(full_command)
  65. DEFAULT_CHECKS = ('repository', 'archives')
  66. def _parse_checks(consistency_config):
  67. '''
  68. Given a consistency config with a space-separated "checks" option, transform it to a tuple of
  69. named checks to run.
  70. For example, given a retention config of:
  71. {'checks': 'repository archives'}
  72. This will be returned as:
  73. ('repository', 'archives')
  74. If no "checks" option is present, return the DEFAULT_CHECKS. If the checks value is the string
  75. "disabled", return an empty tuple, meaning that no checks should be run.
  76. '''
  77. checks = consistency_config.get('checks', '').strip()
  78. if not checks:
  79. return DEFAULT_CHECKS
  80. return tuple(
  81. check for check in consistency_config['checks'].split(' ')
  82. if check.lower() not in ('disabled', '')
  83. )
  84. def _make_check_flags(checks):
  85. '''
  86. Given a parsed sequence of checks, transform it into tuple of command-line flags.
  87. For example, given parsed checks of:
  88. ('repository',)
  89. This will be returned as:
  90. ('--repository-only',)
  91. '''
  92. if checks == DEFAULT_CHECKS:
  93. return ()
  94. return tuple(
  95. '--{}-only'.format(check) for check in checks
  96. )
  97. def check_archives(verbosity, repository, consistency_config, command):
  98. '''
  99. Given a verbosity flag, a local or remote repository path, a consistency config dict, and a
  100. command to run, check the contained attic archives for consistency.
  101. If there are no consistency checks to run, skip running them.
  102. '''
  103. checks = _parse_checks(consistency_config)
  104. if not checks:
  105. return
  106. verbosity_flags = {
  107. VERBOSITY_SOME: ('--verbose',),
  108. VERBOSITY_LOTS: ('--verbose',),
  109. }.get(verbosity, ())
  110. full_command = (
  111. command, 'check',
  112. repository,
  113. ) + _make_check_flags(checks) + verbosity_flags
  114. # The check command spews to stdout even without the verbose flag. Suppress it.
  115. stdout = None if verbosity_flags else open(os.devnull, 'w')
  116. subprocess.check_call(full_command, stdout=stdout)