Exemplo n.º 1
0
def list_archives(repository,
                  storage_config,
                  list_arguments,
                  local_path='borg',
                  remote_path=None):
    '''
    Given a local or remote repository path, a storage config dict, and the arguments to the list
    action, display the output of listing Borg archives in the repository or return JSON output. Or,
    if an archive name is given, listing the files in that archive.
    '''
    lock_wait = storage_config.get('lock_wait', None)
    if list_arguments.successful:
        list_arguments.glob_archives = BORG_EXCLUDE_CHECKPOINTS_GLOB

    full_command = (
        (local_path, 'list') +
        (('--info', ) if logger.getEffectiveLevel() == logging.INFO
         and not list_arguments.json else
         ()) + (('--debug', '--show-rc') if logger.isEnabledFor(logging.DEBUG)
                and not list_arguments.json else
                ()) + make_flags('remote-path', remote_path) +
        make_flags('lock-wait', lock_wait) + make_flags_from_arguments(
            list_arguments,
            excludes=('repository', 'archive', 'paths', 'successful')) +
        ('::'.join((repository, list_arguments.archive))
         if list_arguments.archive else repository, ) +
        (tuple(list_arguments.paths) if list_arguments.paths else ()))

    return execute_command(
        full_command,
        output_log_level=None if list_arguments.json else logging.WARNING,
        error_on_warnings=False,
    )
Exemplo n.º 2
0
def display_archives_info(repository,
                          storage_config,
                          info_arguments,
                          local_path='borg',
                          remote_path=None):
    '''
    Given a local or remote repository path, a storage config dict, and the arguments to the info
    action, display summary information for Borg archives in the repository or return JSON summary
    information.
    '''
    lock_wait = storage_config.get('lock_wait', None)

    full_command = ((local_path, 'info') +
                    (('--info', ) if logger.getEffectiveLevel() == logging.INFO
                     and not info_arguments.json else
                     ()) + (('--debug', '--show-rc') if logger.isEnabledFor(
                         logging.DEBUG) and not info_arguments.json else
                            ()) + make_flags('remote-path', remote_path) +
                    make_flags('lock-wait', lock_wait) +
                    make_flags_from_arguments(
                        info_arguments, excludes=('repository', 'archive')) +
                    ('::'.join((repository, info_arguments.archive))
                     if info_arguments.archive else repository, ))

    return execute_command(
        full_command,
        output_log_level=None if info_arguments.json else logging.WARNING)
Exemplo n.º 3
0
def resolve_archive_name(repository,
                         archive,
                         storage_config,
                         local_path='borg',
                         remote_path=None):
    '''
    Given a local or remote repository path, an archive name, a storage config dict, a local Borg
    path, and a remote Borg path, simply return the archive name. But if the archive name is
    "latest", then instead introspect the repository for the latest successful (non-checkpoint)
    archive, and return its name.

    Raise ValueError if "latest" is given but there are no archives in the repository.
    '''
    if archive != "latest":
        return archive

    lock_wait = storage_config.get('lock_wait', None)

    full_command = (
        (local_path, 'list') +
        (('--info', ) if logger.getEffectiveLevel() == logging.INFO else ()) +
        (('--debug', '--show-rc') if logger.isEnabledFor(logging.DEBUG) else
         ()) + make_flags('remote-path', remote_path) +
        make_flags('lock-wait', lock_wait) +
        make_flags('glob-archives', BORG_EXCLUDE_CHECKPOINTS_GLOB) +
        make_flags('last', 1) + ('--short', repository))

    output = execute_command(full_command,
                             output_log_level=None,
                             error_on_warnings=False)
    try:
        latest_archive = output.strip().splitlines()[-1]
    except IndexError:
        raise ValueError('No archives found in the repository')

    logger.debug('{}: Latest archive is {}'.format(repository, latest_archive))

    return latest_archive
Exemplo n.º 4
0
def test_make_flags_formats_integer_value():
    assert module.make_flags('foo', 3) == ('--foo', '3')
Exemplo n.º 5
0
def test_make_flags_formats_string_value():
    assert module.make_flags('foo', 'bar') == ('--foo', 'bar')
Exemplo n.º 6
0
def test_make_flags_formats_name_with_underscore():
    assert module.make_flags('posix_me_harder', 'okay') == ('--posix-me-harder', 'okay')
Exemplo n.º 7
0
def test_make_flags_omits_false_value():
    assert module.make_flags('foo', False) == ()
Exemplo n.º 8
0
def test_make_flags_formats_true_value():
    assert module.make_flags('foo', True) == ('--foo',)