예제 #1
0
def fake_chks():
    """ Return a random CHK. """
    size = CHK_SIZE - 5
    while True:
        #yield bytes_to_chk('\x00\x02\x02\xff\xff'
        #                   + ''.join(map(chr, map(random.randrange,
        #                                          [0] * size, [256] * size))))
        yield bytes_to_chk('\x00\x02\x02\xff\xff' + ''.join(
            [chr(random.randrange(0, 256)) for dummy in range(0, size)]))
예제 #2
0
def fake_chks():
    """ Return a random CHK. """
    size = CHK_SIZE - 5
    while True:
        #yield bytes_to_chk('\x00\x02\x02\xff\xff'
        #                   + ''.join(map(chr, map(random.randrange,
        #                                          [0] * size, [256] * size))))
        yield bytes_to_chk('\x00\x02\x02\xff\xff'
                           + ''.join([chr(random.randrange(0, 256)) for dummy
                                      in range(0, size)]))
예제 #3
0
def bytes_to_top_key_tuple(bytes):
    """ Parses the top key data from a byte block and
        returns a (top_key_tuple, header_string, salt_byte) tuple. """

    if len(bytes) < BASE_LEN:
        raise ValueError("Not enough data to parse static fields.")

    if not bytes.startswith(HDR_PREFIX):
        raise ValueError("Doesn't look like top key binary data.")

    # Hmmm... return the salt byte?
    hdr, salt, graph_chk_count, update_count = struct.unpack(BASE_FMT,
                                                             bytes[:BASE_LEN])
    #print "bytes_to_top_key_data -- salt: ", dummy
    bytes = bytes[BASE_LEN:]
    if hdr != HDR_BYTES:
        if hdr == HDR_V1:
            print
            print
            print "NOTE:"
            print "hg update -r f67283c92051"
            print "Will get you back to a version of the"
            print "infocalypse source that can read this format."
            print
            print
            raise ValueError("Format version mismatch. "
                             + "That repo is in an obsolete format!")
        if hdr[5] != MAJOR_VERSION:
            # DOH! should have done this in initial release.
            raise ValueError("Format version mismatch. "
                             + "Maybe you're running old code?")
        print "bytes_to_top_key_data -- minor version mismatch: ", hdr
    if len(bytes) == 0:
        print "bytes_to_top_key_data -- No updates?"

    graph_chks = []
    for dummy in range(0, graph_chk_count):
        graph_chks.append(bytes_to_chk(bytes[:CHK_SIZE]))
        bytes = bytes[CHK_SIZE:]

    # REDFLAG: Fix range errors for incomplete / bad data.
    updates = []
    for dummy in range(0, update_count):
        update, bytes = bytes_to_update_tuple(bytes)
        updates.append(update)

    return ((tuple(graph_chks), tuple(updates)), hdr, salt)
예제 #4
0
def bytes_to_top_key_tuple(bytes):
    """ Parses the top key data from a byte block and
        returns a (top_key_tuple, header_string, salt_byte) tuple. """

    if len(bytes) < BASE_LEN:
        raise ValueError("Not enough data to parse static fields.")

    if not bytes.startswith(HDR_PREFIX):
        raise ValueError("Doesn't look like top key binary data.")

    # Hmmm... return the salt byte?
    hdr, salt, graph_chk_count, update_count = struct.unpack(
        BASE_FMT, bytes[:BASE_LEN])
    #print "bytes_to_top_key_data -- salt: ", dummy
    bytes = bytes[BASE_LEN:]
    if hdr != HDR_BYTES:
        if hdr == HDR_V1:
            print
            print
            print "NOTE:"
            print "hg update -r f67283c92051"
            print "Will get you back to a version of the"
            print "infocalypse source that can read this format."
            print
            print
            raise ValueError("Format version mismatch. " +
                             "That repo is in an obsolete format!")
        if hdr[5] != MAJOR_VERSION:
            # DOH! should have done this in initial release.
            raise ValueError("Format version mismatch. " +
                             "Maybe you're running old code?")
        print "bytes_to_top_key_data -- minor version mismatch: ", hdr
    if len(bytes) == 0:
        print "bytes_to_top_key_data -- No updates?"

    graph_chks = []
    for dummy in range(0, graph_chk_count):
        graph_chks.append(bytes_to_chk(bytes[:CHK_SIZE]))
        bytes = bytes[CHK_SIZE:]

    # REDFLAG: Fix range errors for incomplete / bad data.
    updates = []
    for dummy in range(0, update_count):
        update, bytes = bytes_to_update_tuple(bytes)
        updates.append(update)

    return ((tuple(graph_chks), tuple(updates)), hdr, salt)
