Add "borgmatic extract --strip-components" flag to remove leading path components when extracting an archive (#324).

This commit is contained in:
Dan Helfman 2020-06-06 14:57:14 -07:00
parent ed7b1cd3d7
commit aa14449857
6 changed files with 28 additions and 1 deletions

2
NEWS
View file

@ -7,6 +7,8 @@
disk. disk.
* #323: Fix for certain configuration options like ssh_command impacting Borg invocations for * #323: Fix for certain configuration options like ssh_command impacting Borg invocations for
separate configuration files. separate configuration files.
* #324: Add "borgmatic extract --strip-components" flag to remove leading path components when
extracting an archive.
* Tweak comment indentation in generated configuration file for clarity. * Tweak comment indentation in generated configuration file for clarity.
* Link to Borgmacator GNOME AppIndicator from monitoring documentation. * Link to Borgmacator GNOME AppIndicator from monitoring documentation.

View file

@ -64,6 +64,7 @@ def extract_archive(
local_path='borg', local_path='borg',
remote_path=None, remote_path=None,
destination_path=None, destination_path=None,
strip_components=None,
progress=False, progress=False,
extract_to_stdout=False, extract_to_stdout=False,
): ):
@ -91,6 +92,7 @@ def extract_archive(
+ (('--info',) if logger.getEffectiveLevel() == logging.INFO else ()) + (('--info',) if logger.getEffectiveLevel() == logging.INFO else ())
+ (('--debug', '--list', '--show-rc') if logger.isEnabledFor(logging.DEBUG) else ()) + (('--debug', '--list', '--show-rc') if logger.isEnabledFor(logging.DEBUG) else ())
+ (('--dry-run',) if dry_run else ()) + (('--dry-run',) if dry_run else ())
+ (('--strip-components', str(strip_components)) if strip_components else ())
+ (('--progress',) if progress else ()) + (('--progress',) if progress else ())
+ (('--stdout',) if extract_to_stdout else ()) + (('--stdout',) if extract_to_stdout else ())
+ ('::'.join((repository if ':' in repository else os.path.abspath(repository), archive)),) + ('::'.join((repository if ':' in repository else os.path.abspath(repository), archive)),)

View file

@ -340,6 +340,13 @@ def parse_arguments(*unparsed_arguments):
dest='destination', dest='destination',
help='Directory to extract files into, defaults to the current directory', help='Directory to extract files into, defaults to the current directory',
) )
extract_group.add_argument(
'--strip-components',
type=int,
metavar='NUMBER',
dest='strip_components',
help='Number of leading path components to remove from each extracted path. Skip paths with fewer elements',
)
extract_group.add_argument( extract_group.add_argument(
'--progress', '--progress',
dest='progress', dest='progress',

View file

@ -328,6 +328,7 @@ def run_actions(
local_path=local_path, local_path=local_path,
remote_path=remote_path, remote_path=remote_path,
destination_path=arguments['extract'].destination, destination_path=arguments['extract'].destination,
strip_components=arguments['extract'].strip_components,
progress=arguments['extract'].progress, progress=arguments['extract'].progress,
) )
if 'mount' in arguments: if 'mount' in arguments:

View file

@ -62,7 +62,7 @@ def test_initialize_with_relocated_repo_access_should_override_default():
os.environ = orig_environ os.environ = orig_environ
def test_initialize_is_not_effected_by_existing_environment(): def test_initialize_is_not_affected_by_existing_environment():
orig_environ = os.environ orig_environ = os.environ
try: try:

View file

@ -220,6 +220,21 @@ def test_extract_archive_calls_borg_with_destination_path():
) )
def test_extract_archive_calls_borg_with_strip_components():
flexmock(module.os.path).should_receive('abspath').and_return('repo')
insert_execute_command_mock(('borg', 'extract', '--strip-components', '5', 'repo::archive'))
module.extract_archive(
dry_run=False,
repository='repo',
archive='archive',
paths=None,
location_config={},
storage_config={},
strip_components=5,
)
def test_extract_archive_calls_borg_with_progress_parameter(): def test_extract_archive_calls_borg_with_progress_parameter():
flexmock(module.os.path).should_receive('abspath').and_return('repo') flexmock(module.os.path).should_receive('abspath').and_return('repo')
flexmock(module).should_receive('execute_command').with_args( flexmock(module).should_receive('execute_command').with_args(