| 1 | ''' |
| 2 | Module of MacOS API for plyer.uniqueid. |
| 3 | ''' |
| 4 | |
| 5 | from os import environ |
| 6 | from subprocess import Popen, PIPE |
| 7 | from plyer.facades import UniqueID |
| 8 | from plyer.utils import whereis_exe |
| 9 | |
| 10 | |
| 11 | class OSXUniqueID(UniqueID): |
| 12 | ''' |
| 13 | Implementation of MacOS uniqueid API. |
| 14 | ''' |
| 15 | |
| 16 | def _get_uid(self): |
| 17 | old_lang = environ.get('LANG') |
| 18 | environ['LANG'] = 'C' |
| 19 | |
| 20 | ioreg_process = Popen(["ioreg", "-l"], stdout=PIPE) |
| 21 | grep_process = Popen( |
| 22 | ["grep", "IOPlatformSerialNumber"], |
| 23 | stdin=ioreg_process.stdout, stdout=PIPE |
| 24 | ) |
| 25 | ioreg_process.stdout.close() |
| 26 | output = grep_process.communicate()[0] |
| 27 | |
| 28 | if old_lang is None: |
| 29 | environ.pop('LANG') |
| 30 | else: |
| 31 | environ['LANG'] = old_lang |
| 32 | |
| 33 | result = None |
| 34 | if output: |
| 35 | result = output.split()[3][1:-1] |
| 36 | return result |
| 37 | |
| 38 | |
| 39 | def instance(): |
| 40 | ''' |
| 41 | Instance for facade proxy. |
| 42 | ''' |
| 43 | import sys |
| 44 | if whereis_exe('ioreg'): |
| 45 | return OSXUniqueID() |
| 46 | sys.stderr.write("ioreg not found.") |
| 47 | return UniqueID() |