source: trunk/packages/xen-common/xen-common/tools/python/xen/xend/uuid.py @ 34

Last change on this file since 34 was 34, checked in by hartmans, 17 years ago

Add xen and xen-common

File size: 2.4 KB
Line 
1#============================================================================
2# This library is free software; you can redistribute it and/or
3# modify it under the terms of version 2.1 of the GNU Lesser General Public
4# License as published by the Free Software Foundation.
5#
6# This library is distributed in the hope that it will be useful,
7# but WITHOUT ANY WARRANTY; without even the implied warranty of
8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9# Lesser General Public License for more details.
10#
11# You should have received a copy of the GNU Lesser General Public
12# License along with this library; if not, write to the Free Software
13# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
14#============================================================================
15# Copyright (C) 2005 Mike Wray <mike.wray@hp.com>
16# Copyright (C) 2005 XenSource Ltd
17#============================================================================
18
19
20"""Universal Unique Identifiers (UUIDs).  By default, UUIDs generated here are
21purely random, with no internal structure.  However, they are the same size,
22and are formatted by the same conventions, as the UUIDs in the Open Software
23Foundation's Distributed Computing Environment (OSF DCE).  This allows Xend to
24be used with UUIDs generated as per the DCE specification, should that be
25required.  These UUIDs are also, by no coincidence, the same size as the
26'handle' stored by the Xen hypervisor along with the domain structure."""
27
28
29import commands
30import random
31
32
33def getUuidUuidgen(randomly = True):
34    """Generate a UUID using the command uuidgen.
35
36    If randomly is true (default) generates a random uuid.
37    If randomly is false generates a time-based uuid.
38    """
39    cmd = "uuidgen"
40    if randomly:
41        cmd += " -r"
42    else:
43        cmd += " -t"
44    return fromString(commands.getoutput(cmd))
45
46
47def getUuidRandom():
48    """Generate a random UUID."""
49   
50    return [ random.randint(0, 255) for _ in range(0, 16) ]
51
52
53#uuidFactory = getUuidUuidgen
54uuidFactory = getUuidRandom
55
56
57def toString(u):
58    return "-".join(["%02x" * 4, "%02x" * 2, "%02x" * 2, "%02x" * 2,
59                     "%02x" * 6]) % tuple(u)
60
61def fromString(s):
62    s = s.replace('-', '')
63    return [ int(s[i : i + 2], 16) for i in range(0, 32, 2) ]
64
65def create():
66    return uuidFactory()
67
68def createString():
69    return toString(create())
Note: See TracBrowser for help on using the repository browser.