mount.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import logging
  2. from borgmatic.borg import environment, feature, flags
  3. from borgmatic.execute import DO_NOT_CAPTURE, execute_command
  4. logger = logging.getLogger(__name__)
  5. def mount_archive(
  6. repository_path,
  7. archive,
  8. mount_arguments,
  9. config,
  10. local_borg_version,
  11. global_arguments,
  12. local_path='borg',
  13. remote_path=None,
  14. ):
  15. '''
  16. Given a local or remote repository path, an optional archive name, a filesystem mount point,
  17. zero or more paths to mount from the archive, extra Borg mount options, a storage configuration
  18. dict, the local Borg version, global arguments as an argparse.Namespace instance, and optional
  19. local and remote Borg paths, mount the archive onto the mount point.
  20. '''
  21. umask = config.get('umask', None)
  22. lock_wait = config.get('lock_wait', None)
  23. full_command = (
  24. (local_path, 'mount')
  25. + (('--remote-path', remote_path) if remote_path else ())
  26. + (('--umask', str(umask)) if umask else ())
  27. + (('--log-json',) if global_arguments.log_json else ())
  28. + (('--lock-wait', str(lock_wait)) if lock_wait else ())
  29. + (('--info',) if logger.getEffectiveLevel() == logging.INFO else ())
  30. + (('--debug', '--show-rc') if logger.isEnabledFor(logging.DEBUG) else ())
  31. + flags.make_flags_from_arguments(
  32. mount_arguments,
  33. excludes=('repository', 'archive', 'mount_point', 'paths', 'options'),
  34. )
  35. + (('-o', mount_arguments.options) if mount_arguments.options else ())
  36. + (
  37. (
  38. flags.make_repository_flags(repository_path, local_borg_version)
  39. + (
  40. ('--match-archives', archive)
  41. if feature.available(feature.Feature.MATCH_ARCHIVES, local_borg_version)
  42. else ('--glob-archives', archive)
  43. )
  44. )
  45. if feature.available(feature.Feature.SEPARATE_REPOSITORY_ARCHIVE, local_borg_version)
  46. else (
  47. flags.make_repository_archive_flags(repository_path, archive, local_borg_version)
  48. if archive
  49. else flags.make_repository_flags(repository_path, local_borg_version)
  50. )
  51. )
  52. + (mount_arguments.mount_point,)
  53. + (tuple(mount_arguments.paths) if mount_arguments.paths else ())
  54. )
  55. borg_environment = environment.make_environment(config)
  56. # Don't capture the output when foreground mode is used so that ctrl-C can work properly.
  57. if mount_arguments.foreground:
  58. execute_command(
  59. full_command,
  60. output_file=DO_NOT_CAPTURE,
  61. extra_environment=borg_environment,
  62. borg_local_path=local_path,
  63. borg_exit_codes=config.get('borg_exit_codes'),
  64. )
  65. return
  66. execute_command(
  67. full_command,
  68. extra_environment=borg_environment,
  69. borg_local_path=local_path,
  70. borg_exit_codes=config.get('borg_exit_codes'),
  71. )