attic.py 3.7 KB

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