mount.py 3.3 KB

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