| 1 | ''' |
| 2 | Module of Linux 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 LinuxUniqueID(UniqueID): |
| 12 | ''' |
| 13 | Implementation of Linux uniqueid API. |
| 14 | ''' |
| 15 | |
| 16 | def _get_uid(self): |
| 17 | old_lang = environ.get('LANG') |
| 18 | environ['LANG'] = 'C' |
| 19 | stdout = Popen( |
| 20 | ["lshw", "-quiet"], |
| 21 | stdout=PIPE, stderr=PIPE |
| 22 | ).communicate()[0].decode('utf-8') |
| 23 | |
| 24 | output = u'' |
| 25 | for line in stdout.splitlines(): |
| 26 | if 'serial:' not in line: |
| 27 | continue |
| 28 | output = line |
| 29 | break |
| 30 | |
| 31 | environ['LANG'] = old_lang or u'' |
| 32 | result = None |
| 33 | |
| 34 | if output: |
| 35 | result = output.split()[1] |
| 36 | return result |
| 37 | |
| 38 | |
| 39 | def instance(): |
| 40 | ''' |
| 41 | Instance for facade proxy. |
| 42 | ''' |
| 43 | import sys |
| 44 | if whereis_exe('lshw'): |
| 45 | return LinuxUniqueID() |
| 46 | sys.stderr.write("lshw not found.") |
| 47 | return UniqueID() |