aboutsummaryrefslogtreecommitdiffstats
path: root/tools/python/xen/xend/uuid.py
blob: 096fef7f9f5b3aa70e3f978dd878754daad1b2d9 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""Universal(ly) Unique Identifiers (UUIDs).
"""
import commands
import random

def uuidgen(random=True):
    """Generate a UUID using the command uuidgen.

    If random is true (default) generates a random uuid.
    If random is false generates a time-based uuid.
    """
    cmd = "uuidgen"
    if random:
        cmd += " -r"
    else:
        cmd += " -t"
    return commands.getoutput(cmd)

class UuidFactoryUuidgen:

    """A uuid factory using uuidgen."""

    def __init__(self):
        pass

    def getUuid(self):
        return uuidgen()

class UuidFactoryRandom:

    """A random uuid factory."""

    def __init__(self):
        f = file("/dev/urandom", "r")
        seed = f.read(16)
        f.close()
        self.rand = random.Random(seed)

    def randBytes(self, n):
        return [ self.rand.randint(0, 255) for i in range(0, n) ]

    def getUuid(self):
        bytes = self.randBytes(16)
        # Encode the variant.
        bytes[6] = (bytes[6] & 0x0f) | 0x40
        bytes[8] = (bytes[8] & 0x3f) | 0x80
        f = "%02x"
        return ( "-".join([f*4, f*2, f*2, f*2, f*6]) % tuple(bytes) )

def getFactory():
    """Get the factory to use for creating uuids.
    This is so it's easy to change the uuid factory.
    For example, for testing we might want repeatable uuids
    rather than the random ones we normally use.
    """
    global uuidFactory
    try:
        uuidFactory
    except:
        #uuidFactory = UuidFactoryUuidgen()
        uuidFactory = UuidFactoryRandom()
    return uuidFactory

def getUuid():
    return getFactory().getUuid()