How to use the cbor2.dump function in cbor2

To help you get started, we’ve selected a few cbor2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mozilla / version-control-tools / pylib / vcsreplicator / vcsreplicator / consumer.py View on Github external
# and atomic rename.
    dest = os.path.join(local_path, '.hg', 'replicated-data')
    dest_tmp = '%s.tmp' % dest

    if any(len(h) != 40 for h in heads):
        raise ValueError('expected 40 byte hex heads in message')

    with open(dest_tmp, 'wb') as fh:
        data = {
            # May be None or an integer.
            b'last_push_id': last_push_id,
            # Heads are in hex. We store in binary for reading efficiency.
            b'heads': list(map(binascii.unhexlify, heads)),
        }

        cbor2.dump(data, fh, canonical=True)

    # If we're replacing a file, we need to ensure the mtime is advanced,
    # otherwise things caching the file may not see multiple updates in rapid
    # succession and may hold onto an earlier update. We can't just "touch"
    # the file because filesystem mtime resolution may not be that fine
    # grained. So we ensure the mtime is always incremented by at least 2s.
    try:
        st = os.stat(dest)
        old_mtime = st.st_mtime
    except OSError as e:
        if e.errno != errno.ENOENT:
            raise

        old_mtime = 0

    new_mtime = os.stat(dest_tmp).st_mtime