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