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
1diff --git a/bin/lsb_resource.py b/bin/lsb_resource.py
2index 297661e..ba8345a 100755
3--- a/bin/lsb_resource.py
4+++ b/bin/lsb_resource.py
5@@ -2,7 +2,7 @@
6 #
7 # This file is part of Checkbox.
8 #
9-# Copyright 2009 Canonical Ltd.
10+# Copyright 2009-2021 Canonical Ltd.
11 #
12 # Checkbox is free software: you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License version 3,
14@@ -18,37 +18,49 @@
15 # along with Checkbox. If not, see <http://www.gnu.org/licenses/>.
16 #
17 import re
18+import subprocess
19 import sys
20
21
22-def get_lsb_release():
23- lsb_release_map = {
24- "DISTRIB_ID": "distributor_id",
25- "DISTRIB_DESCRIPTION": "description",
26- "DISTRIB_RELEASE": "release",
27- "DISTRIB_CODENAME": "codename"}
28-
29- # Create a default lsb_release() dict in case something goes wrong
30- lsb_release = dict((k, 'unknown') for k in lsb_release_map.values())
31+def get_info():
32+ lsb_release = {}
33
34 try:
35- with open('/etc/lsb-release', 'r') as lsb:
36- for line in lsb.readlines():
37- (key, value) = line.split("=", 1)
38- if key in lsb_release_map:
39- key = lsb_release_map[key]
40- # Strip out quotes and newlines
41- lsb_release[key] = re.sub('["\n]', '', value)
42- except OSError:
43- # Missing file or permissions? Return the default lsb_release
44- pass
45+ import distro
46+ lsb_release = {
47+ "distributor_id": distro.name(pretty=False) or "unknown",
48+ "description": distro.name(pretty=True) or "unknown",
49+ "release": distro.version() or "unknown",
50+ "codename": distro.codename() or "unknown"
51+ }
52+ except (ImportError, subprocess.CalledProcessError):
53+ lsb_release_map = {
54+ "DISTRIB_ID": "distributor_id",
55+ "DISTRIB_DESCRIPTION": "description",
56+ "DISTRIB_RELEASE": "release",
57+ "DISTRIB_CODENAME": "codename"}
58+
59+ # Create a default lsb_release() dict in case something goes wrong
60+ lsb_release = dict((k, 'unknown') for k in lsb_release_map.values())
61+
62+ try:
63+ with open('/etc/lsb-release', 'r') as lsb:
64+ for line in lsb.readlines():
65+ (key, value) = line.split("=", 1)
66+ if key in lsb_release_map:
67+ key = lsb_release_map[key]
68+ # Strip out quotes and newlines
69+ lsb_release[key] = re.sub('["\n]', '', value)
70+ except OSError:
71+ # Missing file or permissions? Return the default lsb_release
72+ pass
73
74 return lsb_release
75
76
77 def main():
78- lsb_release = get_lsb_release()
79- for key, value in lsb_release.items():
80+ release_info = get_info()
81+ for key, value in release_info.items():
82 print("%s: %s" % (key, value))
83
84 return 0

Subscribers

People subscribed via source and target branches