info.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import argparse
  2. import logging
  3. import shlex
  4. import borgmatic.config.paths
  5. import borgmatic.logger
  6. from borgmatic.borg import environment, feature, flags
  7. from borgmatic.execute import execute_command, execute_command_and_capture_output
  8. logger = logging.getLogger(__name__)
  9. def make_info_command(
  10. repository_path,
  11. config,
  12. local_borg_version,
  13. info_arguments,
  14. global_arguments,
  15. local_path,
  16. remote_path,
  17. ):
  18. '''
  19. Given a local or remote repository path, a configuration dict, the local Borg version, the
  20. arguments to the info action as an argparse.Namespace, and global arguments, return a command
  21. as a tuple to display summary information for archives in the repository.
  22. '''
  23. extra_borg_options = config.get('extra_borg_options', {}).get('info', '')
  24. return (
  25. (local_path, 'info')
  26. + (
  27. ('--info',)
  28. if logger.getEffectiveLevel() == logging.INFO and not info_arguments.json
  29. else ()
  30. )
  31. + (
  32. ('--debug', '--show-rc')
  33. if logger.isEnabledFor(logging.DEBUG) and not info_arguments.json
  34. else ()
  35. )
  36. + flags.make_flags('remote-path', remote_path)
  37. + flags.make_flags('umask', config.get('umask'))
  38. + flags.make_flags('log-json', config.get('log_json'))
  39. + flags.make_flags('lock-wait', config.get('lock_wait'))
  40. + (
  41. (
  42. flags.make_flags('match-archives', f'sh:{info_arguments.prefix}*')
  43. if feature.available(feature.Feature.MATCH_ARCHIVES, local_borg_version)
  44. else flags.make_flags('glob-archives', f'{info_arguments.prefix}*')
  45. )
  46. if info_arguments.prefix
  47. else (
  48. flags.make_match_archives_flags(
  49. info_arguments.archive or config.get('match_archives'),
  50. config.get('archive_name_format'),
  51. local_borg_version,
  52. )
  53. )
  54. )
  55. + flags.make_flags_from_arguments(
  56. info_arguments,
  57. excludes=('repository', 'archive', 'prefix', 'match_archives'),
  58. )
  59. + (tuple(shlex.split(extra_borg_options)) if extra_borg_options else ())
  60. + flags.make_repository_flags(repository_path, local_borg_version)
  61. )
  62. def display_archives_info(
  63. repository_path,
  64. config,
  65. local_borg_version,
  66. info_arguments,
  67. global_arguments,
  68. local_path='borg',
  69. remote_path=None,
  70. ):
  71. '''
  72. Given a local or remote repository path, a configuration dict, the local Borg version, the
  73. arguments to the info action as an argparse.Namespace, and global arguments, display summary
  74. information for Borg archives in the repository or return JSON summary information.
  75. '''
  76. borgmatic.logger.add_custom_log_levels()
  77. main_command = make_info_command(
  78. repository_path,
  79. config,
  80. local_borg_version,
  81. info_arguments,
  82. global_arguments,
  83. local_path,
  84. remote_path,
  85. )
  86. json_command = make_info_command(
  87. repository_path,
  88. config,
  89. local_borg_version,
  90. argparse.Namespace(**dict(info_arguments.__dict__, json=True)),
  91. global_arguments,
  92. local_path,
  93. remote_path,
  94. )
  95. borg_exit_codes = config.get('borg_exit_codes')
  96. working_directory = borgmatic.config.paths.get_working_directory(config)
  97. json_info = execute_command_and_capture_output(
  98. json_command,
  99. environment=environment.make_environment(config),
  100. working_directory=working_directory,
  101. borg_local_path=local_path,
  102. borg_exit_codes=borg_exit_codes,
  103. )
  104. if info_arguments.json:
  105. return json_info
  106. flags.warn_for_aggressive_archive_flags(json_command, json_info)
  107. execute_command(
  108. main_command,
  109. output_log_level=logging.ANSWER,
  110. environment=environment.make_environment(config),
  111. working_directory=working_directory,
  112. borg_local_path=local_path,
  113. borg_exit_codes=borg_exit_codes,
  114. )
  115. return None