summaryrefslogtreecommitdiff
path: root/tools/python/xen/migration/verify.py
blob: b847c4bd220fee77123d692abea026a570c26dce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# -*- coding: utf-8 -*-

"""
Common verification infrastructure for v2 streams
"""

from struct import calcsize, unpack

class StreamError(Exception):
    """Error with the stream"""
    pass

class RecordError(Exception):
    """Error with a record in the stream"""
    pass


class VerifyBase(object):

    def __init__(self, info, read):

        self.info = info
        self.read = read

    def rdexact(self, nr_bytes):
        """Read exactly nr_bytes from the stream"""
        _ = self.read(nr_bytes)
        if len(_) != nr_bytes:
            raise IOError("Stream truncated")
        return _

    def unpack_exact(self, fmt):
        """Unpack a struct format string from the stream"""
        sz = calcsize(fmt)
        return unpack(fmt, self.rdexact(sz))