Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
| 2 | # SPDX-License-Identifier: GPL-2.0-only |
| 3 | # |
| 4 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 5 | def get_os_release(): |
| 6 | """Get all key-value pairs from /etc/os-release as a dict""" |
| 7 | from collections import OrderedDict |
| 8 | |
| 9 | data = OrderedDict() |
| 10 | if os.path.exists('/etc/os-release'): |
| 11 | with open('/etc/os-release') as f: |
| 12 | for line in f: |
| 13 | try: |
| 14 | key, val = line.rstrip().split('=', 1) |
| 15 | except ValueError: |
| 16 | continue |
| 17 | data[key.strip()] = val.strip('"') |
| 18 | return data |
| 19 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 20 | def release_dict_osr(): |
| 21 | """ Populate a dict with pertinent values from /etc/os-release """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 22 | data = {} |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 23 | os_release = get_os_release() |
| 24 | if 'ID' in os_release: |
| 25 | data['DISTRIB_ID'] = os_release['ID'] |
| 26 | if 'VERSION_ID' in os_release: |
| 27 | data['DISTRIB_RELEASE'] = os_release['VERSION_ID'] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 28 | |
| 29 | return data |
| 30 | |
| 31 | def release_dict_lsb(): |
| 32 | """ Return the output of lsb_release -ir as a dictionary """ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 33 | from subprocess import PIPE |
| 34 | |
| 35 | try: |
| 36 | output, err = bb.process.run(['lsb_release', '-ir'], stderr=PIPE) |
| 37 | except bb.process.CmdError as exc: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 38 | return {} |
| 39 | |
| 40 | lsb_map = { 'Distributor ID': 'DISTRIB_ID', |
| 41 | 'Release': 'DISTRIB_RELEASE'} |
| 42 | lsb_keys = lsb_map.keys() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 43 | |
| 44 | data = {} |
| 45 | for line in output.splitlines(): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 46 | if line.startswith("-e"): |
| 47 | line = line[3:] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 48 | try: |
| 49 | key, value = line.split(":\t", 1) |
| 50 | except ValueError: |
| 51 | continue |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 52 | if key in lsb_keys: |
| 53 | data[lsb_map[key]] = value |
| 54 | |
| 55 | if len(data.keys()) != 2: |
| 56 | return None |
| 57 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 58 | return data |
| 59 | |
| 60 | def release_dict_file(): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 61 | """ Try to gather release information manually when other methods fail """ |
| 62 | data = {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 63 | try: |
| 64 | if os.path.exists('/etc/lsb-release'): |
| 65 | data = {} |
| 66 | with open('/etc/lsb-release') as f: |
| 67 | for line in f: |
| 68 | key, value = line.split("=", 1) |
| 69 | data[key] = value.strip() |
| 70 | elif os.path.exists('/etc/redhat-release'): |
| 71 | data = {} |
| 72 | with open('/etc/redhat-release') as f: |
| 73 | distro = f.readline().strip() |
| 74 | import re |
| 75 | match = re.match(r'(.*) release (.*) \((.*)\)', distro) |
| 76 | if match: |
| 77 | data['DISTRIB_ID'] = match.group(1) |
| 78 | data['DISTRIB_RELEASE'] = match.group(2) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 79 | elif os.path.exists('/etc/SuSE-release'): |
| 80 | data = {} |
| 81 | data['DISTRIB_ID'] = 'SUSE LINUX' |
| 82 | with open('/etc/SuSE-release') as f: |
| 83 | for line in f: |
| 84 | if line.startswith('VERSION = '): |
| 85 | data['DISTRIB_RELEASE'] = line[10:].rstrip() |
| 86 | break |
| 87 | |
| 88 | except IOError: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 89 | return {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 90 | return data |
| 91 | |
| 92 | def distro_identifier(adjust_hook=None): |
| 93 | """Return a distro identifier string based upon lsb_release -ri, |
| 94 | with optional adjustment via a hook""" |
| 95 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 96 | import re |
| 97 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 98 | # Try /etc/os-release first, then the output of `lsb_release -ir` and |
| 99 | # finally fall back on parsing various release files in order to determine |
| 100 | # host distro name and version. |
| 101 | distro_data = release_dict_osr() |
| 102 | if not distro_data: |
| 103 | distro_data = release_dict_lsb() |
| 104 | if not distro_data: |
| 105 | distro_data = release_dict_file() |
| 106 | |
| 107 | distro_id = distro_data.get('DISTRIB_ID', '') |
| 108 | release = distro_data.get('DISTRIB_RELEASE', '') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 109 | |
| 110 | if adjust_hook: |
| 111 | distro_id, release = adjust_hook(distro_id, release) |
| 112 | if not distro_id: |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 113 | return "unknown" |
| 114 | # Filter out any non-alphanumerics and convert to lowercase |
| 115 | distro_id = re.sub(r'\W', '', distro_id).lower() |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 116 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 117 | if release: |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 118 | id_str = '{0}-{1}'.format(distro_id, release) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 119 | else: |
| 120 | id_str = distro_id |
| 121 | return id_str.replace(' ','-').replace('/','-') |