break_lock.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import logging
  2. from borgmatic.borg import environment, flags
  3. from borgmatic.execute import execute_command
  4. logger = logging.getLogger(__name__)
  5. def break_lock(
  6. repository_path,
  7. storage_config,
  8. local_borg_version,
  9. global_arguments,
  10. local_path='borg',
  11. remote_path=None,
  12. ):
  13. '''
  14. Given a local or remote repository path, a storage configuration dict, the local Borg version,
  15. an argparse.Namespace of global arguments, and optional local and remote Borg paths, break any
  16. repository and cache locks leftover from Borg aborting.
  17. '''
  18. umask = storage_config.get('umask', None)
  19. lock_wait = storage_config.get('lock_wait', None)
  20. full_command = (
  21. (local_path, 'break-lock')
  22. + (('--remote-path', remote_path) if remote_path else ())
  23. + (('--umask', str(umask)) if umask else ())
  24. + (('--log-json',) if global_arguments.log_json else ())
  25. + (('--lock-wait', str(lock_wait)) if lock_wait else ())
  26. + (('--info',) if logger.getEffectiveLevel() == logging.INFO else ())
  27. + (('--debug', '--show-rc') if logger.isEnabledFor(logging.DEBUG) else ())
  28. + flags.make_repository_flags(repository_path, local_borg_version)
  29. )
  30. borg_environment = environment.make_environment(storage_config)
  31. execute_command(full_command, borg_local_path=local_path, extra_environment=borg_environment)