attic.py 4.1 KB

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