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 |
---|
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.""" |
---|
27 | |
---|
28 | |
---|
29 | import commands |
---|
30 | import random |
---|
31 | |
---|
32 | |
---|
33 | def 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 | |
---|
47 | def getUuidRandom(): |
---|
48 | """Generate a random UUID.""" |
---|
49 | |
---|
50 | return [ random.randint(0, 255) for _ in range(0, 16) ] |
---|
51 | |
---|
52 | |
---|
53 | #uuidFactory = getUuidUuidgen |
---|
54 | uuidFactory = getUuidRandom |
---|
55 | |
---|
56 | |
---|
57 | def toString(u): |
---|
58 | return "-".join(["%02x" * 4, "%02x" * 2, "%02x" * 2, "%02x" * 2, |
---|
59 | "%02x" * 6]) % tuple(u) |
---|
60 | |
---|
61 | def fromString(s): |
---|
62 | s = s.replace('-', '') |
---|
63 | return [ int(s[i : i + 2], 16) for i in range(0, 32, 2) ] |
---|
64 | |
---|
65 | def create(): |
---|
66 | return uuidFactory() |
---|
67 | |
---|
68 | def createString(): |
---|
69 | return toString(create()) |
---|