ia64/xen-unstable

changeset 7286:59440a003278

Remove code seeding RNG from /dev/urandom. The random module's default RNG is
already seeded from the clock, so this is unnecessary, non-portable, and
expensive. This should improve start-up time of Xend. Replace twisty maze of
code with something sensible.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@ewan
date Sun Oct 09 11:57:24 2005 +0100 (2005-10-09)
parents f15892b95965
children 8e3ec9ab0075
files tools/python/xen/xend/uuid.py
line diff
     1.1 --- a/tools/python/xen/xend/uuid.py	Sun Oct 09 11:53:34 2005 +0100
     1.2 +++ b/tools/python/xen/xend/uuid.py	Sun Oct 09 11:57:24 2005 +0100
     1.3 @@ -13,14 +13,19 @@
     1.4  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     1.5  #============================================================================
     1.6  # Copyright (C) 2005 Mike Wray <mike.wray@hp.com>
     1.7 +# Copyright (C) 2005 XenSource Ltd
     1.8  #============================================================================
     1.9  
    1.10 +
    1.11  """Universal(ly) Unique Identifiers (UUIDs).
    1.12  """
    1.13 +
    1.14 +
    1.15  import commands
    1.16  import random
    1.17  
    1.18 -def uuidgen(random=True):
    1.19 +
    1.20 +def getUuidUuidgen(random = True):
    1.21      """Generate a UUID using the command uuidgen.
    1.22  
    1.23      If random is true (default) generates a random uuid.
    1.24 @@ -33,50 +38,21 @@ def uuidgen(random=True):
    1.25          cmd += " -t"
    1.26      return commands.getoutput(cmd)
    1.27  
    1.28 -class UuidFactoryUuidgen:
    1.29 -
    1.30 -    """A uuid factory using uuidgen."""
    1.31 -
    1.32 -    def __init__(self):
    1.33 -        pass
    1.34 -
    1.35 -    def getUuid(self):
    1.36 -        return uuidgen()
    1.37 -
    1.38 -class UuidFactoryRandom:
    1.39 -
    1.40 -    """A random uuid factory."""
    1.41 -
    1.42 -    def __init__(self):
    1.43 -        f = file("/dev/urandom", "r")
    1.44 -        seed = f.read(16)
    1.45 -        f.close()
    1.46 -        self.rand = random.Random(seed)
    1.47  
    1.48 -    def randBytes(self, n):
    1.49 -        return [ self.rand.randint(0, 255) for i in range(0, n) ]
    1.50 -
    1.51 -    def getUuid(self):
    1.52 -        bytes = self.randBytes(16)
    1.53 -        # Encode the variant.
    1.54 -        bytes[6] = (bytes[6] & 0x0f) | 0x40
    1.55 -        bytes[8] = (bytes[8] & 0x3f) | 0x80
    1.56 -        f = "%02x"
    1.57 -        return ( "-".join([f*4, f*2, f*2, f*2, f*6]) % tuple(bytes) )
    1.58 +def getUuidRandom():
    1.59 +    """Generate a random UUID."""
    1.60 +    
    1.61 +    bytes = [ random.randint(0, 255) for i in range(0, 16) ]
    1.62 +    # Encode the variant.
    1.63 +    bytes[6] = (bytes[6] & 0x0f) | 0x40
    1.64 +    bytes[8] = (bytes[8] & 0x3f) | 0x80
    1.65 +    f = "%02x"
    1.66 +    return ( "-".join([f*4, f*2, f*2, f*2, f*6]) % tuple(bytes) )
    1.67  
    1.68 -def getFactory():
    1.69 -    """Get the factory to use for creating uuids.
    1.70 -    This is so it's easy to change the uuid factory.
    1.71 -    For example, for testing we might want repeatable uuids
    1.72 -    rather than the random ones we normally use.
    1.73 -    """
    1.74 -    global uuidFactory
    1.75 -    try:
    1.76 -        uuidFactory
    1.77 -    except:
    1.78 -        #uuidFactory = UuidFactoryUuidgen()
    1.79 -        uuidFactory = UuidFactoryRandom()
    1.80 -    return uuidFactory
    1.81 +
    1.82 +#uuidFactory = getUuidUuidgen
    1.83 +uuidFactory = getUuidRandom
    1.84 +
    1.85  
    1.86  def getUuid():
    1.87 -    return getFactory().getUuid()
    1.88 +    return uuidFactory()