Merge ~jocave/plainbox-provider-resource:use-distro-if-available into plainbox-provider-resource:master

Proposed by Jonathan Cave
Status: Merged
Approved by: Jonathan Cave
Approved revision: fbea1adb59d95fc47728528c13c3015af1ab8be5
Merged at revision: 117df7482093b66a45c31062d33775d4babaa457
Proposed branch: ~jocave/plainbox-provider-resource:use-distro-if-available
Merge into: plainbox-provider-resource:master
Diff against target: 84 lines (+34/-22)
1 file modified
bin/lsb_resource.py (+34/-22)
Reviewer Review Type Date Requested Status
Sylvain Pineau (community) Approve
Review via email: mp+397243@code.launchpad.net

Description of the change

Use pytho3-distro when available to make the output of the lsb resource more useful. The crux of the change is that /etc/os-release is used over /etc/lsb-release.

Some example outputs, UC20:

distributor_id: Ubuntu Core
description: Ubuntu Core 20
release: 20
codename: unknown

20.10 desktop:

distributor_id: Ubuntu
description: Ubuntu 20.10
release: 20.10
codename: groovy

20.04 server:

distributor_id: Ubuntu
description: Ubuntu 20.04.1 LTS
release: 20.04
codename: focal

To post a comment you must log in.
Revision history for this message
Sylvain Pineau (sylvain-pineau) wrote :

Using distro module will imply having to deal with this bug: https://github.com/nir0s/distro/issues/260

There's a patch in checkbox-core-snap:

https://git.launchpad.net/checkbox-core-snap/commit/snap/snapcraft.yaml?id=57f5af0255ac19c8c447ebd01be756138c6e4d37

Not sure if we will face it on deb checkbox but it could be a good idea to catch subprocess.CalledProcessError in the resource code and fallback to the old code.

review: Needs Fixing
Revision history for this message
Jonathan Cave (jocave) wrote :

This any better?

Revision history for this message
Sylvain Pineau (sylvain-pineau) wrote :

thanks for the extra try catch, +1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/bin/lsb_resource.py b/bin/lsb_resource.py
index 297661e..ba8345a 100755
--- a/bin/lsb_resource.py
+++ b/bin/lsb_resource.py
@@ -2,7 +2,7 @@
2#2#
3# This file is part of Checkbox.3# This file is part of Checkbox.
4#4#
5# Copyright 2009 Canonical Ltd.5# Copyright 2009-2021 Canonical Ltd.
6#6#
7# Checkbox is free software: you can redistribute it and/or modify7# Checkbox is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License version 3,8# it under the terms of the GNU General Public License version 3,
@@ -18,37 +18,49 @@
18# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.18# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.
19#19#
20import re20import re
21import subprocess
21import sys22import sys
2223
2324
24def get_lsb_release():25def get_info():
25 lsb_release_map = {26 lsb_release = {}
26 "DISTRIB_ID": "distributor_id",
27 "DISTRIB_DESCRIPTION": "description",
28 "DISTRIB_RELEASE": "release",
29 "DISTRIB_CODENAME": "codename"}
30
31 # Create a default lsb_release() dict in case something goes wrong
32 lsb_release = dict((k, 'unknown') for k in lsb_release_map.values())
3327
34 try:28 try:
35 with open('/etc/lsb-release', 'r') as lsb:29 import distro
36 for line in lsb.readlines():30 lsb_release = {
37 (key, value) = line.split("=", 1)31 "distributor_id": distro.name(pretty=False) or "unknown",
38 if key in lsb_release_map:32 "description": distro.name(pretty=True) or "unknown",
39 key = lsb_release_map[key]33 "release": distro.version() or "unknown",
40 # Strip out quotes and newlines34 "codename": distro.codename() or "unknown"
41 lsb_release[key] = re.sub('["\n]', '', value)35 }
42 except OSError:36 except (ImportError, subprocess.CalledProcessError):
43 # Missing file or permissions? Return the default lsb_release37 lsb_release_map = {
44 pass38 "DISTRIB_ID": "distributor_id",
39 "DISTRIB_DESCRIPTION": "description",
40 "DISTRIB_RELEASE": "release",
41 "DISTRIB_CODENAME": "codename"}
42
43 # Create a default lsb_release() dict in case something goes wrong
44 lsb_release = dict((k, 'unknown') for k in lsb_release_map.values())
45
46 try:
47 with open('/etc/lsb-release', 'r') as lsb:
48 for line in lsb.readlines():
49 (key, value) = line.split("=", 1)
50 if key in lsb_release_map:
51 key = lsb_release_map[key]
52 # Strip out quotes and newlines
53 lsb_release[key] = re.sub('["\n]', '', value)
54 except OSError:
55 # Missing file or permissions? Return the default lsb_release
56 pass
4557
46 return lsb_release58 return lsb_release
4759
4860
49def main():61def main():
50 lsb_release = get_lsb_release()62 release_info = get_info()
51 for key, value in lsb_release.items():63 for key, value in release_info.items():
52 print("%s: %s" % (key, value))64 print("%s: %s" % (key, value))
5365
54 return 066 return 0

Subscribers

People subscribed via source and target branches