blob: 3a945e0fce5c1f6f25707cb5fe23a3d1c94aa422 [file] [log] [blame]
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001def 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
20def release_dict_lsb():
21 """ Return the output of lsb_release -ir as a dictionary """
Patrick Williamsc124f4f2015-09-15 14:41:29 -050022 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 Bishop6e60e8b2018-02-01 10:27:11 -050027 return {}
28
29 lsb_map = { 'Distributor ID': 'DISTRIB_ID',
30 'Release': 'DISTRIB_RELEASE'}
31 lsb_keys = lsb_map.keys()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050032
33 data = {}
34 for line in output.splitlines():
Brad Bishop6e60e8b2018-02-01 10:27:11 -050035 if line.startswith("-e"):
36 line = line[3:]
Patrick Williamsc124f4f2015-09-15 14:41:29 -050037 try:
38 key, value = line.split(":\t", 1)
39 except ValueError:
40 continue
Brad Bishop6e60e8b2018-02-01 10:27:11 -050041 if key in lsb_keys:
42 data[lsb_map[key]] = value
43
44 if len(data.keys()) != 2:
45 return None
46
Patrick Williamsc124f4f2015-09-15 14:41:29 -050047 return data
48
49def release_dict_file():
Brad Bishop6e60e8b2018-02-01 10:27:11 -050050 """ Try to gather release information manually when other methods fail """
51 data = {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050052 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 Williamsc124f4f2015-09-15 14:41:29 -050068 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 Bishop6e60e8b2018-02-01 10:27:11 -050078 return {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050079 return data
80
81def 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 Williamsd8c66bc2016-06-20 12:57:21 -050085 import re
86
Brad Bishop6e60e8b2018-02-01 10:27:11 -050087 # 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 Williamsc124f4f2015-09-15 14:41:29 -050098
99 if adjust_hook:
100 distro_id, release = adjust_hook(distro_id, release)
101 if not distro_id:
102 return "Unknown"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500103 # Filter out any non-alphanumerics
104 distro_id = re.sub(r'\W', '', distro_id)
105
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500106 if release:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500107 id_str = '{0}-{1}'.format(distro_id.lower(), release)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500108 else:
109 id_str = distro_id
110 return id_str.replace(' ','-').replace('/','-')