blob: 9cb73e87718122c3a4cd7ee38d1e21e2de996177 [file] [log] [blame]
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001r"""UUID objects (universally unique identifiers) according to RFC 4122.
2
3This module provides immutable UUID objects (class UUID) and the functions
4uuid1(), uuid3(), uuid4(), uuid5() for generating version 1, 3, 4, and 5
5UUIDs as specified in RFC 4122.
6
7If all you want is a unique ID, you should probably call uuid1() or uuid4().
8Note that uuid1() may compromise privacy since it creates a UUID containing
9the computer's network address. uuid4() creates a random UUID.
10
11Typical usage:
12
13 >>> import uuid
14
15 # make a UUID based on the host ID and current time
Georg Brandl1d523e12009-12-19 18:23:28 +000016 >>> uuid.uuid1() # doctest: +SKIP
Thomas Wouters0e3f5912006-08-11 14:57:12 +000017 UUID('a8098c1a-f86e-11da-bd1a-00112444be1e')
18
19 # make a UUID using an MD5 hash of a namespace UUID and a name
20 >>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
21 UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e')
22
23 # make a random UUID
Georg Brandl1d523e12009-12-19 18:23:28 +000024 >>> uuid.uuid4() # doctest: +SKIP
Thomas Wouters0e3f5912006-08-11 14:57:12 +000025 UUID('16fd2706-8baf-433b-82eb-8c7fada847da')
26
27 # make a UUID using a SHA-1 hash of a namespace UUID and a name
28 >>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
29 UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d')
30
31 # make a UUID from a string of hex digits (braces and hyphens ignored)
32 >>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}')
33
34 # convert a UUID to a string of hex digits in standard form
35 >>> str(x)
36 '00010203-0405-0607-0809-0a0b0c0d0e0f'
37
38 # get the raw 16 bytes of the UUID
39 >>> x.bytes
Guido van Rossum65b6a802007-07-09 14:03:08 +000040 b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f'
Thomas Wouters0e3f5912006-08-11 14:57:12 +000041
42 # make a UUID from a 16-byte string
43 >>> uuid.UUID(bytes=x.bytes)
44 UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')
45"""
46
Benjamin Peterson788cb522015-10-29 20:38:04 -070047import os
Antoine Pitroua106aec2017-09-28 23:03:06 +020048import sys
Benjamin Peterson788cb522015-10-29 20:38:04 -070049
Barry Warsaw8c130d72017-02-18 15:45:49 -050050from enum import Enum
51
52
Thomas Wouters0e3f5912006-08-11 14:57:12 +000053__author__ = 'Ka-Ping Yee <ping@zesty.ca>'
Thomas Wouters0e3f5912006-08-11 14:57:12 +000054
55RESERVED_NCS, RFC_4122, RESERVED_MICROSOFT, RESERVED_FUTURE = [
56 'reserved for NCS compatibility', 'specified in RFC 4122',
57 'reserved for Microsoft compatibility', 'reserved for future definition']
58
Guido van Rossum65b6a802007-07-09 14:03:08 +000059int_ = int # The built-in int type
60bytes_ = bytes # The built-in bytes type
Guido van Rossume2a383d2007-01-15 16:59:06 +000061
Barry Warsaw8c130d72017-02-18 15:45:49 -050062
63class SafeUUID(Enum):
64 safe = 0
65 unsafe = -1
66 unknown = None
67
68
69class UUID:
Thomas Wouters0e3f5912006-08-11 14:57:12 +000070 """Instances of the UUID class represent UUIDs as specified in RFC 4122.
71 UUID objects are immutable, hashable, and usable as dictionary keys.
72 Converting a UUID to a string with str() yields something in the form
73 '12345678-1234-1234-1234-123456789abc'. The UUID constructor accepts
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000074 five possible forms: a similar string of hexadecimal digits, or a tuple
75 of six integer fields (with 32-bit, 16-bit, 16-bit, 8-bit, 8-bit, and
76 48-bit values respectively) as an argument named 'fields', or a string
77 of 16 bytes (with all the integer fields in big-endian order) as an
78 argument named 'bytes', or a string of 16 bytes (with the first three
79 fields in little-endian order) as an argument named 'bytes_le', or a
80 single 128-bit integer as an argument named 'int'.
Thomas Wouters0e3f5912006-08-11 14:57:12 +000081
82 UUIDs have these read-only attributes:
83
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000084 bytes the UUID as a 16-byte string (containing the six
85 integer fields in big-endian byte order)
86
87 bytes_le the UUID as a 16-byte string (with time_low, time_mid,
88 and time_hi_version in little-endian byte order)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000089
90 fields a tuple of the six integer fields of the UUID,
91 which are also available as six individual attributes
92 and two derived attributes:
93
94 time_low the first 32 bits of the UUID
95 time_mid the next 16 bits of the UUID
96 time_hi_version the next 16 bits of the UUID
97 clock_seq_hi_variant the next 8 bits of the UUID
98 clock_seq_low the next 8 bits of the UUID
99 node the last 48 bits of the UUID
100
101 time the 60-bit timestamp
102 clock_seq the 14-bit sequence number
103
104 hex the UUID as a 32-character hexadecimal string
105
106 int the UUID as a 128-bit integer
107
108 urn the UUID as a URN as specified in RFC 4122
109
110 variant the UUID variant (one of the constants RESERVED_NCS,
111 RFC_4122, RESERVED_MICROSOFT, or RESERVED_FUTURE)
112
113 version the UUID version number (1 through 5, meaningful only
114 when the variant is RFC_4122)
Barry Warsaw8c130d72017-02-18 15:45:49 -0500115
116 is_safe An enum indicating whether the UUID has been generated in
117 a way that is safe for multiprocessing applications, via
118 uuid_generate_time_safe(3).
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000119 """
120
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000121 def __init__(self, hex=None, bytes=None, bytes_le=None, fields=None,
Barry Warsaw8c130d72017-02-18 15:45:49 -0500122 int=None, version=None,
123 *, is_safe=SafeUUID.unknown):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000124 r"""Create a UUID from either a string of 32 hexadecimal digits,
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000125 a string of 16 bytes as the 'bytes' argument, a string of 16 bytes
126 in little-endian order as the 'bytes_le' argument, a tuple of six
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000127 integers (32-bit time_low, 16-bit time_mid, 16-bit time_hi_version,
128 8-bit clock_seq_hi_variant, 8-bit clock_seq_low, 48-bit node) as
129 the 'fields' argument, or a single 128-bit integer as the 'int'
130 argument. When a string of hex digits is given, curly braces,
131 hyphens, and a URN prefix are all optional. For example, these
132 expressions all yield the same UUID:
133
134 UUID('{12345678-1234-5678-1234-567812345678}')
135 UUID('12345678123456781234567812345678')
136 UUID('urn:uuid:12345678-1234-5678-1234-567812345678')
137 UUID(bytes='\x12\x34\x56\x78'*4)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000138 UUID(bytes_le='\x78\x56\x34\x12\x34\x12\x78\x56' +
139 '\x12\x34\x56\x78\x12\x34\x56\x78')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000140 UUID(fields=(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678))
141 UUID(int=0x12345678123456781234567812345678)
142
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000143 Exactly one of 'hex', 'bytes', 'bytes_le', 'fields', or 'int' must
144 be given. The 'version' argument is optional; if given, the resulting
145 UUID will have its variant and version set according to RFC 4122,
146 overriding the given 'hex', 'bytes', 'bytes_le', 'fields', or 'int'.
Barry Warsaw8c130d72017-02-18 15:45:49 -0500147
148 is_safe is an enum exposed as an attribute on the instance. It
149 indicates whether the UUID has been generated in a way that is safe
150 for multiprocessing applications, via uuid_generate_time_safe(3).
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000151 """
152
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000153 if [hex, bytes, bytes_le, fields, int].count(None) != 4:
Berker Peksagd02eb8a2016-03-20 16:49:10 +0200154 raise TypeError('one of the hex, bytes, bytes_le, fields, '
155 'or int arguments must be given')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000156 if hex is not None:
157 hex = hex.replace('urn:', '').replace('uuid:', '')
158 hex = hex.strip('{}').replace('-', '')
159 if len(hex) != 32:
160 raise ValueError('badly formed hexadecimal UUID string')
Guido van Rossume2a383d2007-01-15 16:59:06 +0000161 int = int_(hex, 16)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000162 if bytes_le is not None:
163 if len(bytes_le) != 16:
164 raise ValueError('bytes_le is not a 16-char string')
Serhiy Storchakafa9be4f2014-09-06 22:14:04 +0300165 bytes = (bytes_le[4-1::-1] + bytes_le[6-1:4-1:-1] +
166 bytes_le[8-1:6-1:-1] + bytes_le[8:])
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000167 if bytes is not None:
168 if len(bytes) != 16:
169 raise ValueError('bytes is not a 16-char string')
Guido van Rossum65b6a802007-07-09 14:03:08 +0000170 assert isinstance(bytes, bytes_), repr(bytes)
Philip Jenvey1221f6b2013-08-29 18:33:50 -0700171 int = int_.from_bytes(bytes, byteorder='big')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000172 if fields is not None:
173 if len(fields) != 6:
174 raise ValueError('fields is not a 6-tuple')
175 (time_low, time_mid, time_hi_version,
176 clock_seq_hi_variant, clock_seq_low, node) = fields
Guido van Rossume2a383d2007-01-15 16:59:06 +0000177 if not 0 <= time_low < 1<<32:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000178 raise ValueError('field 1 out of range (need a 32-bit value)')
Guido van Rossume2a383d2007-01-15 16:59:06 +0000179 if not 0 <= time_mid < 1<<16:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000180 raise ValueError('field 2 out of range (need a 16-bit value)')
Guido van Rossume2a383d2007-01-15 16:59:06 +0000181 if not 0 <= time_hi_version < 1<<16:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000182 raise ValueError('field 3 out of range (need a 16-bit value)')
Guido van Rossume2a383d2007-01-15 16:59:06 +0000183 if not 0 <= clock_seq_hi_variant < 1<<8:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000184 raise ValueError('field 4 out of range (need an 8-bit value)')
Guido van Rossume2a383d2007-01-15 16:59:06 +0000185 if not 0 <= clock_seq_low < 1<<8:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000186 raise ValueError('field 5 out of range (need an 8-bit value)')
Guido van Rossume2a383d2007-01-15 16:59:06 +0000187 if not 0 <= node < 1<<48:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000188 raise ValueError('field 6 out of range (need a 48-bit value)')
Guido van Rossume2a383d2007-01-15 16:59:06 +0000189 clock_seq = (clock_seq_hi_variant << 8) | clock_seq_low
190 int = ((time_low << 96) | (time_mid << 80) |
191 (time_hi_version << 64) | (clock_seq << 48) | node)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000192 if int is not None:
Guido van Rossume2a383d2007-01-15 16:59:06 +0000193 if not 0 <= int < 1<<128:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000194 raise ValueError('int is out of range (need a 128-bit value)')
195 if version is not None:
196 if not 1 <= version <= 5:
197 raise ValueError('illegal version number')
198 # Set the variant to RFC 4122.
Guido van Rossume2a383d2007-01-15 16:59:06 +0000199 int &= ~(0xc000 << 48)
200 int |= 0x8000 << 48
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000201 # Set the version number.
Guido van Rossume2a383d2007-01-15 16:59:06 +0000202 int &= ~(0xf000 << 64)
203 int |= version << 76
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000204 self.__dict__['int'] = int
Barry Warsaw8c130d72017-02-18 15:45:49 -0500205 self.__dict__['is_safe'] = is_safe
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000206
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000207 def __eq__(self, other):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000208 if isinstance(other, UUID):
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000209 return self.int == other.int
210 return NotImplemented
211
Guido van Rossum65b6a802007-07-09 14:03:08 +0000212 # Q. What's the value of being able to sort UUIDs?
213 # A. Use them as keys in a B-Tree or similar mapping.
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000214
215 def __lt__(self, other):
216 if isinstance(other, UUID):
217 return self.int < other.int
218 return NotImplemented
219
220 def __gt__(self, other):
221 if isinstance(other, UUID):
222 return self.int > other.int
223 return NotImplemented
224
225 def __le__(self, other):
226 if isinstance(other, UUID):
227 return self.int <= other.int
228 return NotImplemented
229
230 def __ge__(self, other):
231 if isinstance(other, UUID):
232 return self.int >= other.int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000233 return NotImplemented
234
235 def __hash__(self):
236 return hash(self.int)
237
238 def __int__(self):
239 return self.int
240
241 def __repr__(self):
Serhiy Storchaka465e60e2014-07-25 23:36:00 +0300242 return '%s(%r)' % (self.__class__.__name__, str(self))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000243
244 def __setattr__(self, name, value):
245 raise TypeError('UUID objects are immutable')
246
247 def __str__(self):
248 hex = '%032x' % self.int
249 return '%s-%s-%s-%s-%s' % (
250 hex[:8], hex[8:12], hex[12:16], hex[16:20], hex[20:])
251
Guido van Rossum65b6a802007-07-09 14:03:08 +0000252 @property
253 def bytes(self):
Serhiy Storchakafa9be4f2014-09-06 22:14:04 +0300254 return self.int.to_bytes(16, 'big')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000255
Guido van Rossum65b6a802007-07-09 14:03:08 +0000256 @property
257 def bytes_le(self):
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000258 bytes = self.bytes
Serhiy Storchakafa9be4f2014-09-06 22:14:04 +0300259 return (bytes[4-1::-1] + bytes[6-1:4-1:-1] + bytes[8-1:6-1:-1] +
Guido van Rossum65b6a802007-07-09 14:03:08 +0000260 bytes[8:])
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000261
Guido van Rossum65b6a802007-07-09 14:03:08 +0000262 @property
263 def fields(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000264 return (self.time_low, self.time_mid, self.time_hi_version,
265 self.clock_seq_hi_variant, self.clock_seq_low, self.node)
266
Guido van Rossum65b6a802007-07-09 14:03:08 +0000267 @property
268 def time_low(self):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000269 return self.int >> 96
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000270
Guido van Rossum65b6a802007-07-09 14:03:08 +0000271 @property
272 def time_mid(self):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000273 return (self.int >> 80) & 0xffff
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000274
Guido van Rossum65b6a802007-07-09 14:03:08 +0000275 @property
276 def time_hi_version(self):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000277 return (self.int >> 64) & 0xffff
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000278
Guido van Rossum65b6a802007-07-09 14:03:08 +0000279 @property
280 def clock_seq_hi_variant(self):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000281 return (self.int >> 56) & 0xff
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000282
Guido van Rossum65b6a802007-07-09 14:03:08 +0000283 @property
284 def clock_seq_low(self):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000285 return (self.int >> 48) & 0xff
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000286
Guido van Rossum65b6a802007-07-09 14:03:08 +0000287 @property
288 def time(self):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000289 return (((self.time_hi_version & 0x0fff) << 48) |
290 (self.time_mid << 32) | self.time_low)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000291
Guido van Rossum65b6a802007-07-09 14:03:08 +0000292 @property
293 def clock_seq(self):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000294 return (((self.clock_seq_hi_variant & 0x3f) << 8) |
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000295 self.clock_seq_low)
296
Guido van Rossum65b6a802007-07-09 14:03:08 +0000297 @property
298 def node(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000299 return self.int & 0xffffffffffff
300
Guido van Rossum65b6a802007-07-09 14:03:08 +0000301 @property
302 def hex(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000303 return '%032x' % self.int
304
Guido van Rossum65b6a802007-07-09 14:03:08 +0000305 @property
306 def urn(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000307 return 'urn:uuid:' + str(self)
308
Guido van Rossum65b6a802007-07-09 14:03:08 +0000309 @property
310 def variant(self):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000311 if not self.int & (0x8000 << 48):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000312 return RESERVED_NCS
Guido van Rossume2a383d2007-01-15 16:59:06 +0000313 elif not self.int & (0x4000 << 48):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000314 return RFC_4122
Guido van Rossume2a383d2007-01-15 16:59:06 +0000315 elif not self.int & (0x2000 << 48):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000316 return RESERVED_MICROSOFT
317 else:
318 return RESERVED_FUTURE
319
Guido van Rossum65b6a802007-07-09 14:03:08 +0000320 @property
321 def version(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000322 # The version bits are only meaningful for RFC 4122 UUIDs.
323 if self.variant == RFC_4122:
Guido van Rossume2a383d2007-01-15 16:59:06 +0000324 return int((self.int >> 76) & 0xf)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000325
Serhiy Storchaka57b96772014-11-07 12:23:30 +0200326def _popen(command, *args):
Victor Stinnerb9d01992014-10-21 22:33:10 +0200327 import os, shutil, subprocess
R David Murray4be1e242013-12-17 21:13:16 -0500328 executable = shutil.which(command)
329 if executable is None:
330 path = os.pathsep.join(('/sbin', '/usr/sbin'))
331 executable = shutil.which(command, path=path)
332 if executable is None:
333 return None
Serhiy Storchaka57b96772014-11-07 12:23:30 +0200334 # LC_ALL=C to ensure English output, stderr=DEVNULL to prevent output
335 # on stderr (Note: we don't have an example where the words we search
336 # for are actually localized, but in theory some system could do so.)
337 env = dict(os.environ)
338 env['LC_ALL'] = 'C'
339 proc = subprocess.Popen((executable,) + args,
340 stdout=subprocess.PIPE,
341 stderr=subprocess.DEVNULL,
342 env=env)
343 return proc
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000344
Barry Warsaw23df2d12017-11-28 17:26:04 -0500345# For MAC (a.k.a. IEEE 802, or EUI-48) addresses, the second least significant
346# bit of the first octet signifies whether the MAC address is universally (0)
347# or locally (1) administered. Network cards from hardware manufacturers will
348# always be universally administered to guarantee global uniqueness of the MAC
349# address, but any particular machine may have other interfaces which are
350# locally administered. An example of the latter is the bridge interface to
351# the Touch Bar on MacBook Pros.
352#
353# This bit works out to be the 42nd bit counting from 1 being the least
354# significant, or 1<<41. We'll prefer universally administered MAC addresses
355# over locally administered ones since the former are globally unique, but
356# we'll return the first of the latter found if that's all the machine has.
357#
358# See https://en.wikipedia.org/wiki/MAC_address#Universal_vs._local
359
360def _is_universal(mac):
361 return not (mac & (1 << 41))
362
Serhiy Storchakae66bb962014-11-07 12:19:40 +0200363def _find_mac(command, args, hw_identifiers, get_index):
Barry Warsaw23df2d12017-11-28 17:26:04 -0500364 first_local_mac = None
R David Murray4be1e242013-12-17 21:13:16 -0500365 try:
Serhiy Storchaka57b96772014-11-07 12:23:30 +0200366 proc = _popen(command, *args.split())
367 if not proc:
Barry Warsaw23df2d12017-11-28 17:26:04 -0500368 return None
Victor Stinnerb9d01992014-10-21 22:33:10 +0200369 with proc:
370 for line in proc.stdout:
Serhiy Storchakae66bb962014-11-07 12:19:40 +0200371 words = line.lower().rstrip().split()
R David Murray4be1e242013-12-17 21:13:16 -0500372 for i in range(len(words)):
373 if words[i] in hw_identifiers:
374 try:
Serhiy Storchakae66bb962014-11-07 12:19:40 +0200375 word = words[get_index(i)]
Serhiy Storchaka57b96772014-11-07 12:23:30 +0200376 mac = int(word.replace(b':', b''), 16)
Barry Warsaw23df2d12017-11-28 17:26:04 -0500377 if _is_universal(mac):
Serhiy Storchakae66bb962014-11-07 12:19:40 +0200378 return mac
Barry Warsaw23df2d12017-11-28 17:26:04 -0500379 first_local_mac = first_local_mac or mac
R David Murray4be1e242013-12-17 21:13:16 -0500380 except (ValueError, IndexError):
381 # Virtual interfaces, such as those provided by
382 # VPNs, do not have a colon-delimited MAC address
383 # as expected, but a 16-byte HWAddr separated by
384 # dashes. These should be ignored in favor of a
385 # real MAC address
386 pass
R David Murray0ce3e9d2013-12-17 21:14:41 -0500387 except OSError:
R David Murray4be1e242013-12-17 21:13:16 -0500388 pass
Barry Warsaw23df2d12017-11-28 17:26:04 -0500389 return first_local_mac or None
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000390
391def _ifconfig_getnode():
392 """Get the hardware address on Unix by running ifconfig."""
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000393 # This works on Linux ('' or '-a'), Tru64 ('-av'), but not all Unixes.
Serhiy Storchakaee1a9a22017-11-04 09:37:32 +0200394 keywords = (b'hwaddr', b'ether', b'address:', b'lladdr')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000395 for args in ('', '-a', '-av'):
Serhiy Storchakaee1a9a22017-11-04 09:37:32 +0200396 mac = _find_mac('ifconfig', args, keywords, lambda i: i+1)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000397 if mac:
398 return mac
Barry Warsaw23df2d12017-11-28 17:26:04 -0500399 return None
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000400
Serhiy Storchakaac4aa7b2014-11-30 20:39:04 +0200401def _ip_getnode():
402 """Get the hardware address on Unix by running ip."""
403 # This works on Linux with iproute2.
xdegaye961dbe02017-12-07 12:59:13 +0100404 mac = _find_mac('ip', 'link', [b'link/ether'], lambda i: i+1)
Serhiy Storchakaac4aa7b2014-11-30 20:39:04 +0200405 if mac:
406 return mac
Barry Warsaw23df2d12017-11-28 17:26:04 -0500407 return None
Serhiy Storchakaac4aa7b2014-11-30 20:39:04 +0200408
Serhiy Storchakae66bb962014-11-07 12:19:40 +0200409def _arp_getnode():
410 """Get the hardware address on Unix by running arp."""
411 import os, socket
Serhiy Storchaka525d5ae2014-11-21 21:55:39 +0200412 try:
413 ip_addr = socket.gethostbyname(socket.gethostname())
414 except OSError:
415 return None
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000416
417 # Try getting the MAC addr from arp based on our IP address (Solaris).
Serhiy Storchakaee1a9a22017-11-04 09:37:32 +0200418 mac = _find_mac('arp', '-an', [os.fsencode(ip_addr)], lambda i: -1)
419 if mac:
420 return mac
421
422 # This works on OpenBSD
423 mac = _find_mac('arp', '-an', [os.fsencode(ip_addr)], lambda i: i+1)
424 if mac:
425 return mac
426
427 # This works on Linux, FreeBSD and NetBSD
428 mac = _find_mac('arp', '-an', [os.fsencode('(%s)' % ip_addr)],
429 lambda i: i+2)
Barry Warsaw23df2d12017-11-28 17:26:04 -0500430 # Return None instead of 0.
Serhiy Storchakaee1a9a22017-11-04 09:37:32 +0200431 if mac:
432 return mac
Barry Warsaw23df2d12017-11-28 17:26:04 -0500433 return None
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000434
Serhiy Storchakae66bb962014-11-07 12:19:40 +0200435def _lanscan_getnode():
436 """Get the hardware address on Unix by running lanscan."""
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000437 # This might work on HP-UX.
Serhiy Storchaka57b96772014-11-07 12:23:30 +0200438 return _find_mac('lanscan', '-ai', [b'lan0'], lambda i: 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000439
Serhiy Storchakae66bb962014-11-07 12:19:40 +0200440def _netstat_getnode():
441 """Get the hardware address on Unix by running netstat."""
Benjamin Peterson06930632017-09-04 16:36:05 -0700442 # This might work on AIX, Tru64 UNIX.
Barry Warsaw23df2d12017-11-28 17:26:04 -0500443 first_local_mac = None
Serhiy Storchakae66bb962014-11-07 12:19:40 +0200444 try:
Serhiy Storchaka57b96772014-11-07 12:23:30 +0200445 proc = _popen('netstat', '-ia')
446 if not proc:
Barry Warsaw23df2d12017-11-28 17:26:04 -0500447 return None
Serhiy Storchaka57b96772014-11-07 12:23:30 +0200448 with proc:
449 words = proc.stdout.readline().rstrip().split()
Serhiy Storchakae66bb962014-11-07 12:19:40 +0200450 try:
Serhiy Storchaka57b96772014-11-07 12:23:30 +0200451 i = words.index(b'Address')
Serhiy Storchakae66bb962014-11-07 12:19:40 +0200452 except ValueError:
Barry Warsaw23df2d12017-11-28 17:26:04 -0500453 return None
Serhiy Storchaka57b96772014-11-07 12:23:30 +0200454 for line in proc.stdout:
Serhiy Storchakae66bb962014-11-07 12:19:40 +0200455 try:
456 words = line.rstrip().split()
457 word = words[i]
Serhiy Storchaka57b96772014-11-07 12:23:30 +0200458 if len(word) == 17 and word.count(b':') == 5:
459 mac = int(word.replace(b':', b''), 16)
Barry Warsaw23df2d12017-11-28 17:26:04 -0500460 if _is_universal(mac):
Serhiy Storchakae66bb962014-11-07 12:19:40 +0200461 return mac
Barry Warsaw23df2d12017-11-28 17:26:04 -0500462 first_local_mac = first_local_mac or mac
Serhiy Storchakae66bb962014-11-07 12:19:40 +0200463 except (ValueError, IndexError):
464 pass
465 except OSError:
466 pass
Barry Warsaw23df2d12017-11-28 17:26:04 -0500467 return first_local_mac or None
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000468
469def _ipconfig_getnode():
470 """Get the hardware address on Windows by running ipconfig.exe."""
Segev Finerda6c3da2018-02-13 08:29:54 +0200471 import os, re, subprocess
Barry Warsaw23df2d12017-11-28 17:26:04 -0500472 first_local_mac = None
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000473 dirs = ['', r'c:\windows\system32', r'c:\winnt\system32']
474 try:
475 import ctypes
476 buffer = ctypes.create_string_buffer(300)
477 ctypes.windll.kernel32.GetSystemDirectoryA(buffer, 300)
478 dirs.insert(0, buffer.value.decode('mbcs'))
479 except:
480 pass
481 for dir in dirs:
482 try:
Segev Finerda6c3da2018-02-13 08:29:54 +0200483 proc = subprocess.Popen([os.path.join(dir, 'ipconfig'), '/all'],
484 stdout=subprocess.PIPE,
485 encoding="oem")
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200486 except OSError:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000487 continue
Segev Finerda6c3da2018-02-13 08:29:54 +0200488 with proc:
489 for line in proc.stdout:
Brian Curtin69cd87b2010-11-05 14:48:35 +0000490 value = line.split(':')[-1].strip().lower()
491 if re.match('([0-9a-f][0-9a-f]-){5}[0-9a-f][0-9a-f]', value):
Barry Warsaw23df2d12017-11-28 17:26:04 -0500492 mac = int(value.replace('-', ''), 16)
493 if _is_universal(mac):
494 return mac
495 first_local_mac = first_local_mac or mac
496 return first_local_mac or None
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000497
498def _netbios_getnode():
499 """Get the hardware address on Windows using NetBIOS calls.
500 See http://support.microsoft.com/kb/118623 for details."""
501 import win32wnet, netbios
Barry Warsaw23df2d12017-11-28 17:26:04 -0500502 first_local_mac = None
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000503 ncb = netbios.NCB()
504 ncb.Command = netbios.NCBENUM
505 ncb.Buffer = adapters = netbios.LANA_ENUM()
506 adapters._pack()
507 if win32wnet.Netbios(ncb) != 0:
Barry Warsaw23df2d12017-11-28 17:26:04 -0500508 return None
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000509 adapters._unpack()
510 for i in range(adapters.length):
511 ncb.Reset()
512 ncb.Command = netbios.NCBRESET
513 ncb.Lana_num = ord(adapters.lana[i])
514 if win32wnet.Netbios(ncb) != 0:
515 continue
516 ncb.Reset()
517 ncb.Command = netbios.NCBASTAT
518 ncb.Lana_num = ord(adapters.lana[i])
519 ncb.Callname = '*'.ljust(16)
520 ncb.Buffer = status = netbios.ADAPTER_STATUS()
521 if win32wnet.Netbios(ncb) != 0:
522 continue
523 status._unpack()
Serhiy Storchakafa9be4f2014-09-06 22:14:04 +0300524 bytes = status.adapter_address[:6]
525 if len(bytes) != 6:
526 continue
Barry Warsaw23df2d12017-11-28 17:26:04 -0500527 mac = int.from_bytes(bytes, 'big')
528 if _is_universal(mac):
529 return mac
530 first_local_mac = first_local_mac or mac
531 return first_local_mac or None
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000532
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000533
Antoine Pitroua106aec2017-09-28 23:03:06 +0200534_generate_time_safe = _UuidCreate = None
535_has_uuid_generate_time_safe = None
536
537# Import optional C extension at toplevel, to help disabling it when testing
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000538try:
Antoine Pitroua106aec2017-09-28 23:03:06 +0200539 import _uuid
540except ImportError:
541 _uuid = None
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000542
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000543
Antoine Pitroua106aec2017-09-28 23:03:06 +0200544def _load_system_functions():
545 """
546 Try to load platform-specific functions for generating uuids.
547 """
548 global _generate_time_safe, _UuidCreate, _has_uuid_generate_time_safe
Ronald Oussorenac764d32010-05-05 15:32:33 +0000549
Antoine Pitroua106aec2017-09-28 23:03:06 +0200550 if _has_uuid_generate_time_safe is not None:
551 return
552
553 _has_uuid_generate_time_safe = False
554
555 if sys.platform == "darwin" and int(os.uname().release.split('.')[0]) < 9:
556 # The uuid_generate_* functions are broken on MacOS X 10.5, as noted
557 # in issue #8621 the function generates the same sequence of values
558 # in the parent process and all children created using fork (unless
559 # those children use exec as well).
560 #
561 # Assume that the uuid_generate functions are broken from 10.5 onward,
562 # the test can be adjusted when a later version is fixed.
563 pass
564 elif _uuid is not None:
565 _generate_time_safe = _uuid.generate_time_safe
Victor Stinner4337a0d2017-10-02 07:57:59 -0700566 _has_uuid_generate_time_safe = _uuid.has_uuid_generate_time_safe
Antoine Pitroua106aec2017-09-28 23:03:06 +0200567 return
568
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000569 try:
Antoine Pitroua106aec2017-09-28 23:03:06 +0200570 # If we couldn't find an extension module, try ctypes to find
571 # system routines for UUID generation.
572 # Thanks to Thomas Heller for ctypes and for his help with its use here.
573 import ctypes
574 import ctypes.util
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000575
Antoine Pitroua106aec2017-09-28 23:03:06 +0200576 # The uuid_generate_* routines are provided by libuuid on at least
577 # Linux and FreeBSD, and provided by libc on Mac OS X.
578 _libnames = ['uuid']
579 if not sys.platform.startswith('win'):
580 _libnames.append('c')
581 for libname in _libnames:
582 try:
583 lib = ctypes.CDLL(ctypes.util.find_library(libname))
584 except Exception: # pragma: nocover
585 continue
586 # Try to find the safe variety first.
587 if hasattr(lib, 'uuid_generate_time_safe'):
588 _uuid_generate_time_safe = lib.uuid_generate_time_safe
589 # int uuid_generate_time_safe(uuid_t out);
590 def _generate_time_safe():
591 _buffer = ctypes.create_string_buffer(16)
592 res = _uuid_generate_time_safe(_buffer)
593 return bytes(_buffer.raw), res
594 _has_uuid_generate_time_safe = True
595 break
596
597 elif hasattr(lib, 'uuid_generate_time'): # pragma: nocover
598 _uuid_generate_time = lib.uuid_generate_time
599 # void uuid_generate_time(uuid_t out);
600 _uuid_generate_time.restype = None
601 def _generate_time_safe():
602 _buffer = ctypes.create_string_buffer(16)
603 _uuid_generate_time(_buffer)
604 return bytes(_buffer.raw), None
605 break
606
607 # On Windows prior to 2000, UuidCreate gives a UUID containing the
608 # hardware address. On Windows 2000 and later, UuidCreate makes a
609 # random UUID and UuidCreateSequential gives a UUID containing the
610 # hardware address. These routines are provided by the RPC runtime.
611 # NOTE: at least on Tim's WinXP Pro SP2 desktop box, while the last
612 # 6 bytes returned by UuidCreateSequential are fixed, they don't appear
613 # to bear any relationship to the MAC address of any network device
614 # on the box.
615 try:
616 lib = ctypes.windll.rpcrt4
617 except:
618 lib = None
619 _UuidCreate = getattr(lib, 'UuidCreateSequential',
620 getattr(lib, 'UuidCreate', None))
621
622 except Exception as exc:
623 import warnings
624 warnings.warn(f"Could not find fallback ctypes uuid functions: {exc}",
625 ImportWarning)
626
627
628def _unix_getnode():
629 """Get the hardware address on Unix using the _uuid extension module
630 or ctypes."""
631 _load_system_functions()
632 uuid_time, _ = _generate_time_safe()
633 return UUID(bytes=uuid_time).node
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000634
635def _windll_getnode():
636 """Get the hardware address on Windows using ctypes."""
Antoine Pitroua106aec2017-09-28 23:03:06 +0200637 import ctypes
638 _load_system_functions()
Guido van Rossum37410aa2007-08-24 04:13:42 +0000639 _buffer = ctypes.create_string_buffer(16)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000640 if _UuidCreate(_buffer) == 0:
Guido van Rossumfb56d8f2007-07-20 17:45:09 +0000641 return UUID(bytes=bytes_(_buffer.raw)).node
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000642
643def _random_getnode():
Barry Warsaw23df2d12017-11-28 17:26:04 -0500644 """Get a random node ID."""
645 # RFC 4122, $4.1.6 says "For systems with no IEEE address, a randomly or
646 # pseudo-randomly generated value may be used; see Section 4.5. The
647 # multicast bit must be set in such addresses, in order that they will
648 # never conflict with addresses obtained from network cards."
649 #
650 # The "multicast bit" of a MAC address is defined to be "the least
651 # significant bit of the first octet". This works out to be the 41st bit
652 # counting from 1 being the least significant bit, or 1<<40.
653 #
654 # See https://en.wikipedia.org/wiki/MAC_address#Unicast_vs._multicast
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000655 import random
Barry Warsaw23df2d12017-11-28 17:26:04 -0500656 return random.getrandbits(48) | (1 << 40)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000657
Antoine Pitroua106aec2017-09-28 23:03:06 +0200658
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000659_node = None
660
Bo Bayles6b273f72018-01-23 19:11:44 -0600661_NODE_GETTERS_WIN32 = [_windll_getnode, _netbios_getnode, _ipconfig_getnode]
662
663_NODE_GETTERS_UNIX = [_unix_getnode, _ifconfig_getnode, _ip_getnode,
664 _arp_getnode, _lanscan_getnode, _netstat_getnode]
665
666def getnode(*, getters=None):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000667 """Get the hardware address as a 48-bit positive integer.
668
669 The first time this runs, it may launch a separate program, which could
670 be quite slow. If all attempts to obtain the hardware address fail, we
671 choose a random 48-bit number with its eighth bit set to 1 as recommended
672 in RFC 4122.
673 """
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000674 global _node
675 if _node is not None:
676 return _node
677
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000678 if sys.platform == 'win32':
Bo Bayles6b273f72018-01-23 19:11:44 -0600679 getters = _NODE_GETTERS_WIN32
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000680 else:
Bo Bayles6b273f72018-01-23 19:11:44 -0600681 getters = _NODE_GETTERS_UNIX
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000682
Serhiy Storchakae69fbb62017-12-04 11:51:55 +0200683 for getter in getters + [_random_getnode]:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000684 try:
685 _node = getter()
686 except:
687 continue
Bo Bayles6b273f72018-01-23 19:11:44 -0600688 if (_node is not None) and (0 <= _node < (1 << 48)):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000689 return _node
Bo Bayles6b273f72018-01-23 19:11:44 -0600690 assert False, '_random_getnode() returned invalid value: {}'.format(_node)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000691
Antoine Pitroua106aec2017-09-28 23:03:06 +0200692
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000693_last_timestamp = None
694
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000695def uuid1(node=None, clock_seq=None):
696 """Generate a UUID from a host ID, sequence number, and the current time.
697 If 'node' is not given, getnode() is used to obtain the hardware
698 address. If 'clock_seq' is given, it is used as the sequence number;
699 otherwise a random 14-bit sequence number is chosen."""
700
701 # When the system provides a version-1 UUID generator, use it (but don't
702 # use UuidCreate here because its UUIDs don't conform to RFC 4122).
Antoine Pitroua106aec2017-09-28 23:03:06 +0200703 _load_system_functions()
704 if _generate_time_safe is not None and node is clock_seq is None:
705 uuid_time, safely_generated = _generate_time_safe()
Barry Warsaw8c130d72017-02-18 15:45:49 -0500706 try:
707 is_safe = SafeUUID(safely_generated)
708 except ValueError:
709 is_safe = SafeUUID.unknown
Antoine Pitroua106aec2017-09-28 23:03:06 +0200710 return UUID(bytes=uuid_time, is_safe=is_safe)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000711
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000712 global _last_timestamp
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000713 import time
714 nanoseconds = int(time.time() * 1e9)
715 # 0x01b21dd213814000 is the number of 100-ns intervals between the
716 # UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
Guido van Rossume2a383d2007-01-15 16:59:06 +0000717 timestamp = int(nanoseconds/100) + 0x01b21dd213814000
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000718 if _last_timestamp is not None and timestamp <= _last_timestamp:
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000719 timestamp = _last_timestamp + 1
720 _last_timestamp = timestamp
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000721 if clock_seq is None:
722 import random
Serhiy Storchakafa9be4f2014-09-06 22:14:04 +0300723 clock_seq = random.getrandbits(14) # instead of stable storage
Guido van Rossume2a383d2007-01-15 16:59:06 +0000724 time_low = timestamp & 0xffffffff
725 time_mid = (timestamp >> 32) & 0xffff
726 time_hi_version = (timestamp >> 48) & 0x0fff
727 clock_seq_low = clock_seq & 0xff
728 clock_seq_hi_variant = (clock_seq >> 8) & 0x3f
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000729 if node is None:
730 node = getnode()
731 return UUID(fields=(time_low, time_mid, time_hi_version,
732 clock_seq_hi_variant, clock_seq_low, node), version=1)
733
734def uuid3(namespace, name):
735 """Generate a UUID from the MD5 hash of a namespace UUID and a name."""
Guido van Rossume7ba4952007-06-06 23:52:48 +0000736 from hashlib import md5
Guido van Rossum65b6a802007-07-09 14:03:08 +0000737 hash = md5(namespace.bytes + bytes(name, "utf-8")).digest()
Guido van Rossum5ed033b2007-07-09 14:29:40 +0000738 return UUID(bytes=hash[:16], version=3)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000739
740def uuid4():
741 """Generate a random UUID."""
Benjamin Peterson788cb522015-10-29 20:38:04 -0700742 return UUID(bytes=os.urandom(16), version=4)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000743
744def uuid5(namespace, name):
745 """Generate a UUID from the SHA-1 hash of a namespace UUID and a name."""
Guido van Rossume7ba4952007-06-06 23:52:48 +0000746 from hashlib import sha1
Guido van Rossum65b6a802007-07-09 14:03:08 +0000747 hash = sha1(namespace.bytes + bytes(name, "utf-8")).digest()
Guido van Rossum5ed033b2007-07-09 14:29:40 +0000748 return UUID(bytes=hash[:16], version=5)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000749
750# The following standard UUIDs are for use with uuid3() or uuid5().
751
752NAMESPACE_DNS = UUID('6ba7b810-9dad-11d1-80b4-00c04fd430c8')
753NAMESPACE_URL = UUID('6ba7b811-9dad-11d1-80b4-00c04fd430c8')
754NAMESPACE_OID = UUID('6ba7b812-9dad-11d1-80b4-00c04fd430c8')
755NAMESPACE_X500 = UUID('6ba7b814-9dad-11d1-80b4-00c04fd430c8')