0
# Data Source Access
1
2
Low-level access to the underlying data sources that distro uses for distribution detection. These functions provide direct access to raw data from os-release files, lsb_release command, distro release files, and uname command.
3
4
## Capabilities
5
6
### OS Release Information
7
8
Access to `/etc/os-release` or `/usr/lib/os-release` file contents.
9
10
```python { .api }
11
def os_release_info() -> Dict[str, str]:
12
"""
13
Return the os-release information as a dictionary.
14
15
Reads from /etc/os-release with fallback to /usr/lib/os-release.
16
This is the modern standard for Linux distribution identification.
17
18
Returns:
19
Dict[str, str]: Key-value pairs from os-release file
20
21
Common keys include:
22
- ID: distribution identifier
23
- NAME: human-readable name
24
- VERSION: version string
25
- VERSION_ID: machine-readable version
26
- VERSION_CODENAME: release codename
27
- ID_LIKE: similar distributions
28
- PRETTY_NAME: formatted name with version
29
- HOME_URL: distribution homepage
30
- BUG_REPORT_URL: bug reporting URL
31
"""
32
```
33
34
Usage example:
35
```python
36
import distro
37
38
os_info = distro.os_release_info()
39
print(os_info)
40
# {
41
# 'ID': 'ubuntu',
42
# 'NAME': 'Ubuntu',
43
# 'VERSION': '20.04.3 LTS (Focal Fossa)',
44
# 'VERSION_ID': '20.04',
45
# 'VERSION_CODENAME': 'focal',
46
# 'ID_LIKE': 'debian',
47
# 'PRETTY_NAME': 'Ubuntu 20.04.3 LTS',
48
# 'HOME_URL': 'https://www.ubuntu.com/',
49
# 'BUG_REPORT_URL': 'https://bugs.launchpad.net/ubuntu/'
50
# }
51
52
# Access specific values
53
if 'ID' in os_info:
54
print(f"Distribution ID: {os_info['ID']}")
55
```
56
57
### LSB Release Information
58
59
Access to `lsb_release` command output.
60
61
```python { .api }
62
def lsb_release_info() -> Dict[str, str]:
63
"""
64
Return the lsb_release information as a dictionary.
65
66
Executes 'lsb_release -a' command and parses output.
67
LSB (Linux Standard Base) compliance information.
68
69
Returns:
70
Dict[str, str]: Key-value pairs from lsb_release command
71
72
Common keys include:
73
- Distributor ID: distribution identifier
74
- Description: full description with version
75
- Release: version number
76
- Codename: release codename
77
"""
78
```
79
80
Usage example:
81
```python
82
import distro
83
84
lsb_info = distro.lsb_release_info()
85
print(lsb_info)
86
# {
87
# 'Distributor ID': 'Ubuntu',
88
# 'Description': 'Ubuntu 20.04.3 LTS',
89
# 'Release': '20.04',
90
# 'Codename': 'focal'
91
# }
92
93
# Check if LSB is available
94
if lsb_info:
95
print(f"LSB Description: {lsb_info.get('Description', 'N/A')}")
96
else:
97
print("LSB release information not available")
98
```
99
100
### Distro Release Information
101
102
Access to distribution-specific release files.
103
104
```python { .api }
105
def distro_release_info() -> Dict[str, str]:
106
"""
107
Return the distro release file information as a dictionary.
108
109
Reads from distribution-specific release files like:
110
- /etc/redhat-release (Red Hat, CentOS, Fedora)
111
- /etc/debian_version (Debian)
112
- /etc/SuSE-release (openSUSE)
113
- /etc/arch-release (Arch Linux)
114
115
Returns:
116
Dict[str, str]: Parsed information from release files
117
118
Keys vary by distribution but commonly include:
119
- name: distribution name
120
- version: version string
121
- codename: release codename (if available)
122
"""
123
```
124
125
Usage example:
126
```python
127
import distro
128
129
release_info = distro.distro_release_info()
130
print(release_info)
131
# Example for CentOS:
132
# {
133
# 'name': 'CentOS Linux',
134
# 'version': '8.4.2105',
135
# 'codename': 'Core'
136
# }
137
138
# Check what release files were found
139
if release_info:
140
print(f"Release file info: {release_info}")
141
else:
142
print("No distro release files found")
143
```
144
145
### Uname Information
146
147
Access to `uname` command output, primarily for BSD systems.
148
149
```python { .api }
150
def uname_info() -> Dict[str, str]:
151
"""
152
Return the uname information as a dictionary.
153
154
Executes 'uname -rs' command to get system information.
155
Primarily used for BSD-based systems where other methods may not be available.
156
157
Returns:
158
Dict[str, str]: System information from uname
159
160
Keys include:
161
- name: operating system name
162
- release: release version
163
"""
164
```
165
166
Usage example:
167
```python
168
import distro
169
170
uname_info = distro.uname_info()
171
print(uname_info)
172
# Example for FreeBSD:
173
# {
174
# 'name': 'FreeBSD',
175
# 'release': '13.0-RELEASE'
176
# }
177
178
# Useful for BSD detection
179
if 'BSD' in uname_info.get('name', ''):
180
print("BSD-based system detected")
181
```
182
183
### Attribute Access Functions
184
185
Direct access to specific attributes from each data source.
186
187
```python { .api }
188
def os_release_attr(attribute: str) -> str:
189
"""
190
Return a single named attribute from os-release.
191
192
Args:
193
attribute: Name of the attribute to retrieve
194
195
Returns:
196
str: Attribute value, empty string if not found
197
"""
198
199
def lsb_release_attr(attribute: str) -> str:
200
"""
201
Return a single named attribute from lsb_release.
202
203
Args:
204
attribute: Name of the attribute to retrieve
205
206
Returns:
207
str: Attribute value, empty string if not found
208
"""
209
210
def distro_release_attr(attribute: str) -> str:
211
"""
212
Return a single named attribute from distro release file.
213
214
Args:
215
attribute: Name of the attribute to retrieve
216
217
Returns:
218
str: Attribute value, empty string if not found
219
"""
220
221
def uname_attr(attribute: str) -> str:
222
"""
223
Return a single named attribute from uname.
224
225
Args:
226
attribute: Name of the attribute to retrieve ('name' or 'release')
227
228
Returns:
229
str: Attribute value, empty string if not found
230
"""
231
```
232
233
Usage examples:
234
```python
235
import distro
236
237
# Get specific attributes from each source
238
os_id = distro.os_release_attr('ID')
239
lsb_desc = distro.lsb_release_attr('Description')
240
release_name = distro.distro_release_attr('name')
241
uname_name = distro.uname_attr('name')
242
243
print(f"OS Release ID: {os_id}")
244
print(f"LSB Description: {lsb_desc}")
245
print(f"Release name: {release_name}")
246
print(f"Uname name: {uname_name}")
247
248
# Fallback pattern
249
version = (distro.os_release_attr('VERSION_ID') or
250
distro.lsb_release_attr('Release') or
251
distro.distro_release_attr('version'))
252
print(f"Version from any source: {version}")
253
```
254
255
## Data Source Availability
256
257
Not all data sources are available on every system:
258
259
- **os-release**: Available on most modern Linux distributions (systemd-based)
260
- **lsb_release**: Requires LSB package installation, not always present
261
- **Distro release files**: Distribution-specific, legacy method
262
- **uname**: Available on all Unix-like systems, limited information
263
264
```python
265
import distro
266
267
# Check data source availability
268
sources = {
269
'os-release': bool(distro.os_release_info()),
270
'lsb_release': bool(distro.lsb_release_info()),
271
'distro_release': bool(distro.distro_release_info()),
272
'uname': bool(distro.uname_info())
273
}
274
275
print("Available data sources:")
276
for source, available in sources.items():
277
status = "✓" if available else "✗"
278
print(f" {status} {source}")
279
```
280
281
## Error Handling
282
283
Data source functions return empty dictionaries or strings when:
284
- Files don't exist or aren't readable
285
- Commands fail to execute
286
- Data cannot be parsed
287
288
Always check return values before using them:
289
290
```python
291
import distro
292
293
# Safe access pattern
294
os_info = distro.os_release_info()
295
if os_info and 'ID' in os_info:
296
dist_id = os_info['ID']
297
else:
298
dist_id = "unknown"
299
300
print(f"Distribution: {dist_id}")
301
```