예제 #5
0
def bytes_to_top_key_tuple(bytes):
    """ Writes the binary rep of an archive top key.

        Where top_key_tuple is:
        ( ((length, (chk,), max_age), .. ), ((root_sha, kind), ..), age )
    """

    if len(bytes) < BASE_LEN:
        raise ValueError("Not enough data to parse static fields.")

    if not bytes.startswith(HDR_PREFIX):
        raise ValueError("Doesn't look like %s top key binary data." %
                         HDR_PREFIX)


    version, salt, age, num_blocks, num_obs = struct.unpack(BASE_FMT,
                                                            bytes[:BASE_LEN])

    check_version(version)

    bytes = bytes[BASE_LEN:]

    blocks = []
    for dummy0 in range(0, num_blocks):
        length, age, chk_count = struct.unpack(BLOCK_BASE_FMT,
                                               bytes[:BLOCK_BASE_LEN])
        bytes = bytes[BLOCK_BASE_LEN:]

        chks = []
        for dummy1 in range(0, chk_count):
            chks.append(bytes_to_chk(bytes[:CHK_SIZE]))
            bytes = bytes[CHK_SIZE:]

        blocks.append((length, tuple(chks), age))


    root_objs = []
    for dummy2 in range(0, num_obs):
        root_objs.append(struct.unpack(ROOT_OBJ_FMT,
                                       bytes[:ROOT_OBJ_LEN]))
        bytes = bytes[ROOT_OBJ_LEN:]

    return ((tuple(blocks), tuple(root_objs), age), salt)
예제 #6
0
def bytes_to_update_tuple(bytes):
    """ INTERNAL: Read a single update from raw bytes. """
    length, flags, parent_count, head_count, chk_count = struct.unpack(
        BASE_UPDATE_FMT, bytes[:BASE_UPDATE_LEN])

    bytes = bytes[BASE_UPDATE_LEN:]

    parents = versions_from_bytes(bytes[:HGVER_SIZE * parent_count])
    bytes = bytes[HGVER_SIZE * parent_count:]

    heads = versions_from_bytes(bytes[:HGVER_SIZE * head_count])
    bytes = bytes[HGVER_SIZE * head_count:]

    chks = []
    for dummy in range(0, chk_count):
        chks.append(bytes_to_chk(bytes[:CHK_SIZE]))
        bytes = bytes[CHK_SIZE:]

    return ((length, parents, heads, tuple(chks), bool(flags & HAS_PARENTS),
             bool(flags & HAS_HEADS)), bytes)
예제 #7
0
def bytes_to_top_key_tuple(bytes):
    """ Writes the binary rep of an archive top key.

        Where top_key_tuple is:
        ( ((length, (chk,), max_age), .. ), ((root_sha, kind), ..), age )
    """

    if len(bytes) < BASE_LEN:
        raise ValueError("Not enough data to parse static fields.")

    if not bytes.startswith(HDR_PREFIX):
        raise ValueError("Doesn't look like %s top key binary data." %
                         HDR_PREFIX)

    version, salt, age, num_blocks, num_obs = struct.unpack(
        BASE_FMT, bytes[:BASE_LEN])

    check_version(version)

    bytes = bytes[BASE_LEN:]

    blocks = []
    for dummy0 in range(0, num_blocks):
        length, age, chk_count = struct.unpack(BLOCK_BASE_FMT,
                                               bytes[:BLOCK_BASE_LEN])
        bytes = bytes[BLOCK_BASE_LEN:]

        chks = []
        for dummy1 in range(0, chk_count):
            chks.append(bytes_to_chk(bytes[:CHK_SIZE]))
            bytes = bytes[CHK_SIZE:]

        blocks.append((length, tuple(chks), age))

    root_objs = []
    for dummy2 in range(0, num_obs):
        root_objs.append(struct.unpack(ROOT_OBJ_FMT, bytes[:ROOT_OBJ_LEN]))
        bytes = bytes[ROOT_OBJ_LEN:]

    return ((tuple(blocks), tuple(root_objs), age), salt)
예제 #8
0
def bytes_to_update_tuple(bytes):
    """ INTERNAL: Read a single update from raw bytes. """
    length, flags, parent_count, head_count, chk_count = struct.unpack(
        BASE_UPDATE_FMT,
        bytes[:BASE_UPDATE_LEN])

    bytes = bytes[BASE_UPDATE_LEN:]

    parents = versions_from_bytes(bytes[:HGVER_SIZE * parent_count])
    bytes = bytes[HGVER_SIZE * parent_count:]

    heads = versions_from_bytes(bytes[:HGVER_SIZE * head_count])
    bytes = bytes[HGVER_SIZE * head_count:]

    chks = []
    for dummy in range(0, chk_count):
        chks.append(bytes_to_chk(bytes[:CHK_SIZE]))
        bytes = bytes[CHK_SIZE:]

    return ((length, parents, heads, tuple(chks),
             bool(flags & HAS_PARENTS), bool(flags & HAS_HEADS)),
            bytes)