Ejemplo n.º 1
0
def test_thaw_errors():
    buf = freeze_basic_mapping({'hello': 'world'})

    for s in (buf[1:], buf[:-1], b'"foo"[2]', b'open("/dev/null", "r")',
              b'"foo".__class__'):
        with pytest.raises(ThawError):
            thaw_basic_mapping(s)
Ejemplo n.º 2
0
def test_thaw_errors():
    buf = freeze_basic_mapping({ 'hello': 'world' })

    for s in (buf[1:], buf[:-1],
              b'"foo"[2]', b'open("/dev/null", "r")',
              b'"foo".__class__'):
        with pytest.raises(ThawError):
            thaw_basic_mapping(s)
Ejemplo n.º 3
0
def test_simple():
    d = { 'hello': 42,
          'world': True,
          'data': b'fooxyz',
          'str': 'nice' }

    assert thaw_basic_mapping(freeze_basic_mapping(d)) == d
Ejemplo n.º 4
0
def test_simple():
    d = { 'hello': 42,
          'world': True,
          'data': b'fooxyz',
          'str': 'nice' }

    assert thaw_basic_mapping(freeze_basic_mapping(d)) == d
Ejemplo n.º 5
0
def main(args=None):

    if args is None:
        args = sys.argv[1:]

    options = parse_args(args)
    setup_logging(options)

    # Determine available backups
    backup_list = set(x for x in os.listdir('.')
                      if re.match(r'^\d{4}-\d\d-\d\d_\d\d:\d\d:\d\d$', x))

    if not os.path.exists(options.state) and len(backup_list) > 1:
        if not options.reconstruct_state:
            raise QuietError(
                'Found more than one backup but no state file! Aborting.')

        log.warning('Trying to reconstruct state file..')
        state = upgrade_to_state(backup_list)
        if not options.n:
            log.info('Saving reconstructed state..')
            with open(options.state, 'wb') as fh:
                fh.write(freeze_basic_mapping(state))
    elif not os.path.exists(options.state):
        log.warning('Creating state file..')
        state = dict()
    else:
        log.info('Reading state...')
        # Older versions used pickle to store state...
        with open(options.state, 'rb') as fh:
            proto = fh.read(2)
            fh.seek(0)
            if proto == b'\x80\x02':
                state = pickle.load(fh)
            else:
                state = thaw_basic_mapping(fh.read())

    to_delete = process_backups(backup_list, state, options.cycles)

    for x in to_delete:
        log.info('Backup %s is no longer needed, removing...', x)
        if not options.n:
            if options.use_s3qlrm:
                s3qlrm([x])
            else:
                shutil.rmtree(x)

    if options.n:
        log.info('Dry run, not saving state.')
    else:
        log.info('Saving state..')
        with open(options.state, 'wb') as fh:
            fh.write(freeze_basic_mapping(state))
Ejemplo n.º 6
0
def main(args=None):

    if args is None:
        args = sys.argv[1:]

    options = parse_args(args)
    setup_logging(options)

    # Determine available backups
    backup_list = set(x for x in os.listdir('.')
                      if re.match(r'^\d{4}-\d\d-\d\d_\d\d:\d\d:\d\d$', x))

    if not os.path.exists(options.state) and len(backup_list) > 1:
        if not options.reconstruct_state:
            raise QuietError('Found more than one backup but no state file! Aborting.')

        log.warning('Trying to reconstruct state file..')
        state = upgrade_to_state(backup_list)
        if not options.n:
            log.info('Saving reconstructed state..')
            with open(options.state, 'wb') as fh:
                fh.write(freeze_basic_mapping(state))
    elif not os.path.exists(options.state):
        log.warning('Creating state file..')
        state = dict()
    else:
        log.info('Reading state...')
        # Older versions used pickle to store state...
        with open(options.state, 'rb') as fh:
            proto = fh.read(2)
            fh.seek(0)
            if proto == b'\x80\x02':
                state = pickle.load(fh)
            else:
                state = thaw_basic_mapping(fh.read())

    to_delete = process_backups(backup_list, state, options.cycles)

    for x in to_delete:
        log.info('Backup %s is no longer needed, removing...', x)
        if not options.n:
            if options.use_s3qlrm:
                s3qlrm([x])
            else:
                shutil.rmtree(x)

    if options.n:
        log.info('Dry run, not saving state.')
    else:
        log.info('Saving state..')
        with open(options.state, 'wb') as fh:
            fh.write(freeze_basic_mapping(state))