[Sugar-devel] [PATCH sugar] About my computer: use NetworkManager to acquire list of devices
Sascha Silbe
sascha-pgp at silbe.org
Tue Nov 30 08:37:23 EST 2010
The hardcoded name 'eth0' that is used for querying the firmware version
doesn't match the interface name actually assigned to the wireless device in
recent OLPC builds or on most non-XO hardware.
Querying NetworkManager for the list of devices will work even on systems
where the interface has been renamed during boot (persistent device names).
If there are multiple wireless interfaces, firmware information for all of
them will be shown (prefixed by interface name).
Signed-off-by: Sascha Silbe <sascha-pgp at silbe.org>
---
extensions/cpsection/aboutcomputer/model.py | 72 +++++++++++++++++++++++----
1 files changed, 62 insertions(+), 10 deletions(-)
diff --git a/extensions/cpsection/aboutcomputer/model.py b/extensions/cpsection/aboutcomputer/model.py
index e3b9687..3219dd1 100644
--- a/extensions/cpsection/aboutcomputer/model.py
+++ b/extensions/cpsection/aboutcomputer/model.py
@@ -22,9 +22,17 @@ import subprocess
from gettext import gettext as _
import errno
+import dbus
+
from jarabe import config
+_NM_SERVICE = 'org.freedesktop.NetworkManager'
+_NM_PATH = '/org/freedesktop/NetworkManager'
+_NM_IFACE = 'org.freedesktop.NetworkManager'
+_NM_DEVICE_IFACE = 'org.freedesktop.NetworkManager.Device'
+_NM_DEVICE_TYPE_WIFI = 2
+
_logger = logging.getLogger('ControlPanel - AboutComputer')
_not_available = _('Not available')
@@ -94,18 +102,62 @@ def print_firmware_number():
print get_firmware_number()
-def get_wireless_firmware():
+def _get_wireless_interfaces():
try:
- info = subprocess.Popen(['/usr/sbin/ethtool', '-i', 'eth0'],
- stdout=subprocess.PIPE).stdout.readlines()
- except OSError:
+ bus = dbus.SystemBus()
+ manager_object = bus.get_object(_NM_SERVICE, _NM_PATH)
+ network_manager = dbus.Interface(manager_object, _NM_IFACE)
+ except dbus.DBusException:
+ _logger.warning('Cannot connect to NetworkManager, falling back to'
+ ' static list of devices')
+ return ['wlan0', 'eth0']
+
+ interfaces = []
+ for device_path in network_manager.GetDevices():
+ device_object = bus.get_object(_NM_SERVICE, device_path)
+ properties = dbus.Interface(device_object,
+ 'org.freedesktop.DBus.Properties')
+ device_type = properties.Get(_NM_DEVICE_IFACE, 'DeviceType')
+ if device_type != _NM_DEVICE_TYPE_WIFI:
+ continue
+
+ interfaces.append(properties.Get(_NM_DEVICE_IFACE, 'Interface'))
+
+ return interfaces
+
+
+def get_wireless_firmware():
+ environment = os.environ.copy()
+ environment['PATH'] = '%s:/usr/sbin' % (environment['PATH'], )
+ firmware_info = {}
+ for interface in _get_wireless_interfaces():
+ try:
+ output = subprocess.Popen(['ethtool', '-i', interface],
+ stdout=subprocess.PIPE,
+ env=environment).stdout.readlines()
+ except OSError:
+ _logger.exception('Error running ethtool for %r', interface)
+ continue
+
+ try:
+ version = ([line for line in output
+ if line.startswith('firmware')][0].split()[1])
+ except IndexError:
+ _logger.exception('Error parsing ethtool output for %r',
+ interface)
+ continue
+
+ firmware_info[interface] = version
+
+ if not firmware_info:
return _not_available
- try:
- wireless_firmware = [line for line in info
- if line.startswith('firmware')][0].split()[1]
- except IndexError:
- wireless_firmware = _not_available
- return wireless_firmware
+
+ if len(firmware_info) == 1:
+ return firmware_info.values()[0]
+
+ return ', '.join([_('%(interface)s: %(version)s') %
+ {'interface': interface, 'version': version}
+ for interface, version in firmware_info.items()])
def print_wireless_firmware():
--
1.7.2.3
More information about the Sugar-devel
mailing list