Browse Source

update borg/recreate.py

Vandal 4 months ago
parent
commit
4e2805918d
1 changed files with 35 additions and 29 deletions
  1. 35 29
      borgmatic/borg/recreate.py

+ 35 - 29
borgmatic/borg/recreate.py

@@ -1,21 +1,18 @@
-import argparse
 import logging
 import logging
 
 
 import borgmatic.borg.environment
 import borgmatic.borg.environment
-import borgmatic.borg.feature
-import borgmatic.borg.flags
-import borgmatic.borg.repo_delete
 import borgmatic.config.paths
 import borgmatic.config.paths
 import borgmatic.execute
 import borgmatic.execute
-from borgmatic.borg.create import make_exclude_flags, write_patterns_file
+from borgmatic.borg.create import make_exclude_flags
+from borgmatic.borg.flags import make_flags_from_arguments, make_repository_archive_flags
 
 
 logger = logging.getLogger(__name__)
 logger = logging.getLogger(__name__)
 
 
 
 
 def make_recreate_command(
 def make_recreate_command(
     repository,
     repository,
+    archive,
     config,
     config,
-    patterns,
     local_borg_version,
     local_borg_version,
     recreate_arguments,
     recreate_arguments,
     global_arguments,
     global_arguments,
@@ -23,30 +20,39 @@ def make_recreate_command(
     remote_path=None,
     remote_path=None,
 ):
 ):
     '''
     '''
-    Given a repository path, configuration dict, patterns, and Borg options, return a command
-    list for recreating a Borg archive.
+    Given a local or remote repository path, an archive name, a configuration dict,
+    the local Borg version string, an argparse.Namespace of recreate arguments,
+    an argparse.Namespace of global arguments, optional local and remote Borg paths.
+
+    Returns the recreate command as a tuple of strings ready for execution.
     '''
     '''
-    verbosity_flags = ()
-    if logger.isEnabledFor(logging.DEBUG):
-        verbosity_flags = ('--debug', '--show-rc')
-    elif logger.isEnabledFor(logging.INFO):
-        verbosity_flags = ('--info',)
+    verbosity_flags = (('--debug', '--show-rc') if logger.isEnabledFor(logging.DEBUG) else ()) + (
+        ('--info',) if logger.isEnabledFor(logging.INFO) else ()
+    )
 
 
-    command = [local_path, 'recreate', repository]
-    command.extend(verbosity_flags)
-    command.extend(global_arguments)
-    command.extend(recreate_arguments)
+    # handle both the recreate and global arguments
+    recreate_flags = make_flags_from_arguments(
+        recreate_arguments, excludes=('repository', 'archive')
+    )
+    global_flags = make_flags_from_arguments(global_arguments)
 
 
+    repo_archive_flags = make_repository_archive_flags(repository, archive, local_borg_version)
     exclude_flags = make_exclude_flags(config)
     exclude_flags = make_exclude_flags(config)
-    command.extend(exclude_flags)
 
 
-    return command
+    return (
+        (local_path, 'recreate')
+        + repo_archive_flags
+        + verbosity_flags
+        + global_flags
+        + recreate_flags
+        + exclude_flags
+    )
 
 
 
 
 def recreate_archive(
 def recreate_archive(
     repository,
     repository,
+    archive,
     config,
     config,
-    patterns,
     local_borg_version,
     local_borg_version,
     recreate_arguments,
     recreate_arguments,
     global_arguments,
     global_arguments,
@@ -54,12 +60,16 @@ def recreate_archive(
     remote_path=None,
     remote_path=None,
 ):
 ):
     '''
     '''
-    Recreate a Borg archive with the given repository and configuration.
+    Given a local or remote repository path, an archive name, a configuration dict,
+    the local Borg version string, an argparse.Namespace of recreate arguments,
+    an argparse.Namespace of global arguments, optional local and remote Borg paths.
+
+    Executes the recreate command with the given arguments.
     '''
     '''
     command = make_recreate_command(
     command = make_recreate_command(
         repository,
         repository,
+        archive,
         config,
         config,
-        patterns,
         local_borg_version,
         local_borg_version,
         recreate_arguments,
         recreate_arguments,
         global_arguments,
         global_arguments,
@@ -67,16 +77,12 @@ def recreate_archive(
         remote_path,
         remote_path,
     )
     )
 
 
-    patterns_file = write_patterns_file(patterns, borgmatic.config.paths.get_runtime_directory())
-    if patterns_file:
-        command.extend(['--patterns-from', patterns_file.name])
-
     borgmatic.execute.execute_command(
     borgmatic.execute.execute_command(
         command,
         command,
         output_log_level=logging.ANSWER,
         output_log_level=logging.ANSWER,
         environment=borgmatic.borg.environment.make_environment(config),
         environment=borgmatic.borg.environment.make_environment(config),
         working_directory=borgmatic.config.paths.get_working_directory(config),
         working_directory=borgmatic.config.paths.get_working_directory(config),
-        remote_path=remote_path, 
+        remote_path=remote_path,
         borg_local_path=local_path,
         borg_local_path=local_path,
-        borg_exit_codes=config.get('borg_exit_codes')
-        )
+        borg_exit_codes=config.get('borg_exit_codes'),
+    )