ia64/xen-unstable

view tools/python/xen/xend/uuid.py @ 12725:36fe7ca48e54

Tidy up the creation of directories that Xend needs. This avoids potential
races in this creation.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author Ewan Mellor <ewan@xensource.com>
date Fri Dec 01 11:32:32 2006 +0000 (2006-12-01)
parents a3788998952d
children
line source
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 #============================================================================
20 """Universal Unique Identifiers (UUIDs). By default, UUIDs generated here are
21 purely random, with no internal structure. However, they are the same size,
22 and are formatted by the same conventions, as the UUIDs in the Open Software
23 Foundation's Distributed Computing Environment (OSF DCE). This allows Xend to
24 be used with UUIDs generated as per the DCE specification, should that be
25 required. These UUIDs are also, by no coincidence, the same size as the
26 'handle' stored by the Xen hypervisor along with the domain structure."""
29 import commands
30 import random
33 def getUuidUuidgen(randomly = True):
34 """Generate a UUID using the command uuidgen.
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))
47 def getUuidRandom():
48 """Generate a random UUID."""
50 return [ random.randint(0, 255) for _ in range(0, 16) ]
53 #uuidFactory = getUuidUuidgen
54 uuidFactory = getUuidRandom
57 def toString(u):
58 return "-".join(["%02x" * 4, "%02x" * 2, "%02x" * 2, "%02x" * 2,
59 "%02x" * 6]) % tuple(u)
61 def fromString(s):
62 s = s.replace('-', '')
63 return [ int(s[i : i + 2], 16) for i in range(0, 32, 2) ]
65 def create():
66 return uuidFactory()
68 def createString():
69 return toString(create())