-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlsb_release.py
executable file
·442 lines (370 loc) · 15.9 KB
/
lsb_release.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#!/usr/bin/python3 -Es
# LSB release detection module for Debian
# (C) 2005-10 Chris Lawrence <[email protected]>
# (C) 2018 Didier Raboud <[email protected]>
# This package is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 dated June, 1991.
# This package is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this package; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA
from argparse import ArgumentParser
import csv
import os
import re
import subprocess
import sys
import typing
import warnings
StrDict = typing.Dict[str, str]
def get_distro_info(origin: str = "Debian") -> None:
try:
csvfile = open('/usr/share/distro-info/%s.csv' % origin.lower())
except FileNotFoundError:
# Unknown distro, fallback to Debian
csvfile = open('/usr/share/distro-info/debian.csv')
reader = csv.DictReader(csvfile)
global RELEASE_CODENAME_LOOKUP, RELEASES_ORDER, TESTING_CODENAME
RELEASE_CODENAME_LOOKUP = { r['version']: r['series'] for r in reader if r['version']}
RELEASES_ORDER = list(RELEASE_CODENAME_LOOKUP.items())
RELEASES_ORDER.sort(key=lambda n: [int(v) for v in re.split('\D+', n[0]) if v.isdigit()])
RELEASES_ORDER = list(list(zip(*RELEASES_ORDER))[1])
if origin.lower() == 'debian':
TESTING_CODENAME = 'unknown.new.testing'
RELEASES_ORDER.extend(['stable', 'proposed-updates', 'testing', 'testing-proposed-updates', 'unstable', 'sid'])
csvfile.close()
# Populate default distro info
get_distro_info()
def lookup_codename(release: str, unknown: typing.Optional[str] = None) -> str:
m = re.match(r'(\d+)\.(\d+)(r(\d+))?', release)
if not m:
return unknown
if int(m.group(1)) < 7:
shortrelease = '%s.%s' % m.group(1,2)
else:
shortrelease = '%s' % m.group(1)
return RELEASE_CODENAME_LOOKUP.get(shortrelease, unknown)
def valid_lsb_versions(version: str, module: str) -> typing.List[str]:
# If a module is ever released that only appears in >= version, deal
# with that here
if version == '3.0':
return ['2.0', '3.0']
elif version == '3.1':
if module in ('desktop', 'qt4'):
return ['3.1']
elif module == 'cxx':
return ['3.0', '3.1']
else:
return ['2.0', '3.0', '3.1']
elif version == '3.2':
if module == 'desktop':
return ['3.1', '3.2']
elif module == 'qt4':
return ['3.1']
elif module in ('printing', 'languages', 'multimedia'):
return ['3.2']
elif module == 'cxx':
return ['3.0', '3.1', '3.2']
else:
return ['2.0', '3.0', '3.1', '3.2']
elif version == '4.0':
if module == 'desktop':
return ['3.1', '3.2', '4.0']
elif module == 'qt4':
return ['3.1']
elif module in ('printing', 'languages', 'multimedia'):
return ['3.2', '4.0']
elif module == 'security':
return ['4.0']
elif module == 'cxx':
return ['3.0', '3.1', '3.2', '4.0']
else:
return ['2.0', '3.0', '3.1', '3.2', '4.0']
elif version == '4.1':
if module == 'desktop':
return ['3.1', '3.2', '4.0', '4.1']
elif module == 'qt4':
return ['3.1']
elif module in ('printing', 'languages', 'multimedia'):
return ['3.2', '4.0', '4.1']
elif module == 'security':
return ['4.0', '4.1']
elif module == 'cxx':
return ['3.0', '3.1', '3.2', '4.0', '4.1']
else:
return ['2.0', '3.0', '3.1', '3.2', '4.0', '4.1']
return [version]
try:
set # introduced in 2.4
except NameError:
import sets
set = sets.Set
# This is Debian-specific at present
def check_modules_installed() -> typing.List[typing.Any]:
return []
longnames = {'v' : 'version', 'o': 'origin', 'a': 'suite',
'c' : 'component', 'l': 'label'}
def parse_policy_line(data: str) -> StrDict:
retval = {}
bits = data.split(',')
for bit in bits:
kv = bit.split('=', 1)
if len(kv) > 1:
k, v = kv[:2]
if k in longnames:
retval[longnames[k]] = v
return retval
def release_index(x: typing.List[typing.Union[str, StrDict]]) -> int:
suite = x[1].get('suite')
if suite:
if suite in RELEASES_ORDER:
return int(len(RELEASES_ORDER) - RELEASES_ORDER.index(suite))
else:
try:
return float(suite)
except ValueError:
return 0
return 0
def compare_release(x: typing.List[typing.Union[str, StrDict]], y: typing.List[typing.Union[str, StrDict]]) -> int:
warnings.warn('compare_release(x,y) is deprecated; please use the release_index(x) as key for sort() instead.', DeprecationWarning, stacklevel=2)
suite_x_i = release_index(x)
suite_y_i = release_index(y)
try:
return suite_x_i - suite_y_i
except TypeError:
return (suite_x_i > suite_y_i) - (suite_x_i < suite_y_i)
def parse_apt_policy() -> typing.List[typing.Tuple[int, StrDict]]:
data = []
C_env = os.environ.copy(); C_env['LC_ALL'] = 'C.UTF-8'
try:
policy = subprocess.Popen(['apt-cache','policy'],
env=C_env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
close_fds=True).communicate()[0].decode('utf-8')
except Exception as e:
print('Failed to run apt-cache:', e, file=sys.stderr)
return
for line in policy.split('\n'):
line = line.strip()
m = re.match(r'(-?\d+)', line)
if m:
priority = int(m.group(1))
if line.startswith('release'):
bits = line.split(' ', 1)
if len(bits) > 1:
data.append( (priority, parse_policy_line(bits[1])) )
return data
def guess_release_from_apt(origin: str = "Debian", component: str = "main", ignoresuites: str = ("experimental"), label: str = "Debian", alternate_olabels: typing.Dict[str, typing.Tuple[str, str]] = {"Debian Ports": ("ftp.ports.debian.org", "ftp.debian-ports.org")}) -> None:
releases = parse_apt_policy()
if not releases:
return None
# We only care about the specified origin, component, and label
releases = [x for x in releases if (
x[1].get('origin', '') == origin and
x[1].get('suite', '') not in ignoresuites and
x[1].get('component', '') == component and
x[1].get('label', '') == label) or (
x[1].get('origin', '') in alternate_olabels and
x[1].get('label', '') in alternate_olabels.get(x[1].get('origin', '')))]
# Check again to make sure we didn't wipe out all of the releases
if not releases:
return None
releases.sort(key=lambda tuple: tuple[0],reverse=True)
# We've sorted the list by descending priority, so the first entry should
# be the "main" release in use on the system
max_priority = releases[0][0]
releases = [x for x in releases if x[0] == max_priority]
releases.sort(key=release_index)
return releases[0][1]
def guess_debian_release() -> StrDict:
distinfo = {}
distinfo['ID'] = 'Debian'
# Use /etc/dpkg/origins/default to fetch the distribution name
etc_dpkg_origins_default = os.environ.get('LSB_ETC_DPKG_ORIGINS_DEFAULT','/etc/dpkg/origins/default')
if os.path.exists(etc_dpkg_origins_default):
try:
with open(etc_dpkg_origins_default) as dpkg_origins_file:
for line in dpkg_origins_file:
try:
(header, content) = line.split(': ', 1)
header = header.lower()
content = content.strip()
if header == 'vendor':
distinfo['ID'] = content
except ValueError:
pass
except IOError as msg:
print('Unable to open ' + etc_dpkg_origins_default + ':', str(msg), file=sys.stderr)
# Populate RELEASES_ORDER for the correct distro
get_distro_info(distinfo['ID'])
kern = os.uname()[0]
if kern in ('Linux', 'Hurd', 'NetBSD'):
distinfo['OS'] = 'GNU/'+kern
elif kern == 'FreeBSD':
distinfo['OS'] = 'GNU/k'+kern
elif kern in ('GNU/Linux', 'GNU/kFreeBSD'):
distinfo['OS'] = kern
else:
distinfo['OS'] = 'GNU'
distinfo['DESCRIPTION'] = '%(ID)s %(OS)s' % distinfo
etc_debian_version = os.environ.get('LSB_ETC_DEBIAN_VERSION','/etc/debian_version')
if os.path.exists(etc_debian_version):
try:
with open(etc_debian_version) as debian_version:
release = debian_version.read().strip()
except IOError as msg:
print('Unable to open ' + etc_debian_version + ':', str(msg), file=sys.stderr)
release = 'unknown'
if not release[0:1].isalpha():
# /etc/debian_version should be numeric
codename = lookup_codename(release, 'n/a')
distinfo.update({ 'RELEASE' : release, 'CODENAME' : codename })
elif release.endswith('/sid'):
if release.rstrip('/sid').lower() != 'testing':
global TESTING_CODENAME
TESTING_CODENAME = release.rstrip('/sid')
distinfo['RELEASE'] = 'testing/unstable'
else:
distinfo['RELEASE'] = release
# Only use apt information if we did not get the proper information
# from /etc/debian_version or if we don't have a codename
# (which will happen if /etc/debian_version does not contain a
# number but some text like 'testing/unstable' or 'lenny/sid')
#
# This is slightly faster and less error prone in case the user
# has an entry in his /etc/apt/sources.list but has not actually
# upgraded the system.
if not distinfo.get('CODENAME'):
rinfo = guess_release_from_apt()
if rinfo:
release = rinfo.get('version')
# Special case Debian-Ports as their Release file has 'version': '1.0'
if release == '1.0' and rinfo.get('origin') == 'Debian Ports' and rinfo.get('label') in ('ftp.ports.debian.org', 'ftp.debian-ports.org'):
release = None
rinfo.update({'suite': 'unstable'})
if release:
codename = lookup_codename(release, 'n/a')
else:
release = rinfo.get('suite', 'unstable')
if release == 'testing':
# Would be nice if I didn't have to hardcode this.
codename = TESTING_CODENAME
else:
codename = 'sid'
distinfo.update({ 'RELEASE' : release, 'CODENAME' : codename })
if distinfo.get('RELEASE'):
distinfo['DESCRIPTION'] += ' %(RELEASE)s' % distinfo
if distinfo.get('CODENAME'):
distinfo['DESCRIPTION'] += ' (%(CODENAME)s)' % distinfo
return distinfo
# Whatever is guessed above can be overridden in /usr/lib/os-release by derivatives
def get_os_release() -> StrDict:
distinfo = {}
os_release = os.environ.get('LSB_OS_RELEASE', '/usr/lib/os-release')
if os.path.exists(os_release):
try:
with open(os_release) as os_release_file:
for line in os_release_file:
line = line.strip()
if not line:
continue
# Skip invalid lines
if not '=' in line:
continue
var, arg = line.split('=', 1)
if arg.startswith('"') and arg.endswith('"'):
arg = arg[1:-1]
if arg: # Ignore empty arguments
# Concert os-release to lsb-release-style
if var == 'VERSION_ID':
# It'll ignore point-releases
distinfo['RELEASE'] = arg.strip()
elif var == 'VERSION_CODENAME':
distinfo['CODENAME'] = arg.strip()
elif var == 'ID':
# ID=debian
distinfo['ID'] = arg.strip().title()
elif var == 'PRETTY_NAME':
distinfo['DESCRIPTION'] = arg.strip()
except IOError as msg:
print('Unable to open ' + os_release + ':', str(msg), file=sys.stderr)
return distinfo
def get_distro_information() -> StrDict:
lsbinfo = get_os_release()
# OS is only used inside guess_debian_release anyway
for key in ('ID', 'RELEASE', 'CODENAME', 'DESCRIPTION',):
if key not in lsbinfo:
distinfo = guess_debian_release()
distinfo.update(lsbinfo)
return distinfo
else:
return lsbinfo
def test():
print(get_distro_information())
print(check_modules_installed())
def main():
parser = ArgumentParser()
parser.add_argument('-v', '--version', dest='version', action='store_true',
default=False,
help="show LSB modules this system supports")
parser.add_argument('-i', '--id', dest='id', action='store_true',
default=False,
help="show distributor ID")
parser.add_argument('-d', '--description', dest='description',
default=False, action='store_true',
help="show description of this distribution")
parser.add_argument('-r', '--release', dest='release',
default=False, action='store_true',
help="show release number of this distribution")
parser.add_argument('-c', '--codename', dest='codename',
default=False, action='store_true',
help="show code name of this distribution")
parser.add_argument('-a', '--all', dest='all',
default=False, action='store_true',
help="show all of the above information")
parser.add_argument('-s', '--short', dest='short',
action='store_true', default=False,
help="show requested information in short format")
(options, args) = parser.parse_args()
if args:
parser.error("No arguments are permitted")
short = (options.short)
none = not (options.all or options.version or options.id or
options.description or options.codename or options.release)
distinfo = lsb_release.get_distro_information()
if none or options.all or options.version:
verinfo = lsb_release.check_modules_installed()
if not verinfo:
print("No LSB modules are available.", file=sys.stderr)
elif short:
print(':'.join(verinfo))
else:
print('LSB Version:\t' + ':'.join(verinfo))
if options.id or options.all:
if short:
print(distinfo.get('ID', 'n/a'))
else:
print('Distributor ID:\t%s' % distinfo.get('ID', 'n/a'))
if options.description or options.all:
if short:
print(distinfo.get('DESCRIPTION', 'n/a'))
else:
print('Description:\t%s' % distinfo.get('DESCRIPTION', 'n/a'))
if options.release or options.all:
if short:
print(distinfo.get('RELEASE', 'n/a'))
else:
print('Release:\t%s' % distinfo.get('RELEASE', 'n/a'))
if options.codename or options.all:
if short:
print(distinfo.get('CODENAME', 'n/a'))
else:
print('Codename:\t%s' % distinfo.get('CODENAME', 'n/a'))
if __name__ == '__main__':
main()