Code formatting (#635).

This commit is contained in:
Dan Helfman
2025-01-25 14:14:48 -08:00
parent cc7e01be68
commit 8c5db19490
30 changed files with 75 additions and 142 deletions
+1 -3
View File
@@ -22,9 +22,7 @@ def run_borg(
if borg_arguments.repository is None or borgmatic.config.validate.repositories_match(
repository, borg_arguments.repository
):
logger.info(
'Running arbitrary Borg command'
)
logger.info('Running arbitrary Borg command')
archive_name = borgmatic.borg.repo_list.resolve_archive_name(
repository['path'],
borg_arguments.archive,
+1 -3
View File
@@ -21,9 +21,7 @@ def run_break_lock(
if break_lock_arguments.repository is None or borgmatic.config.validate.repositories_match(
repository, break_lock_arguments.repository
):
logger.info(
'Breaking repository and cache locks'
)
logger.info('Breaking repository and cache locks')
borgmatic.borg.break_lock.break_lock(
repository['path'],
config,
+1 -3
View File
@@ -24,9 +24,7 @@ def run_change_passphrase(
repository, change_passphrase_arguments.repository
)
):
logger.info(
'Changing repository passphrase'
)
logger.info('Changing repository passphrase')
borgmatic.borg.change_passphrase.change_passphrase(
repository['path'],
config,
+1 -3
View File
@@ -762,9 +762,7 @@ def run_check(
write_check_time(make_check_time_path(config, repository_id, 'extract'))
if 'spot' in checks:
with borgmatic.config.paths.Runtime_directory(
config
) as borgmatic_runtime_directory:
with borgmatic.config.paths.Runtime_directory(config) as borgmatic_runtime_directory:
spot_check(
repository,
config,
+2 -6
View File
@@ -37,9 +37,7 @@ def run_compact(
**hook_context,
)
if borgmatic.borg.feature.available(borgmatic.borg.feature.Feature.COMPACT, local_borg_version):
logger.info(
f'Compacting segments{dry_run_label}'
)
logger.info(f'Compacting segments{dry_run_label}')
borgmatic.borg.compact.compact_segments(
global_arguments.dry_run,
repository['path'],
@@ -53,9 +51,7 @@ def run_compact(
threshold=compact_arguments.threshold,
)
else: # pragma: nocover
logger.info(
'Skipping compact (only available/needed in Borg 1.2+)'
)
logger.info('Skipping compact (only available/needed in Borg 1.2+)')
borgmatic.hooks.command.execute_hook(
config.get('after_compact'),
+1 -3
View File
@@ -286,9 +286,7 @@ def run_create(
logger.info(f'Creating archive{dry_run_label}')
working_directory = borgmatic.config.paths.get_working_directory(config)
with borgmatic.config.paths.Runtime_directory(
config
) as borgmatic_runtime_directory:
with borgmatic.config.paths.Runtime_directory(config) as borgmatic_runtime_directory:
borgmatic.hooks.dispatch.call_hooks_even_if_unconfigured(
'remove_data_source_dumps',
config,
+1 -3
View File
@@ -22,9 +22,7 @@ def run_export_tar(
if export_tar_arguments.repository is None or borgmatic.config.validate.repositories_match(
repository, export_tar_arguments.repository
):
logger.info(
f'Exporting archive {export_tar_arguments.archive} as tar file'
)
logger.info(f'Exporting archive {export_tar_arguments.archive} as tar file')
borgmatic.borg.export_tar.export_tar_archive(
global_arguments.dry_run,
repository['path'],
+1 -3
View File
@@ -33,9 +33,7 @@ def run_extract(
if extract_arguments.repository is None or borgmatic.config.validate.repositories_match(
repository, extract_arguments.repository
):
logger.info(
f'Extracting archive {extract_arguments.archive}'
)
logger.info(f'Extracting archive {extract_arguments.archive}')
borgmatic.borg.extract.extract_archive(
global_arguments.dry_run,
repository['path'],
+1 -3
View File
@@ -27,9 +27,7 @@ def run_info(
repository, info_arguments.repository
):
if not info_arguments.json:
logger.answer(
'Displaying archive summary information'
)
logger.answer('Displaying archive summary information')
archive_name = borgmatic.borg.repo_list.resolve_archive_name(
repository['path'],
info_arguments.archive,
+1 -3
View File
@@ -23,9 +23,7 @@ def run_mount(
repository, mount_arguments.repository
):
if mount_arguments.archive:
logger.info(
f'Mounting archive {mount_arguments.archive}'
)
logger.info(f'Mounting archive {mount_arguments.archive}')
else: # pragma: nocover
logger.info('Mounting repository')
+1 -2
View File
@@ -21,8 +21,7 @@ def run_repo_delete(
repository, repo_delete_arguments.repository
):
logger.answer(
'Deleting repository'
+ (' cache' if repo_delete_arguments.cache_only else '')
'Deleting repository' + (' cache' if repo_delete_arguments.cache_only else '')
)
borgmatic.borg.repo_delete.delete_repository(
+1 -3
View File
@@ -25,9 +25,7 @@ def run_repo_info(
repository, repo_info_arguments.repository
):
if not repo_info_arguments.json:
logger.answer(
'Displaying repository summary information'
)
logger.answer('Displaying repository summary information')
json_output = borgmatic.borg.repo_info.display_repository_info(
repository['path'],
+2 -6
View File
@@ -172,9 +172,7 @@ def restore_single_dump(
Dump(hook_name, data_source['name'], data_source.get('hostname'), data_source.get('port'))
)
logger.info(
f'Restoring data source {dump_metadata}'
)
logger.info(f'Restoring data source {dump_metadata}')
dump_patterns = borgmatic.hooks.dispatch.call_hooks(
'make_data_source_dump_patterns',
@@ -443,9 +441,7 @@ def run_restore(
logger.info(f'Restoring data sources from archive {restore_arguments.archive}')
with borgmatic.config.paths.Runtime_directory(
config
) as borgmatic_runtime_directory:
with borgmatic.config.paths.Runtime_directory(config) as borgmatic_runtime_directory:
borgmatic.hooks.dispatch.call_hooks_even_if_unconfigured(
'remove_data_source_dumps',
config,
+1 -3
View File
@@ -17,9 +17,7 @@ def run_transfer(
'''
Run the "transfer" action for the given repository.
'''
logger.info(
'Transferring archives to repository'
)
logger.info('Transferring archives to repository')
borgmatic.borg.transfer.transfer_archives(
global_arguments.dry_run,
repository['path'],
+16 -14
View File
@@ -39,7 +39,13 @@ from borgmatic.commands.arguments import parse_arguments
from borgmatic.config import checks, collect, validate
from borgmatic.hooks import command, dispatch
from borgmatic.hooks.monitoring import monitor
from borgmatic.logger import DISABLED, add_custom_log_levels, configure_logging, should_do_markup, set_log_prefix
from borgmatic.logger import (
DISABLED,
add_custom_log_levels,
configure_logging,
set_log_prefix,
should_do_markup,
)
from borgmatic.signals import configure_signals
from borgmatic.verbosity import verbosity_to_log_level
@@ -131,15 +137,11 @@ def run_configuration(config_filename, config, config_paths, arguments):
try:
while not repo_queue.empty():
repository, retry_num = repo_queue.get()
set_log_prefix(repository.get('label', repository['path']))
logger.debug(
'Running actions for repository'
)
set_log_prefix(repository.get('label', repository['path']))
logger.debug('Running actions for repository')
timeout = retry_num * retry_wait
if timeout:
logger.warning(
f'Sleeping {timeout}s before next retry'
)
logger.warning(f'Sleeping {timeout}s before next retry')
time.sleep(timeout)
try:
yield from run_actions(
@@ -165,9 +167,7 @@ def run_configuration(config_filename, config, config_paths, arguments):
log_command_error_output=True,
)
)
logger.warning(
f'Retrying... attempt {retry_num + 1}/{retries}'
)
logger.warning(f'Retrying... attempt {retry_num + 1}/{retries}')
continue
if command.considered_soft_failure(error):
@@ -819,9 +819,11 @@ def collect_configuration_run_summary_logs(configs, config_paths, arguments):
try:
for config_filename, config in configs.items():
set_log_prefix(config_filename)
set_log_prefix(config_filename)
results = list(run_configuration(config_filename, config, config_paths, arguments))
error_logs = tuple(result for result in results if isinstance(result, logging.LogRecord))
error_logs = tuple(
result for result in results if isinstance(result, logging.LogRecord)
)
if error_logs:
yield from log_error_records('An error occurred')
@@ -837,7 +839,7 @@ def collect_configuration_run_summary_logs(configs, config_paths, arguments):
if results:
json_results.extend(results)
finally:
set_log_prefix(None)
set_log_prefix(None)
if 'umount' in arguments:
logger.info(f"Unmounting mount point {arguments['umount'].mount_point}")
+1 -3
View File
@@ -60,9 +60,7 @@ def execute_hook(commands, umask, config_filename, description, dry_run, **conte
dry_run_label = ' (dry run; not actually running hooks)' if dry_run else ''
context['configuration_filename'] = config_filename
commands = [
interpolate_context(description, command, context) for command in commands
]
commands = [interpolate_context(description, command, context) for command in commands]
if len(commands) == 1:
logger.info(f'Running command for {description} hook{dry_run_label}')
+1 -3
View File
@@ -83,9 +83,7 @@ def remove_data_source_dumps(hook_config, config, borgmatic_runtime_directory, d
for manifest_directory in glob.glob(manifest_glob):
manifest_file_path = os.path.join(manifest_directory, 'manifest.json')
logger.debug(
f'Removing bootstrap manifest at {manifest_file_path}{dry_run_label}'
)
logger.debug(f'Removing bootstrap manifest at {manifest_file_path}{dry_run_label}')
if dry_run:
continue
+2 -6
View File
@@ -338,9 +338,7 @@ def remove_data_source_dumps(hook_config, config, borgmatic_runtime_directory, d
),
'lvm_snapshots',
)
logger.debug(
f'Looking for snapshots to remove in {snapshots_glob}{dry_run_label}'
)
logger.debug(f'Looking for snapshots to remove in {snapshots_glob}{dry_run_label}')
umount_command = hook_config.get('umount_command', 'umount')
for snapshots_directory in glob.glob(snapshots_glob):
@@ -364,9 +362,7 @@ def remove_data_source_dumps(hook_config, config, borgmatic_runtime_directory, d
if not os.path.isdir(snapshot_mount_path):
continue
logger.debug(
f'Unmounting LVM snapshot at {snapshot_mount_path}{dry_run_label}'
)
logger.debug(f'Unmounting LVM snapshot at {snapshot_mount_path}{dry_run_label}')
if dry_run:
continue
+3 -9
View File
@@ -102,9 +102,7 @@ def execute_dump_command(
+ ('--result-file', dump_filename)
)
logger.debug(
f'Dumping MariaDB database "{database_name}" to {dump_filename}{dry_run_label}'
)
logger.debug(f'Dumping MariaDB database "{database_name}" to {dump_filename}{dry_run_label}')
if dry_run:
return None
@@ -155,9 +153,7 @@ def dump_data_sources(
for database in databases:
dump_path = make_dump_path(borgmatic_runtime_directory)
extra_environment = {'MYSQL_PWD': database['password']} if 'password' in database else None
dump_database_names = database_names_to_dump(
database, extra_environment, dry_run
)
dump_database_names = database_names_to_dump(database, extra_environment, dry_run)
if not dump_database_names:
if dry_run:
@@ -209,9 +205,7 @@ def remove_data_source_dumps(
borgmatic_runtime_directory to construct the destination path. If this is a dry run, then don't
actually remove anything.
'''
dump.remove_data_source_dumps(
make_dump_path(borgmatic_runtime_directory), 'MariaDB', dry_run
)
dump.remove_data_source_dumps(make_dump_path(borgmatic_runtime_directory), 'MariaDB', dry_run)
def make_data_source_dump_patterns(
+1 -3
View File
@@ -123,9 +123,7 @@ def remove_data_source_dumps(
borgmatic_runtime_directory to construct the destination path. If this is a dry run, then don't
actually remove anything.
'''
dump.remove_data_source_dumps(
make_dump_path(borgmatic_runtime_directory), 'MongoDB', dry_run
)
dump.remove_data_source_dumps(make_dump_path(borgmatic_runtime_directory), 'MongoDB', dry_run)
def make_data_source_dump_patterns(
+3 -9
View File
@@ -101,9 +101,7 @@ def execute_dump_command(
+ ('--result-file', dump_filename)
)
logger.debug(
f'Dumping MySQL database "{database_name}" to {dump_filename}{dry_run_label}'
)
logger.debug(f'Dumping MySQL database "{database_name}" to {dump_filename}{dry_run_label}')
if dry_run:
return None
@@ -154,9 +152,7 @@ def dump_data_sources(
for database in databases:
dump_path = make_dump_path(borgmatic_runtime_directory)
extra_environment = {'MYSQL_PWD': database['password']} if 'password' in database else None
dump_database_names = database_names_to_dump(
database, extra_environment, dry_run
)
dump_database_names = database_names_to_dump(database, extra_environment, dry_run)
if not dump_database_names:
if dry_run:
@@ -208,9 +204,7 @@ def remove_data_source_dumps(
borgmatic runtime directory to construct the destination path. If this is a dry run, then don't
actually remove anything.
'''
dump.remove_data_source_dumps(
make_dump_path(borgmatic_runtime_directory), 'MySQL', dry_run
)
dump.remove_data_source_dumps(make_dump_path(borgmatic_runtime_directory), 'MySQL', dry_run)
def make_data_source_dump_patterns(
+2 -6
View File
@@ -137,9 +137,7 @@ def dump_data_sources(
for database in databases:
extra_environment = make_extra_environment(database)
dump_path = make_dump_path(borgmatic_runtime_directory)
dump_database_names = database_names_to_dump(
database, extra_environment, dry_run
)
dump_database_names = database_names_to_dump(database, extra_environment, dry_run)
if not dump_database_names:
if dry_run:
@@ -350,9 +348,7 @@ def restore_data_source_dump(
data_source, restore_connection_params=connection_params
)
logger.debug(
f"Restoring PostgreSQL database {data_source['name']}{dry_run_label}"
)
logger.debug(f"Restoring PostgreSQL database {data_source['name']}{dry_run_label}")
if dry_run:
return
+1 -3
View File
@@ -105,9 +105,7 @@ def remove_data_source_dumps(
borgmatic runtime directory to construct the destination path. If this is a dry run, then don't
actually remove anything.
'''
dump.remove_data_source_dumps(
make_dump_path(borgmatic_runtime_directory), 'SQLite', dry_run
)
dump.remove_data_source_dumps(make_dump_path(borgmatic_runtime_directory), 'SQLite', dry_run)
def make_data_source_dump_patterns(
+2 -6
View File
@@ -335,9 +335,7 @@ def remove_data_source_dumps(hook_config, config, borgmatic_runtime_directory, d
),
'zfs_snapshots',
)
logger.debug(
f'Looking for snapshots to remove in {snapshots_glob}{dry_run_label}'
)
logger.debug(f'Looking for snapshots to remove in {snapshots_glob}{dry_run_label}')
umount_command = hook_config.get('umount_command', 'umount')
for snapshots_directory in glob.glob(snapshots_glob):
@@ -362,9 +360,7 @@ def remove_data_source_dumps(hook_config, config, borgmatic_runtime_directory, d
if not os.path.isdir(snapshot_mount_path):
continue
logger.debug(
f'Unmounting ZFS snapshot at {snapshot_mount_path}{dry_run_label}'
)
logger.debug(f'Unmounting ZFS snapshot at {snapshot_mount_path}{dry_run_label}')
if not dry_run:
try:
+12 -4
View File
@@ -89,7 +89,9 @@ class Multi_stream_handler(logging.Handler):
class Console_no_color_formatter(logging.Formatter):
def __init__(self, *args, **kwargs):
super(Console_no_color_formatter, self).__init__('{prefix}{message}', style='{', defaults={'prefix': ''}, *args, **kwargs)
super(Console_no_color_formatter, self).__init__(
'{prefix}{message}', style='{', defaults={'prefix': ''}, *args, **kwargs
)
class Color(enum.Enum):
@@ -103,7 +105,9 @@ class Color(enum.Enum):
class Console_color_formatter(logging.Formatter):
def __init__(self, *args, **kwargs):
super(Console_color_formatter, self).__init__('{prefix}{message}', style='{', defaults={'prefix': ''}, *args, **kwargs)
super(Console_color_formatter, self).__init__(
'{prefix}{message}', style='{', defaults={'prefix': ''}, *args, **kwargs
)
def format(self, record):
add_custom_log_levels()
@@ -255,7 +259,9 @@ def configure_logging(
if syslog_path:
syslog_handler = logging.handlers.SysLogHandler(address=syslog_path)
syslog_handler.setFormatter(
logging.Formatter('borgmatic: {levelname} {prefix}{message}', style='{', defaults={'prefix': ''}) # noqa: FS003
logging.Formatter(
'borgmatic: {levelname} {prefix}{message}', style='{', defaults={'prefix': ''}
) # noqa: FS003
)
syslog_handler.setLevel(syslog_log_level)
handlers.append(syslog_handler)
@@ -264,7 +270,9 @@ def configure_logging(
file_handler = logging.handlers.WatchedFileHandler(log_file)
file_handler.setFormatter(
logging.Formatter(
log_file_format or '[{asctime}] {levelname}: {prefix}{message}', style='{', defaults={'prefix': ''} # noqa: FS003
log_file_format or '[{asctime}] {levelname}: {prefix}{message}',
style='{',
defaults={'prefix': ''}, # noqa: FS003
)
)
file_handler.setLevel(log_file_log_level)
+1 -3
View File
@@ -21,9 +21,7 @@ def test_write_patterns_file_writes_pattern_lines():
def test_write_patterns_file_with_empty_exclude_patterns_does_not_raise():
module.write_patterns_file(
[], borgmatic_runtime_directory='/run/user/0'
)
module.write_patterns_file([], borgmatic_runtime_directory='/run/user/0')
def test_write_patterns_file_appends_to_existing():
+5 -10
View File
@@ -9,9 +9,7 @@ from borgmatic.hooks.data_source import mariadb as module
def test_database_names_to_dump_passes_through_name():
extra_environment = flexmock()
names = module.database_names_to_dump(
{'name': 'foo'}, extra_environment, dry_run=False
)
names = module.database_names_to_dump({'name': 'foo'}, extra_environment, dry_run=False)
assert names == ('foo',)
@@ -20,9 +18,7 @@ def test_database_names_to_dump_bails_for_dry_run():
extra_environment = flexmock()
flexmock(module).should_receive('execute_command_and_capture_output').never()
names = module.database_names_to_dump(
{'name': 'all'}, extra_environment, dry_run=True
)
names = module.database_names_to_dump({'name': 'all'}, extra_environment, dry_run=True)
assert names == ()
@@ -34,16 +30,15 @@ def test_database_names_to_dump_queries_mariadb_for_database_names():
extra_environment=extra_environment,
).and_return('foo\nbar\nmysql\n').once()
names = module.database_names_to_dump(
{'name': 'all'}, extra_environment, dry_run=False
)
names = module.database_names_to_dump({'name': 'all'}, extra_environment, dry_run=False)
assert names == ('foo', 'bar')
def test_use_streaming_true_for_any_databases():
assert module.use_streaming(
databases=[flexmock(), flexmock()], config=flexmock(),
databases=[flexmock(), flexmock()],
config=flexmock(),
)
+5 -10
View File
@@ -9,9 +9,7 @@ from borgmatic.hooks.data_source import mysql as module
def test_database_names_to_dump_passes_through_name():
extra_environment = flexmock()
names = module.database_names_to_dump(
{'name': 'foo'}, extra_environment, dry_run=False
)
names = module.database_names_to_dump({'name': 'foo'}, extra_environment, dry_run=False)
assert names == ('foo',)
@@ -20,9 +18,7 @@ def test_database_names_to_dump_bails_for_dry_run():
extra_environment = flexmock()
flexmock(module).should_receive('execute_command_and_capture_output').never()
names = module.database_names_to_dump(
{'name': 'all'}, extra_environment, dry_run=True
)
names = module.database_names_to_dump({'name': 'all'}, extra_environment, dry_run=True)
assert names == ()
@@ -34,16 +30,15 @@ def test_database_names_to_dump_queries_mysql_for_database_names():
extra_environment=extra_environment,
).and_return('foo\nbar\nmysql\n').once()
names = module.database_names_to_dump(
{'name': 'all'}, extra_environment, dry_run=False
)
names = module.database_names_to_dump({'name': 'all'}, extra_environment, dry_run=False)
assert names == ('foo', 'bar')
def test_use_streaming_true_for_any_databases():
assert module.use_streaming(
databases=[flexmock(), flexmock()], config=flexmock(),
databases=[flexmock(), flexmock()],
config=flexmock(),
)
+2 -1
View File
@@ -7,7 +7,8 @@ from borgmatic.hooks.data_source import sqlite as module
def test_use_streaming_true_for_any_databases():
assert module.use_streaming(
databases=[flexmock(), flexmock()], config=flexmock(),
databases=[flexmock(), flexmock()],
config=flexmock(),
)
+2 -5
View File
@@ -20,9 +20,7 @@ def test_interpolate_context_interpolates_variables():
command = 'ls {foo}{baz} {baz}' # noqa: FS003
context = {'foo': 'bar', 'baz': 'quux'}
assert (
module.interpolate_context('pre-backup', command, context) == 'ls barquux quux'
)
assert module.interpolate_context('pre-backup', command, context) == 'ls barquux quux'
def test_interpolate_context_escapes_interpolated_variables():
@@ -30,8 +28,7 @@ def test_interpolate_context_escapes_interpolated_variables():
context = {'foo': 'bar', 'inject': 'hi; naughty-command'}
assert (
module.interpolate_context('pre-backup', command, context)
== "ls bar 'hi; naughty-command'"
module.interpolate_context('pre-backup', command, context) == "ls bar 'hi; naughty-command'"
)