uuid.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. r"""UUID objects (universally unique identifiers) according to RFC 4122.
  2. This module provides immutable UUID objects (class UUID) and the functions
  3. uuid1(), uuid3(), uuid4(), uuid5() for generating version 1, 3, 4, and 5
  4. UUIDs as specified in RFC 4122.
  5. If all you want is a unique ID, you should probably call uuid1() or uuid4().
  6. Note that uuid1() may compromise privacy since it creates a UUID containing
  7. the computer's network address. uuid4() creates a random UUID.
  8. Typical usage:
  9. >>> import uuid
  10. # make a UUID based on the host ID and current time
  11. >>> uuid.uuid1() # doctest: +SKIP
  12. UUID('a8098c1a-f86e-11da-bd1a-00112444be1e')
  13. # make a UUID using an MD5 hash of a namespace UUID and a name
  14. >>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
  15. UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e')
  16. # make a random UUID
  17. >>> uuid.uuid4() # doctest: +SKIP
  18. UUID('16fd2706-8baf-433b-82eb-8c7fada847da')
  19. # make a UUID using a SHA-1 hash of a namespace UUID and a name
  20. >>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
  21. UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d')
  22. # make a UUID from a string of hex digits (braces and hyphens ignored)
  23. >>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}')
  24. # convert a UUID to a string of hex digits in standard form
  25. >>> str(x)
  26. '00010203-0405-0607-0809-0a0b0c0d0e0f'
  27. # get the raw 16 bytes of the UUID
  28. >>> x.bytes
  29. b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f'
  30. # make a UUID from a 16-byte string
  31. >>> uuid.UUID(bytes=x.bytes)
  32. UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')
  33. """
  34. import os
  35. import sys
  36. from enum import Enum, _simple_enum
  37. __author__ = 'Ka-Ping Yee <ping@zesty.ca>'
  38. # The recognized platforms - known behaviors
  39. if sys.platform in ('win32', 'darwin', 'emscripten', 'wasi'):
  40. _AIX = _LINUX = False
  41. else:
  42. import platform
  43. _platform_system = platform.system()
  44. _AIX = _platform_system == 'AIX'
  45. _LINUX = _platform_system == 'Linux'
  46. _MAC_DELIM = b':'
  47. _MAC_OMITS_LEADING_ZEROES = False
  48. if _AIX:
  49. _MAC_DELIM = b'.'
  50. _MAC_OMITS_LEADING_ZEROES = True
  51. RESERVED_NCS, RFC_4122, RESERVED_MICROSOFT, RESERVED_FUTURE = [
  52. 'reserved for NCS compatibility', 'specified in RFC 4122',
  53. 'reserved for Microsoft compatibility', 'reserved for future definition']
  54. int_ = int # The built-in int type
  55. bytes_ = bytes # The built-in bytes type
  56. @_simple_enum(Enum)
  57. class SafeUUID:
  58. safe = 0
  59. unsafe = -1
  60. unknown = None
  61. class UUID:
  62. """Instances of the UUID class represent UUIDs as specified in RFC 4122.
  63. UUID objects are immutable, hashable, and usable as dictionary keys.
  64. Converting a UUID to a string with str() yields something in the form
  65. '12345678-1234-1234-1234-123456789abc'. The UUID constructor accepts
  66. five possible forms: a similar string of hexadecimal digits, or a tuple
  67. of six integer fields (with 32-bit, 16-bit, 16-bit, 8-bit, 8-bit, and
  68. 48-bit values respectively) as an argument named 'fields', or a string
  69. of 16 bytes (with all the integer fields in big-endian order) as an
  70. argument named 'bytes', or a string of 16 bytes (with the first three
  71. fields in little-endian order) as an argument named 'bytes_le', or a
  72. single 128-bit integer as an argument named 'int'.
  73. UUIDs have these read-only attributes:
  74. bytes the UUID as a 16-byte string (containing the six
  75. integer fields in big-endian byte order)
  76. bytes_le the UUID as a 16-byte string (with time_low, time_mid,
  77. and time_hi_version in little-endian byte order)
  78. fields a tuple of the six integer fields of the UUID,
  79. which are also available as six individual attributes
  80. and two derived attributes:
  81. time_low the first 32 bits of the UUID
  82. time_mid the next 16 bits of the UUID
  83. time_hi_version the next 16 bits of the UUID
  84. clock_seq_hi_variant the next 8 bits of the UUID
  85. clock_seq_low the next 8 bits of the UUID
  86. node the last 48 bits of the UUID
  87. time the 60-bit timestamp
  88. clock_seq the 14-bit sequence number
  89. hex the UUID as a 32-character hexadecimal string
  90. int the UUID as a 128-bit integer
  91. urn the UUID as a URN as specified in RFC 4122
  92. variant the UUID variant (one of the constants RESERVED_NCS,
  93. RFC_4122, RESERVED_MICROSOFT, or RESERVED_FUTURE)
  94. version the UUID version number (1 through 5, meaningful only
  95. when the variant is RFC_4122)
  96. is_safe An enum indicating whether the UUID has been generated in
  97. a way that is safe for multiprocessing applications, via
  98. uuid_generate_time_safe(3).
  99. """
  100. __slots__ = ('int', 'is_safe', '__weakref__')
  101. def __init__(self, hex=None, bytes=None, bytes_le=None, fields=None,
  102. int=None, version=None,
  103. *, is_safe=SafeUUID.unknown):
  104. r"""Create a UUID from either a string of 32 hexadecimal digits,
  105. a string of 16 bytes as the 'bytes' argument, a string of 16 bytes
  106. in little-endian order as the 'bytes_le' argument, a tuple of six
  107. integers (32-bit time_low, 16-bit time_mid, 16-bit time_hi_version,
  108. 8-bit clock_seq_hi_variant, 8-bit clock_seq_low, 48-bit node) as
  109. the 'fields' argument, or a single 128-bit integer as the 'int'
  110. argument. When a string of hex digits is given, curly braces,
  111. hyphens, and a URN prefix are all optional. For example, these
  112. expressions all yield the same UUID:
  113. UUID('{12345678-1234-5678-1234-567812345678}')
  114. UUID('12345678123456781234567812345678')
  115. UUID('urn:uuid:12345678-1234-5678-1234-567812345678')
  116. UUID(bytes='\x12\x34\x56\x78'*4)
  117. UUID(bytes_le='\x78\x56\x34\x12\x34\x12\x78\x56' +
  118. '\x12\x34\x56\x78\x12\x34\x56\x78')
  119. UUID(fields=(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678))
  120. UUID(int=0x12345678123456781234567812345678)
  121. Exactly one of 'hex', 'bytes', 'bytes_le', 'fields', or 'int' must
  122. be given. The 'version' argument is optional; if given, the resulting
  123. UUID will have its variant and version set according to RFC 4122,
  124. overriding the given 'hex', 'bytes', 'bytes_le', 'fields', or 'int'.
  125. is_safe is an enum exposed as an attribute on the instance. It
  126. indicates whether the UUID has been generated in a way that is safe
  127. for multiprocessing applications, via uuid_generate_time_safe(3).
  128. """
  129. if [hex, bytes, bytes_le, fields, int].count(None) != 4:
  130. raise TypeError('one of the hex, bytes, bytes_le, fields, '
  131. 'or int arguments must be given')
  132. if hex is not None:
  133. hex = hex.replace('urn:', '').replace('uuid:', '')
  134. hex = hex.strip('{}').replace('-', '')
  135. if len(hex) != 32:
  136. raise ValueError('badly formed hexadecimal UUID string')
  137. int = int_(hex, 16)
  138. if bytes_le is not None:
  139. if len(bytes_le) != 16:
  140. raise ValueError('bytes_le is not a 16-char string')
  141. bytes = (bytes_le[4-1::-1] + bytes_le[6-1:4-1:-1] +
  142. bytes_le[8-1:6-1:-1] + bytes_le[8:])
  143. if bytes is not None:
  144. if len(bytes) != 16:
  145. raise ValueError('bytes is not a 16-char string')
  146. assert isinstance(bytes, bytes_), repr(bytes)
  147. int = int_.from_bytes(bytes) # big endian
  148. if fields is not None:
  149. if len(fields) != 6:
  150. raise ValueError('fields is not a 6-tuple')
  151. (time_low, time_mid, time_hi_version,
  152. clock_seq_hi_variant, clock_seq_low, node) = fields
  153. if not 0 <= time_low < 1<<32:
  154. raise ValueError('field 1 out of range (need a 32-bit value)')
  155. if not 0 <= time_mid < 1<<16:
  156. raise ValueError('field 2 out of range (need a 16-bit value)')
  157. if not 0 <= time_hi_version < 1<<16:
  158. raise ValueError('field 3 out of range (need a 16-bit value)')
  159. if not 0 <= clock_seq_hi_variant < 1<<8:
  160. raise ValueError('field 4 out of range (need an 8-bit value)')
  161. if not 0 <= clock_seq_low < 1<<8:
  162. raise ValueError('field 5 out of range (need an 8-bit value)')
  163. if not 0 <= node < 1<<48:
  164. raise ValueError('field 6 out of range (need a 48-bit value)')
  165. clock_seq = (clock_seq_hi_variant << 8) | clock_seq_low
  166. int = ((time_low << 96) | (time_mid << 80) |
  167. (time_hi_version << 64) | (clock_seq << 48) | node)
  168. if int is not None:
  169. if not 0 <= int < 1<<128:
  170. raise ValueError('int is out of range (need a 128-bit value)')
  171. if version is not None:
  172. if not 1 <= version <= 5:
  173. raise ValueError('illegal version number')
  174. # Set the variant to RFC 4122.
  175. int &= ~(0xc000 << 48)
  176. int |= 0x8000 << 48
  177. # Set the version number.
  178. int &= ~(0xf000 << 64)
  179. int |= version << 76
  180. object.__setattr__(self, 'int', int)
  181. object.__setattr__(self, 'is_safe', is_safe)
  182. def __getstate__(self):
  183. d = {'int': self.int}
  184. if self.is_safe != SafeUUID.unknown:
  185. # is_safe is a SafeUUID instance. Return just its value, so that
  186. # it can be un-pickled in older Python versions without SafeUUID.
  187. d['is_safe'] = self.is_safe.value
  188. return d
  189. def __setstate__(self, state):
  190. object.__setattr__(self, 'int', state['int'])
  191. # is_safe was added in 3.7; it is also omitted when it is "unknown"
  192. object.__setattr__(self, 'is_safe',
  193. SafeUUID(state['is_safe'])
  194. if 'is_safe' in state else SafeUUID.unknown)
  195. def __eq__(self, other):
  196. if isinstance(other, UUID):
  197. return self.int == other.int
  198. return NotImplemented
  199. # Q. What's the value of being able to sort UUIDs?
  200. # A. Use them as keys in a B-Tree or similar mapping.
  201. def __lt__(self, other):
  202. if isinstance(other, UUID):
  203. return self.int < other.int
  204. return NotImplemented
  205. def __gt__(self, other):
  206. if isinstance(other, UUID):
  207. return self.int > other.int
  208. return NotImplemented
  209. def __le__(self, other):
  210. if isinstance(other, UUID):
  211. return self.int <= other.int
  212. return NotImplemented
  213. def __ge__(self, other):
  214. if isinstance(other, UUID):
  215. return self.int >= other.int
  216. return NotImplemented
  217. def __hash__(self):
  218. return hash(self.int)
  219. def __int__(self):
  220. return self.int
  221. def __repr__(self):
  222. return '%s(%r)' % (self.__class__.__name__, str(self))
  223. def __setattr__(self, name, value):
  224. raise TypeError('UUID objects are immutable')
  225. def __str__(self):
  226. hex = '%032x' % self.int
  227. return '%s-%s-%s-%s-%s' % (
  228. hex[:8], hex[8:12], hex[12:16], hex[16:20], hex[20:])
  229. @property
  230. def bytes(self):
  231. return self.int.to_bytes(16) # big endian
  232. @property
  233. def bytes_le(self):
  234. bytes = self.bytes
  235. return (bytes[4-1::-1] + bytes[6-1:4-1:-1] + bytes[8-1:6-1:-1] +
  236. bytes[8:])
  237. @property
  238. def fields(self):
  239. return (self.time_low, self.time_mid, self.time_hi_version,
  240. self.clock_seq_hi_variant, self.clock_seq_low, self.node)
  241. @property
  242. def time_low(self):
  243. return self.int >> 96
  244. @property
  245. def time_mid(self):
  246. return (self.int >> 80) & 0xffff
  247. @property
  248. def time_hi_version(self):
  249. return (self.int >> 64) & 0xffff
  250. @property
  251. def clock_seq_hi_variant(self):
  252. return (self.int >> 56) & 0xff
  253. @property
  254. def clock_seq_low(self):
  255. return (self.int >> 48) & 0xff
  256. @property
  257. def time(self):
  258. return (((self.time_hi_version & 0x0fff) << 48) |
  259. (self.time_mid << 32) | self.time_low)
  260. @property
  261. def clock_seq(self):
  262. return (((self.clock_seq_hi_variant & 0x3f) << 8) |
  263. self.clock_seq_low)
  264. @property
  265. def node(self):
  266. return self.int & 0xffffffffffff
  267. @property
  268. def hex(self):
  269. return '%032x' % self.int
  270. @property
  271. def urn(self):
  272. return 'urn:uuid:' + str(self)
  273. @property
  274. def variant(self):
  275. if not self.int & (0x8000 << 48):
  276. return RESERVED_NCS
  277. elif not self.int & (0x4000 << 48):
  278. return RFC_4122
  279. elif not self.int & (0x2000 << 48):
  280. return RESERVED_MICROSOFT
  281. else:
  282. return RESERVED_FUTURE
  283. @property
  284. def version(self):
  285. # The version bits are only meaningful for RFC 4122 UUIDs.
  286. if self.variant == RFC_4122:
  287. return int((self.int >> 76) & 0xf)
  288. def _get_command_stdout(command, *args):
  289. import io, os, shutil, subprocess
  290. try:
  291. path_dirs = os.environ.get('PATH', os.defpath).split(os.pathsep)
  292. path_dirs.extend(['/sbin', '/usr/sbin'])
  293. executable = shutil.which(command, path=os.pathsep.join(path_dirs))
  294. if executable is None:
  295. return None
  296. # LC_ALL=C to ensure English output, stderr=DEVNULL to prevent output
  297. # on stderr (Note: we don't have an example where the words we search
  298. # for are actually localized, but in theory some system could do so.)
  299. env = dict(os.environ)
  300. env['LC_ALL'] = 'C'
  301. # Empty strings will be quoted by popen so we should just ommit it
  302. if args != ('',):
  303. command = (executable, *args)
  304. else:
  305. command = (executable,)
  306. proc = subprocess.Popen(command,
  307. stdout=subprocess.PIPE,
  308. stderr=subprocess.DEVNULL,
  309. env=env)
  310. if not proc:
  311. return None
  312. stdout, stderr = proc.communicate()
  313. return io.BytesIO(stdout)
  314. except (OSError, subprocess.SubprocessError):
  315. return None
  316. # For MAC (a.k.a. IEEE 802, or EUI-48) addresses, the second least significant
  317. # bit of the first octet signifies whether the MAC address is universally (0)
  318. # or locally (1) administered. Network cards from hardware manufacturers will
  319. # always be universally administered to guarantee global uniqueness of the MAC
  320. # address, but any particular machine may have other interfaces which are
  321. # locally administered. An example of the latter is the bridge interface to
  322. # the Touch Bar on MacBook Pros.
  323. #
  324. # This bit works out to be the 42nd bit counting from 1 being the least
  325. # significant, or 1<<41. We'll prefer universally administered MAC addresses
  326. # over locally administered ones since the former are globally unique, but
  327. # we'll return the first of the latter found if that's all the machine has.
  328. #
  329. # See https://en.wikipedia.org/wiki/MAC_address#Universal_vs._local_(U/L_bit)
  330. def _is_universal(mac):
  331. return not (mac & (1 << 41))
  332. def _find_mac_near_keyword(command, args, keywords, get_word_index):
  333. """Searches a command's output for a MAC address near a keyword.
  334. Each line of words in the output is case-insensitively searched for
  335. any of the given keywords. Upon a match, get_word_index is invoked
  336. to pick a word from the line, given the index of the match. For
  337. example, lambda i: 0 would get the first word on the line, while
  338. lambda i: i - 1 would get the word preceding the keyword.
  339. """
  340. stdout = _get_command_stdout(command, args)
  341. if stdout is None:
  342. return None
  343. first_local_mac = None
  344. for line in stdout:
  345. words = line.lower().rstrip().split()
  346. for i in range(len(words)):
  347. if words[i] in keywords:
  348. try:
  349. word = words[get_word_index(i)]
  350. mac = int(word.replace(_MAC_DELIM, b''), 16)
  351. except (ValueError, IndexError):
  352. # Virtual interfaces, such as those provided by
  353. # VPNs, do not have a colon-delimited MAC address
  354. # as expected, but a 16-byte HWAddr separated by
  355. # dashes. These should be ignored in favor of a
  356. # real MAC address
  357. pass
  358. else:
  359. if _is_universal(mac):
  360. return mac
  361. first_local_mac = first_local_mac or mac
  362. return first_local_mac or None
  363. def _parse_mac(word):
  364. # Accept 'HH:HH:HH:HH:HH:HH' MAC address (ex: '52:54:00:9d:0e:67'),
  365. # but reject IPv6 address (ex: 'fe80::5054:ff:fe9' or '123:2:3:4:5:6:7:8').
  366. #
  367. # Virtual interfaces, such as those provided by VPNs, do not have a
  368. # colon-delimited MAC address as expected, but a 16-byte HWAddr separated
  369. # by dashes. These should be ignored in favor of a real MAC address
  370. parts = word.split(_MAC_DELIM)
  371. if len(parts) != 6:
  372. return
  373. if _MAC_OMITS_LEADING_ZEROES:
  374. # (Only) on AIX the macaddr value given is not prefixed by 0, e.g.
  375. # en0 1500 link#2 fa.bc.de.f7.62.4 110854824 0 160133733 0 0
  376. # not
  377. # en0 1500 link#2 fa.bc.de.f7.62.04 110854824 0 160133733 0 0
  378. if not all(1 <= len(part) <= 2 for part in parts):
  379. return
  380. hexstr = b''.join(part.rjust(2, b'0') for part in parts)
  381. else:
  382. if not all(len(part) == 2 for part in parts):
  383. return
  384. hexstr = b''.join(parts)
  385. try:
  386. return int(hexstr, 16)
  387. except ValueError:
  388. return
  389. def _find_mac_under_heading(command, args, heading):
  390. """Looks for a MAC address under a heading in a command's output.
  391. The first line of words in the output is searched for the given
  392. heading. Words at the same word index as the heading in subsequent
  393. lines are then examined to see if they look like MAC addresses.
  394. """
  395. stdout = _get_command_stdout(command, args)
  396. if stdout is None:
  397. return None
  398. keywords = stdout.readline().rstrip().split()
  399. try:
  400. column_index = keywords.index(heading)
  401. except ValueError:
  402. return None
  403. first_local_mac = None
  404. for line in stdout:
  405. words = line.rstrip().split()
  406. try:
  407. word = words[column_index]
  408. except IndexError:
  409. continue
  410. mac = _parse_mac(word)
  411. if mac is None:
  412. continue
  413. if _is_universal(mac):
  414. return mac
  415. if first_local_mac is None:
  416. first_local_mac = mac
  417. return first_local_mac
  418. # The following functions call external programs to 'get' a macaddr value to
  419. # be used as basis for an uuid
  420. def _ifconfig_getnode():
  421. """Get the hardware address on Unix by running ifconfig."""
  422. # This works on Linux ('' or '-a'), Tru64 ('-av'), but not all Unixes.
  423. keywords = (b'hwaddr', b'ether', b'address:', b'lladdr')
  424. for args in ('', '-a', '-av'):
  425. mac = _find_mac_near_keyword('ifconfig', args, keywords, lambda i: i+1)
  426. if mac:
  427. return mac
  428. return None
  429. def _ip_getnode():
  430. """Get the hardware address on Unix by running ip."""
  431. # This works on Linux with iproute2.
  432. mac = _find_mac_near_keyword('ip', 'link', [b'link/ether'], lambda i: i+1)
  433. if mac:
  434. return mac
  435. return None
  436. def _arp_getnode():
  437. """Get the hardware address on Unix by running arp."""
  438. import os, socket
  439. if not hasattr(socket, "gethostbyname"):
  440. return None
  441. try:
  442. ip_addr = socket.gethostbyname(socket.gethostname())
  443. except OSError:
  444. return None
  445. # Try getting the MAC addr from arp based on our IP address (Solaris).
  446. mac = _find_mac_near_keyword('arp', '-an', [os.fsencode(ip_addr)], lambda i: -1)
  447. if mac:
  448. return mac
  449. # This works on OpenBSD
  450. mac = _find_mac_near_keyword('arp', '-an', [os.fsencode(ip_addr)], lambda i: i+1)
  451. if mac:
  452. return mac
  453. # This works on Linux, FreeBSD and NetBSD
  454. mac = _find_mac_near_keyword('arp', '-an', [os.fsencode('(%s)' % ip_addr)],
  455. lambda i: i+2)
  456. # Return None instead of 0.
  457. if mac:
  458. return mac
  459. return None
  460. def _lanscan_getnode():
  461. """Get the hardware address on Unix by running lanscan."""
  462. # This might work on HP-UX.
  463. return _find_mac_near_keyword('lanscan', '-ai', [b'lan0'], lambda i: 0)
  464. def _netstat_getnode():
  465. """Get the hardware address on Unix by running netstat."""
  466. # This works on AIX and might work on Tru64 UNIX.
  467. return _find_mac_under_heading('netstat', '-ian', b'Address')
  468. def _ipconfig_getnode():
  469. """[DEPRECATED] Get the hardware address on Windows."""
  470. # bpo-40501: UuidCreateSequential() is now the only supported approach
  471. return _windll_getnode()
  472. def _netbios_getnode():
  473. """[DEPRECATED] Get the hardware address on Windows."""
  474. # bpo-40501: UuidCreateSequential() is now the only supported approach
  475. return _windll_getnode()
  476. # Import optional C extension at toplevel, to help disabling it when testing
  477. try:
  478. import _uuid
  479. _generate_time_safe = getattr(_uuid, "generate_time_safe", None)
  480. _UuidCreate = getattr(_uuid, "UuidCreate", None)
  481. _has_uuid_generate_time_safe = _uuid.has_uuid_generate_time_safe
  482. except ImportError:
  483. _uuid = None
  484. _generate_time_safe = None
  485. _UuidCreate = None
  486. _has_uuid_generate_time_safe = None
  487. def _load_system_functions():
  488. """[DEPRECATED] Platform-specific functions loaded at import time"""
  489. def _unix_getnode():
  490. """Get the hardware address on Unix using the _uuid extension module."""
  491. if _generate_time_safe:
  492. uuid_time, _ = _generate_time_safe()
  493. return UUID(bytes=uuid_time).node
  494. def _windll_getnode():
  495. """Get the hardware address on Windows using the _uuid extension module."""
  496. if _UuidCreate:
  497. uuid_bytes = _UuidCreate()
  498. return UUID(bytes_le=uuid_bytes).node
  499. def _random_getnode():
  500. """Get a random node ID."""
  501. # RFC 4122, $4.1.6 says "For systems with no IEEE address, a randomly or
  502. # pseudo-randomly generated value may be used; see Section 4.5. The
  503. # multicast bit must be set in such addresses, in order that they will
  504. # never conflict with addresses obtained from network cards."
  505. #
  506. # The "multicast bit" of a MAC address is defined to be "the least
  507. # significant bit of the first octet". This works out to be the 41st bit
  508. # counting from 1 being the least significant bit, or 1<<40.
  509. #
  510. # See https://en.wikipedia.org/w/index.php?title=MAC_address&oldid=1128764812#Universal_vs._local_(U/L_bit)
  511. import random
  512. return random.getrandbits(48) | (1 << 40)
  513. # _OS_GETTERS, when known, are targeted for a specific OS or platform.
  514. # The order is by 'common practice' on the specified platform.
  515. # Note: 'posix' and 'windows' _OS_GETTERS are prefixed by a dll/dlload() method
  516. # which, when successful, means none of these "external" methods are called.
  517. # _GETTERS is (also) used by test_uuid.py to SkipUnless(), e.g.,
  518. # @unittest.skipUnless(_uuid._ifconfig_getnode in _uuid._GETTERS, ...)
  519. if _LINUX:
  520. _OS_GETTERS = [_ip_getnode, _ifconfig_getnode]
  521. elif sys.platform == 'darwin':
  522. _OS_GETTERS = [_ifconfig_getnode, _arp_getnode, _netstat_getnode]
  523. elif sys.platform == 'win32':
  524. # bpo-40201: _windll_getnode will always succeed, so these are not needed
  525. _OS_GETTERS = []
  526. elif _AIX:
  527. _OS_GETTERS = [_netstat_getnode]
  528. else:
  529. _OS_GETTERS = [_ifconfig_getnode, _ip_getnode, _arp_getnode,
  530. _netstat_getnode, _lanscan_getnode]
  531. if os.name == 'posix':
  532. _GETTERS = [_unix_getnode] + _OS_GETTERS
  533. elif os.name == 'nt':
  534. _GETTERS = [_windll_getnode] + _OS_GETTERS
  535. else:
  536. _GETTERS = _OS_GETTERS
  537. _node = None
  538. def getnode():
  539. """Get the hardware address as a 48-bit positive integer.
  540. The first time this runs, it may launch a separate program, which could
  541. be quite slow. If all attempts to obtain the hardware address fail, we
  542. choose a random 48-bit number with its eighth bit set to 1 as recommended
  543. in RFC 4122.
  544. """
  545. global _node
  546. if _node is not None:
  547. return _node
  548. for getter in _GETTERS + [_random_getnode]:
  549. try:
  550. _node = getter()
  551. except:
  552. continue
  553. if (_node is not None) and (0 <= _node < (1 << 48)):
  554. return _node
  555. assert False, '_random_getnode() returned invalid value: {}'.format(_node)
  556. _last_timestamp = None
  557. def uuid1(node=None, clock_seq=None):
  558. """Generate a UUID from a host ID, sequence number, and the current time.
  559. If 'node' is not given, getnode() is used to obtain the hardware
  560. address. If 'clock_seq' is given, it is used as the sequence number;
  561. otherwise a random 14-bit sequence number is chosen."""
  562. # When the system provides a version-1 UUID generator, use it (but don't
  563. # use UuidCreate here because its UUIDs don't conform to RFC 4122).
  564. if _generate_time_safe is not None and node is clock_seq is None:
  565. uuid_time, safely_generated = _generate_time_safe()
  566. try:
  567. is_safe = SafeUUID(safely_generated)
  568. except ValueError:
  569. is_safe = SafeUUID.unknown
  570. return UUID(bytes=uuid_time, is_safe=is_safe)
  571. global _last_timestamp
  572. import time
  573. nanoseconds = time.time_ns()
  574. # 0x01b21dd213814000 is the number of 100-ns intervals between the
  575. # UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
  576. timestamp = nanoseconds // 100 + 0x01b21dd213814000
  577. if _last_timestamp is not None and timestamp <= _last_timestamp:
  578. timestamp = _last_timestamp + 1
  579. _last_timestamp = timestamp
  580. if clock_seq is None:
  581. import random
  582. clock_seq = random.getrandbits(14) # instead of stable storage
  583. time_low = timestamp & 0xffffffff
  584. time_mid = (timestamp >> 32) & 0xffff
  585. time_hi_version = (timestamp >> 48) & 0x0fff
  586. clock_seq_low = clock_seq & 0xff
  587. clock_seq_hi_variant = (clock_seq >> 8) & 0x3f
  588. if node is None:
  589. node = getnode()
  590. return UUID(fields=(time_low, time_mid, time_hi_version,
  591. clock_seq_hi_variant, clock_seq_low, node), version=1)
  592. def uuid3(namespace, name):
  593. """Generate a UUID from the MD5 hash of a namespace UUID and a name."""
  594. if isinstance(name, str):
  595. name = bytes(name, "utf-8")
  596. from hashlib import md5
  597. digest = md5(
  598. namespace.bytes + name,
  599. usedforsecurity=False
  600. ).digest()
  601. return UUID(bytes=digest[:16], version=3)
  602. def uuid4():
  603. """Generate a random UUID."""
  604. return UUID(bytes=os.urandom(16), version=4)
  605. def uuid5(namespace, name):
  606. """Generate a UUID from the SHA-1 hash of a namespace UUID and a name."""
  607. if isinstance(name, str):
  608. name = bytes(name, "utf-8")
  609. from hashlib import sha1
  610. hash = sha1(namespace.bytes + name).digest()
  611. return UUID(bytes=hash[:16], version=5)
  612. def main():
  613. """Run the uuid command line interface."""
  614. uuid_funcs = {
  615. "uuid1": uuid1,
  616. "uuid3": uuid3,
  617. "uuid4": uuid4,
  618. "uuid5": uuid5
  619. }
  620. uuid_namespace_funcs = ("uuid3", "uuid5")
  621. namespaces = {
  622. "@dns": NAMESPACE_DNS,
  623. "@url": NAMESPACE_URL,
  624. "@oid": NAMESPACE_OID,
  625. "@x500": NAMESPACE_X500
  626. }
  627. import argparse
  628. parser = argparse.ArgumentParser(
  629. description="Generates a uuid using the selected uuid function.")
  630. parser.add_argument("-u", "--uuid", choices=uuid_funcs.keys(), default="uuid4",
  631. help="The function to use to generate the uuid. "
  632. "By default uuid4 function is used.")
  633. parser.add_argument("-n", "--namespace",
  634. help="The namespace is a UUID, or '@ns' where 'ns' is a "
  635. "well-known predefined UUID addressed by namespace name. "
  636. "Such as @dns, @url, @oid, and @x500. "
  637. "Only required for uuid3/uuid5 functions.")
  638. parser.add_argument("-N", "--name",
  639. help="The name used as part of generating the uuid. "
  640. "Only required for uuid3/uuid5 functions.")
  641. args = parser.parse_args()
  642. uuid_func = uuid_funcs[args.uuid]
  643. namespace = args.namespace
  644. name = args.name
  645. if args.uuid in uuid_namespace_funcs:
  646. if not namespace or not name:
  647. parser.error(
  648. "Incorrect number of arguments. "
  649. f"{args.uuid} requires a namespace and a name. "
  650. "Run 'python -m uuid -h' for more information."
  651. )
  652. namespace = namespaces[namespace] if namespace in namespaces else UUID(namespace)
  653. print(uuid_func(namespace, name))
  654. else:
  655. print(uuid_func())
  656. # The following standard UUIDs are for use with uuid3() or uuid5().
  657. NAMESPACE_DNS = UUID('6ba7b810-9dad-11d1-80b4-00c04fd430c8')
  658. NAMESPACE_URL = UUID('6ba7b811-9dad-11d1-80b4-00c04fd430c8')
  659. NAMESPACE_OID = UUID('6ba7b812-9dad-11d1-80b4-00c04fd430c8')
  660. NAMESPACE_X500 = UUID('6ba7b814-9dad-11d1-80b4-00c04fd430c8')
  661. if __name__ == "__main__":
  662. main()