From 58c95d8015344fe442d7cad1036c73573a93dd52 Mon Sep 17 00:00:00 2001 From: Divyansh Singh Date: Mon, 20 Mar 2023 02:43:23 +0530 Subject: [PATCH] feat: file:// URLs support --- borgmatic/borg/export_tar.py | 6 +++++- borgmatic/borg/extract.py | 6 +++++- borgmatic/config/normalize.py | 28 ++++++++++++++++------------ borgmatic/config/validate.py | 11 +++++++---- 4 files changed, 33 insertions(+), 18 deletions(-) diff --git a/borgmatic/borg/export_tar.py b/borgmatic/borg/export_tar.py index 5ed494c8..28a876ef 100644 --- a/borgmatic/borg/export_tar.py +++ b/borgmatic/borg/export_tar.py @@ -47,7 +47,11 @@ def export_tar_archive( + (('--tar-filter', tar_filter) if tar_filter else ()) + (('--strip-components', str(strip_components)) if strip_components else ()) + flags.make_repository_archive_flags( - repository if ':' in repository else os.path.abspath(repository), + os.path.abspath(repository) + if ':' not in repository + else os.path.abspath(repository[7:]) + if repository.startswith('file://') + else repository, archive, local_borg_version, ) diff --git a/borgmatic/borg/extract.py b/borgmatic/borg/extract.py index 8ea8bbbd..be3c5b76 100644 --- a/borgmatic/borg/extract.py +++ b/borgmatic/borg/extract.py @@ -100,7 +100,11 @@ def extract_archive( + (('--progress',) if progress else ()) + (('--stdout',) if extract_to_stdout else ()) + flags.make_repository_archive_flags( - repository if ':' in repository else os.path.abspath(repository), + os.path.abspath(repository) + if ':' not in repository + else os.path.abspath(repository[7:]) + if repository.startswith('file://') + else repository, archive, local_borg_version, ) diff --git a/borgmatic/config/normalize.py b/borgmatic/config/normalize.py index 574a48a1..7a250148 100644 --- a/borgmatic/config/normalize.py +++ b/borgmatic/config/normalize.py @@ -1,4 +1,5 @@ import logging +import os def normalize(config_filename, config): @@ -68,20 +69,23 @@ def normalize(config_filename, config): ) ) ) - if ':' in repository and not repository.startswith('ssh://'): - rewritten_repository = ( - f"ssh://{repository.replace(':~', '/~').replace(':/', '/').replace(':', '/./')}" - ) - logs.append( - logging.makeLogRecord( - dict( - levelno=logging.WARNING, - levelname='WARNING', - msg=f'{config_filename}: Remote repository paths without ssh:// syntax are deprecated. Interpreting "{repository}" as "{rewritten_repository}"', + if ':' in repository: + if repository.startswith('file://'): + config['location']['repositories'].append(os.path.abspath(repository[7:])) + elif repository.startswith('ssh://'): + config['location']['repositories'].append(repository) + else: + rewritten_repository = f"ssh://{repository.replace(':~', '/~').replace(':/', '/').replace(':', '/./')}" + logs.append( + logging.makeLogRecord( + dict( + levelno=logging.WARNING, + levelname='WARNING', + msg=f'{config_filename}: Remote repository paths without ssh:// syntax are deprecated. Interpreting "{repository}" as "{rewritten_repository}"', + ) ) ) - ) - config['location']['repositories'].append(rewritten_repository) + config['location']['repositories'].append(rewritten_repository) else: config['location']['repositories'].append(repository) diff --git a/borgmatic/config/validate.py b/borgmatic/config/validate.py index 3a2807f9..d61f7f3a 100644 --- a/borgmatic/config/validate.py +++ b/borgmatic/config/validate.py @@ -126,12 +126,15 @@ def normalize_repository_path(repository): ''' Given a repository path, return the absolute path of it (for local repositories). ''' - # A colon in the repository indicates it's a remote repository. Bail. - if ':' in repository: + # A colon in the repository could mean that it's either a file:// URL or a remote repository. + # If it's a remote repository, we don't want to normalize it. If it's a file:// URL, we do. + if ':' not in repository: + return os.path.abspath(repository) + elif repository.startswith('file://'): + return os.path.abspath(repository[7:]) + else: return repository - return os.path.abspath(repository) - def repositories_match(first, second): '''