attic.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from datetime import datetime
  2. import platform
  3. import subprocess
  4. def create_archive(excludes_filename, verbose, source_directories, repository):
  5. '''
  6. Given an excludes filename, a vebosity flag, a space-separated list of source directories, and
  7. a local or remote repository path, create an attic archive.
  8. '''
  9. sources = tuple(source_directories.split(' '))
  10. command = (
  11. 'attic', 'create',
  12. '--exclude-from', excludes_filename,
  13. '{repo}::{hostname}-{timestamp}'.format(
  14. repo=repository,
  15. hostname=platform.node(),
  16. timestamp=datetime.now().isoformat(),
  17. ),
  18. ) + sources + (
  19. ('--verbose', '--stats') if verbose else ()
  20. )
  21. subprocess.check_call(command)
  22. def make_prune_flags(retention_config):
  23. '''
  24. Given a retention config dict mapping from option name to value, tranform it into an iterable of
  25. command-line name-value flag pairs.
  26. For example, given a retention config of:
  27. {'keep_weekly': 4, 'keep_monthly': 6}
  28. This will be returned as an iterable of:
  29. (
  30. ('--keep-weekly', '4'),
  31. ('--keep-monthly', '6'),
  32. )
  33. '''
  34. return (
  35. ('--' + option_name.replace('_', '-'), str(retention_config[option_name]))
  36. for option_name, value in retention_config.items()
  37. )
  38. def prune_archives(verbose, repository, retention_config):
  39. '''
  40. Given a verbosity flag, a local or remote repository path, and a retention config dict, prune
  41. attic archives according the the retention policy specified in that configuration.
  42. '''
  43. command = (
  44. 'attic', 'prune',
  45. repository,
  46. ) + tuple(
  47. element
  48. for pair in make_prune_flags(retention_config)
  49. for element in pair
  50. ) + (('--verbose',) if verbose else ())
  51. subprocess.check_call(command)