10a8c90248264a8b26970b4473770bcc3df8515fJosh Gaor"""UUID objects (universally unique identifiers) according to RFC 4122. 20a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 30a8c90248264a8b26970b4473770bcc3df8515fJosh GaoThis module provides immutable UUID objects (class UUID) and the functions 40a8c90248264a8b26970b4473770bcc3df8515fJosh Gaouuid1(), uuid3(), uuid4(), uuid5() for generating version 1, 3, 4, and 5 50a8c90248264a8b26970b4473770bcc3df8515fJosh GaoUUIDs as specified in RFC 4122. 60a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 70a8c90248264a8b26970b4473770bcc3df8515fJosh GaoIf all you want is a unique ID, you should probably call uuid1() or uuid4(). 80a8c90248264a8b26970b4473770bcc3df8515fJosh GaoNote that uuid1() may compromise privacy since it creates a UUID containing 90a8c90248264a8b26970b4473770bcc3df8515fJosh Gaothe computer's network address. uuid4() creates a random UUID. 100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 110a8c90248264a8b26970b4473770bcc3df8515fJosh GaoTypical usage: 120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> import uuid 140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # make a UUID based on the host ID and current time 160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> uuid.uuid1() 170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao UUID('a8098c1a-f86e-11da-bd1a-00112444be1e') 180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # make a UUID using an MD5 hash of a namespace UUID and a name 200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org') 210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e') 220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # make a random UUID 240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> uuid.uuid4() 250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao UUID('16fd2706-8baf-433b-82eb-8c7fada847da') 260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # make a UUID using a SHA-1 hash of a namespace UUID and a name 280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org') 290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d') 300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # make a UUID from a string of hex digits (braces and hyphens ignored) 320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}') 330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # convert a UUID to a string of hex digits in standard form 350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> str(x) 360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '00010203-0405-0607-0809-0a0b0c0d0e0f' 370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # get the raw 16 bytes of the UUID 390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> x.bytes 400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f' 410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # make a UUID from a 16-byte string 430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> uuid.UUID(bytes=x.bytes) 440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao UUID('00010203-0405-0607-0809-0a0b0c0d0e0f') 450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao""" 460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao__author__ = 'Ka-Ping Yee <ping@zesty.ca>' 480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 490a8c90248264a8b26970b4473770bcc3df8515fJosh GaoRESERVED_NCS, RFC_4122, RESERVED_MICROSOFT, RESERVED_FUTURE = [ 500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'reserved for NCS compatibility', 'specified in RFC 4122', 510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'reserved for Microsoft compatibility', 'reserved for future definition'] 520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 530a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass UUID(object): 540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Instances of the UUID class represent UUIDs as specified in RFC 4122. 550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao UUID objects are immutable, hashable, and usable as dictionary keys. 560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Converting a UUID to a string with str() yields something in the form 570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '12345678-1234-1234-1234-123456789abc'. The UUID constructor accepts 580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao five possible forms: a similar string of hexadecimal digits, or a tuple 590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao of six integer fields (with 32-bit, 16-bit, 16-bit, 8-bit, 8-bit, and 600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 48-bit values respectively) as an argument named 'fields', or a string 610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao of 16 bytes (with all the integer fields in big-endian order) as an 620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao argument named 'bytes', or a string of 16 bytes (with the first three 630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fields in little-endian order) as an argument named 'bytes_le', or a 640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao single 128-bit integer as an argument named 'int'. 650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao UUIDs have these read-only attributes: 670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao bytes the UUID as a 16-byte string (containing the six 690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao integer fields in big-endian byte order) 700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao bytes_le the UUID as a 16-byte string (with time_low, time_mid, 720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao and time_hi_version in little-endian byte order) 730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fields a tuple of the six integer fields of the UUID, 750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao which are also available as six individual attributes 760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao and two derived attributes: 770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao time_low the first 32 bits of the UUID 790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao time_mid the next 16 bits of the UUID 800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao time_hi_version the next 16 bits of the UUID 810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao clock_seq_hi_variant the next 8 bits of the UUID 820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao clock_seq_low the next 8 bits of the UUID 830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao node the last 48 bits of the UUID 840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao time the 60-bit timestamp 860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao clock_seq the 14-bit sequence number 870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao hex the UUID as a 32-character hexadecimal string 890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao int the UUID as a 128-bit integer 910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao urn the UUID as a URN as specified in RFC 4122 930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao variant the UUID variant (one of the constants RESERVED_NCS, 950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao RFC_4122, RESERVED_MICROSOFT, or RESERVED_FUTURE) 960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao version the UUID version number (1 through 5, meaningful only 980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao when the variant is RFC_4122) 990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 1000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, hex=None, bytes=None, bytes_le=None, fields=None, 1020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao int=None, version=None): 1030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao r"""Create a UUID from either a string of 32 hexadecimal digits, 1040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a string of 16 bytes as the 'bytes' argument, a string of 16 bytes 1050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao in little-endian order as the 'bytes_le' argument, a tuple of six 1060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao integers (32-bit time_low, 16-bit time_mid, 16-bit time_hi_version, 1070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8-bit clock_seq_hi_variant, 8-bit clock_seq_low, 48-bit node) as 1080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao the 'fields' argument, or a single 128-bit integer as the 'int' 1090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao argument. When a string of hex digits is given, curly braces, 1100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao hyphens, and a URN prefix are all optional. For example, these 1110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expressions all yield the same UUID: 1120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao UUID('{12345678-1234-5678-1234-567812345678}') 1140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao UUID('12345678123456781234567812345678') 1150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao UUID('urn:uuid:12345678-1234-5678-1234-567812345678') 1160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao UUID(bytes='\x12\x34\x56\x78'*4) 1170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao UUID(bytes_le='\x78\x56\x34\x12\x34\x12\x78\x56' + 1180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '\x12\x34\x56\x78\x12\x34\x56\x78') 1190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao UUID(fields=(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678)) 1200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao UUID(int=0x12345678123456781234567812345678) 1210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Exactly one of 'hex', 'bytes', 'bytes_le', 'fields', or 'int' must 1230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao be given. The 'version' argument is optional; if given, the resulting 1240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao UUID will have its variant and version set according to RFC 4122, 1250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao overriding the given 'hex', 'bytes', 'bytes_le', 'fields', or 'int'. 1260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 1270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if [hex, bytes, bytes_le, fields, int].count(None) != 4: 1290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise TypeError('need one of hex, bytes, bytes_le, fields, or int') 1300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if hex is not None: 1310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao hex = hex.replace('urn:', '').replace('uuid:', '') 1320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao hex = hex.strip('{}').replace('-', '') 1330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if len(hex) != 32: 1340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError('badly formed hexadecimal UUID string') 1350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao int = long(hex, 16) 1360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if bytes_le is not None: 1370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if len(bytes_le) != 16: 1380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError('bytes_le is not a 16-char string') 1390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao bytes = (bytes_le[3] + bytes_le[2] + bytes_le[1] + bytes_le[0] + 1400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao bytes_le[5] + bytes_le[4] + bytes_le[7] + bytes_le[6] + 1410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao bytes_le[8:]) 1420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if bytes is not None: 1430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if len(bytes) != 16: 1440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError('bytes is not a 16-char string') 1450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao int = long(('%02x'*16) % tuple(map(ord, bytes)), 16) 1460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if fields is not None: 1470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if len(fields) != 6: 1480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError('fields is not a 6-tuple') 1490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (time_low, time_mid, time_hi_version, 1500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao clock_seq_hi_variant, clock_seq_low, node) = fields 1510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not 0 <= time_low < 1<<32L: 1520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError('field 1 out of range (need a 32-bit value)') 1530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not 0 <= time_mid < 1<<16L: 1540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError('field 2 out of range (need a 16-bit value)') 1550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not 0 <= time_hi_version < 1<<16L: 1560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError('field 3 out of range (need a 16-bit value)') 1570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not 0 <= clock_seq_hi_variant < 1<<8L: 1580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError('field 4 out of range (need an 8-bit value)') 1590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not 0 <= clock_seq_low < 1<<8L: 1600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError('field 5 out of range (need an 8-bit value)') 1610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not 0 <= node < 1<<48L: 1620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError('field 6 out of range (need a 48-bit value)') 1630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao clock_seq = (clock_seq_hi_variant << 8L) | clock_seq_low 1640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao int = ((time_low << 96L) | (time_mid << 80L) | 1650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (time_hi_version << 64L) | (clock_seq << 48L) | node) 1660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if int is not None: 1670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not 0 <= int < 1<<128L: 1680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError('int is out of range (need a 128-bit value)') 1690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if version is not None: 1700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not 1 <= version <= 5: 1710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError('illegal version number') 1720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Set the variant to RFC 4122. 1730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao int &= ~(0xc000 << 48L) 1740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao int |= 0x8000 << 48L 1750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Set the version number. 1760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao int &= ~(0xf000 << 64L) 1770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao int |= version << 76L 1780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.__dict__['int'] = int 1790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __cmp__(self, other): 1810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if isinstance(other, UUID): 1820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return cmp(self.int, other.int) 1830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return NotImplemented 1840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __hash__(self): 1860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return hash(self.int) 1870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __int__(self): 1890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self.int 1900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __repr__(self): 1920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 'UUID(%r)' % str(self) 1930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __setattr__(self, name, value): 1950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise TypeError('UUID objects are immutable') 1960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __str__(self): 1980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao hex = '%032x' % self.int 1990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return '%s-%s-%s-%s-%s' % ( 2000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao hex[:8], hex[8:12], hex[12:16], hex[16:20], hex[20:]) 2010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def get_bytes(self): 2030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao bytes = '' 2040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for shift in range(0, 128, 8): 2050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao bytes = chr((self.int >> shift) & 0xff) + bytes 2060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return bytes 2070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao bytes = property(get_bytes) 2090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def get_bytes_le(self): 2110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao bytes = self.bytes 2120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (bytes[3] + bytes[2] + bytes[1] + bytes[0] + 2130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao bytes[5] + bytes[4] + bytes[7] + bytes[6] + bytes[8:]) 2140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao bytes_le = property(get_bytes_le) 2160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def get_fields(self): 2180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (self.time_low, self.time_mid, self.time_hi_version, 2190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.clock_seq_hi_variant, self.clock_seq_low, self.node) 2200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fields = property(get_fields) 2220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def get_time_low(self): 2240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self.int >> 96L 2250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao time_low = property(get_time_low) 2270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def get_time_mid(self): 2290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (self.int >> 80L) & 0xffff 2300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao time_mid = property(get_time_mid) 2320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def get_time_hi_version(self): 2340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (self.int >> 64L) & 0xffff 2350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao time_hi_version = property(get_time_hi_version) 2370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def get_clock_seq_hi_variant(self): 2390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (self.int >> 56L) & 0xff 2400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao clock_seq_hi_variant = property(get_clock_seq_hi_variant) 2420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def get_clock_seq_low(self): 2440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (self.int >> 48L) & 0xff 2450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao clock_seq_low = property(get_clock_seq_low) 2470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def get_time(self): 2490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (((self.time_hi_version & 0x0fffL) << 48L) | 2500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (self.time_mid << 32L) | self.time_low) 2510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao time = property(get_time) 2530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def get_clock_seq(self): 2550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (((self.clock_seq_hi_variant & 0x3fL) << 8L) | 2560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.clock_seq_low) 2570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao clock_seq = property(get_clock_seq) 2590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def get_node(self): 2610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self.int & 0xffffffffffff 2620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao node = property(get_node) 2640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def get_hex(self): 2660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return '%032x' % self.int 2670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao hex = property(get_hex) 2690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def get_urn(self): 2710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 'urn:uuid:' + str(self) 2720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao urn = property(get_urn) 2740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def get_variant(self): 2760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self.int & (0x8000 << 48L): 2770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return RESERVED_NCS 2780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif not self.int & (0x4000 << 48L): 2790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return RFC_4122 2800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif not self.int & (0x2000 << 48L): 2810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return RESERVED_MICROSOFT 2820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 2830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return RESERVED_FUTURE 2840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao variant = property(get_variant) 2860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def get_version(self): 2880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # The version bits are only meaningful for RFC 4122 UUIDs. 2890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self.variant == RFC_4122: 2900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return int((self.int >> 76L) & 0xf) 2910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao version = property(get_version) 2930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2940a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _find_mac(command, args, hw_identifiers, get_index): 2950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao import os 2960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for dir in ['', '/sbin/', '/usr/sbin']: 2970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao executable = os.path.join(dir, command) 2980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not os.path.exists(executable): 2990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao continue 3000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 3020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # LC_ALL to get English output, 2>/dev/null to 3030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # prevent output on stderr 3040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao cmd = 'LC_ALL=C %s %s 2>/dev/null' % (executable, args) 3050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao with os.popen(cmd) as pipe: 3060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for line in pipe: 3070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao words = line.lower().split() 3080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for i in range(len(words)): 3090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if words[i] in hw_identifiers: 3100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return int( 3110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao words[get_index(i)].replace(':', ''), 16) 3120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except IOError: 3130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao continue 3140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return None 3150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3160a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _ifconfig_getnode(): 3170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Get the hardware address on Unix by running ifconfig.""" 3180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # This works on Linux ('' or '-a'), Tru64 ('-av'), but not all Unixes. 3200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for args in ('', '-a', '-av'): 3210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao mac = _find_mac('ifconfig', args, ['hwaddr', 'ether'], lambda i: i+1) 3220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if mac: 3230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return mac 3240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao import socket 3260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ip_addr = socket.gethostbyname(socket.gethostname()) 3270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Try getting the MAC addr from arp based on our IP address (Solaris). 3290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao mac = _find_mac('arp', '-an', [ip_addr], lambda i: -1) 3300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if mac: 3310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return mac 3320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # This might work on HP-UX. 3340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao mac = _find_mac('lanscan', '-ai', ['lan0'], lambda i: 0) 3350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if mac: 3360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return mac 3370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return None 3390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3400a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _ipconfig_getnode(): 3410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Get the hardware address on Windows by running ipconfig.exe.""" 3420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao import os, re 3430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dirs = ['', r'c:\windows\system32', r'c:\winnt\system32'] 3440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 3450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao import ctypes 3460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao buffer = ctypes.create_string_buffer(300) 3470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ctypes.windll.kernel32.GetSystemDirectoryA(buffer, 300) 3480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dirs.insert(0, buffer.value.decode('mbcs')) 3490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except: 3500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 3510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for dir in dirs: 3520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 3530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pipe = os.popen(os.path.join(dir, 'ipconfig') + ' /all') 3540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except IOError: 3550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao continue 3560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 3570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for line in pipe: 3580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao value = line.split(':')[-1].strip().lower() 3590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if re.match('([0-9a-f][0-9a-f]-){5}[0-9a-f][0-9a-f]', value): 3600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return int(value.replace('-', ''), 16) 3610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao finally: 3620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pipe.close() 3630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3640a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _netbios_getnode(): 3650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Get the hardware address on Windows using NetBIOS calls. 3660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao See http://support.microsoft.com/kb/118623 for details.""" 3670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao import win32wnet, netbios 3680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ncb = netbios.NCB() 3690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ncb.Command = netbios.NCBENUM 3700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ncb.Buffer = adapters = netbios.LANA_ENUM() 3710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao adapters._pack() 3720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if win32wnet.Netbios(ncb) != 0: 3730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 3740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao adapters._unpack() 3750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for i in range(adapters.length): 3760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ncb.Reset() 3770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ncb.Command = netbios.NCBRESET 3780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ncb.Lana_num = ord(adapters.lana[i]) 3790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if win32wnet.Netbios(ncb) != 0: 3800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao continue 3810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ncb.Reset() 3820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ncb.Command = netbios.NCBASTAT 3830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ncb.Lana_num = ord(adapters.lana[i]) 3840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ncb.Callname = '*'.ljust(16) 3850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ncb.Buffer = status = netbios.ADAPTER_STATUS() 3860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if win32wnet.Netbios(ncb) != 0: 3870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao continue 3880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao status._unpack() 3890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao bytes = map(ord, status.adapter_address) 3900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ((bytes[0]<<40L) + (bytes[1]<<32L) + (bytes[2]<<24L) + 3910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (bytes[3]<<16L) + (bytes[4]<<8L) + bytes[5]) 3920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Thanks to Thomas Heller for ctypes and for his help with its use here. 3940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# If ctypes is available, use it to find system routines for UUID generation. 3960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_uuid_generate_random = _uuid_generate_time = _UuidCreate = None 3970a8c90248264a8b26970b4473770bcc3df8515fJosh Gaotry: 3980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao import ctypes, ctypes.util 3990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # The uuid_generate_* routines are provided by libuuid on at least 4010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Linux and FreeBSD, and provided by libc on Mac OS X. 4020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for libname in ['uuid', 'c']: 4030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 4040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao lib = ctypes.CDLL(ctypes.util.find_library(libname)) 4050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except: 4060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao continue 4070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if hasattr(lib, 'uuid_generate_random'): 4080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao _uuid_generate_random = lib.uuid_generate_random 4090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if hasattr(lib, 'uuid_generate_time'): 4100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao _uuid_generate_time = lib.uuid_generate_time 4110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # The uuid_generate_* functions are broken on MacOS X 10.5, as noted 4130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # in issue #8621 the function generates the same sequence of values 4140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # in the parent process and all children created using fork (unless 4150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # those children use exec as well). 4160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 4170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Assume that the uuid_generate functions are broken from 10.5 onward, 4180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # the test can be adjusted when a later version is fixed. 4190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao import sys 4200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if sys.platform == 'darwin': 4210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao import os 4220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if int(os.uname()[2].split('.')[0]) >= 9: 4230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao _uuid_generate_random = _uuid_generate_time = None 4240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # On Windows prior to 2000, UuidCreate gives a UUID containing the 4260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # hardware address. On Windows 2000 and later, UuidCreate makes a 4270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # random UUID and UuidCreateSequential gives a UUID containing the 4280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # hardware address. These routines are provided by the RPC runtime. 4290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # NOTE: at least on Tim's WinXP Pro SP2 desktop box, while the last 4300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 6 bytes returned by UuidCreateSequential are fixed, they don't appear 4310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # to bear any relationship to the MAC address of any network device 4320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # on the box. 4330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 4340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao lib = ctypes.windll.rpcrt4 4350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except: 4360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao lib = None 4370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao _UuidCreate = getattr(lib, 'UuidCreateSequential', 4380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao getattr(lib, 'UuidCreate', None)) 4390a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoexcept: 4400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 4410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4420a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _unixdll_getnode(): 4430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Get the hardware address on Unix using ctypes.""" 4440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao _buffer = ctypes.create_string_buffer(16) 4450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao _uuid_generate_time(_buffer) 4460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return UUID(bytes=_buffer.raw).node 4470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4480a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _windll_getnode(): 4490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Get the hardware address on Windows using ctypes.""" 4500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao _buffer = ctypes.create_string_buffer(16) 4510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if _UuidCreate(_buffer) == 0: 4520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return UUID(bytes=_buffer.raw).node 4530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4540a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _random_getnode(): 4550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Get a random node ID, with eighth bit set as suggested by RFC 4122.""" 4560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao import random 4570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return random.randrange(0, 1<<48L) | 0x010000000000L 4580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_node = None 4600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4610a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef getnode(): 4620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Get the hardware address as a 48-bit positive integer. 4630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The first time this runs, it may launch a separate program, which could 4650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao be quite slow. If all attempts to obtain the hardware address fail, we 4660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao choose a random 48-bit number with its eighth bit set to 1 as recommended 4670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao in RFC 4122. 4680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 4690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao global _node 4710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if _node is not None: 4720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _node 4730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao import sys 4750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if sys.platform == 'win32': 4760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao getters = [_windll_getnode, _netbios_getnode, _ipconfig_getnode] 4770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 4780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao getters = [_unixdll_getnode, _ifconfig_getnode] 4790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for getter in getters + [_random_getnode]: 4810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 4820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao _node = getter() 4830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except: 4840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao continue 4850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if _node is not None: 4860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _node 4870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_last_timestamp = None 4890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4900a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef uuid1(node=None, clock_seq=None): 4910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Generate a UUID from a host ID, sequence number, and the current time. 4920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao If 'node' is not given, getnode() is used to obtain the hardware 4930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao address. If 'clock_seq' is given, it is used as the sequence number; 4940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao otherwise a random 14-bit sequence number is chosen.""" 4950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # When the system provides a version-1 UUID generator, use it (but don't 4970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # use UuidCreate here because its UUIDs don't conform to RFC 4122). 4980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if _uuid_generate_time and node is clock_seq is None: 4990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao _buffer = ctypes.create_string_buffer(16) 5000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao _uuid_generate_time(_buffer) 5010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return UUID(bytes=_buffer.raw) 5020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao global _last_timestamp 5040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao import time 5050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao nanoseconds = int(time.time() * 1e9) 5060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 0x01b21dd213814000 is the number of 100-ns intervals between the 5070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00. 5080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao timestamp = int(nanoseconds//100) + 0x01b21dd213814000L 5090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if _last_timestamp is not None and timestamp <= _last_timestamp: 5100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao timestamp = _last_timestamp + 1 5110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao _last_timestamp = timestamp 5120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if clock_seq is None: 5130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao import random 5140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao clock_seq = random.randrange(1<<14L) # instead of stable storage 5150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao time_low = timestamp & 0xffffffffL 5160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao time_mid = (timestamp >> 32L) & 0xffffL 5170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao time_hi_version = (timestamp >> 48L) & 0x0fffL 5180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao clock_seq_low = clock_seq & 0xffL 5190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao clock_seq_hi_variant = (clock_seq >> 8L) & 0x3fL 5200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if node is None: 5210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao node = getnode() 5220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return UUID(fields=(time_low, time_mid, time_hi_version, 5230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao clock_seq_hi_variant, clock_seq_low, node), version=1) 5240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5250a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef uuid3(namespace, name): 5260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Generate a UUID from the MD5 hash of a namespace UUID and a name.""" 5270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao from hashlib import md5 5280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao hash = md5(namespace.bytes + name).digest() 5290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return UUID(bytes=hash[:16], version=3) 5300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5310a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef uuid4(): 5320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Generate a random UUID.""" 5330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # When the system provides a version-4 UUID generator, use it. 5350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if _uuid_generate_random: 5360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao _buffer = ctypes.create_string_buffer(16) 5370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao _uuid_generate_random(_buffer) 5380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return UUID(bytes=_buffer.raw) 5390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Otherwise, get randomness from urandom or the 'random' module. 5410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 5420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao import os 5430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return UUID(bytes=os.urandom(16), version=4) 5440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except: 5450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao import random 5460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao bytes = [chr(random.randrange(256)) for i in range(16)] 5470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return UUID(bytes=bytes, version=4) 5480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5490a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef uuid5(namespace, name): 5500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Generate a UUID from the SHA-1 hash of a namespace UUID and a name.""" 5510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao from hashlib import sha1 5520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao hash = sha1(namespace.bytes + name).digest() 5530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return UUID(bytes=hash[:16], version=5) 5540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# The following standard UUIDs are for use with uuid3() or uuid5(). 5560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5570a8c90248264a8b26970b4473770bcc3df8515fJosh GaoNAMESPACE_DNS = UUID('6ba7b810-9dad-11d1-80b4-00c04fd430c8') 5580a8c90248264a8b26970b4473770bcc3df8515fJosh GaoNAMESPACE_URL = UUID('6ba7b811-9dad-11d1-80b4-00c04fd430c8') 5590a8c90248264a8b26970b4473770bcc3df8515fJosh GaoNAMESPACE_OID = UUID('6ba7b812-9dad-11d1-80b4-00c04fd430c8') 5600a8c90248264a8b26970b4473770bcc3df8515fJosh GaoNAMESPACE_X500 = UUID('6ba7b814-9dad-11d1-80b4-00c04fd430c8') 561