Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/extensions/cpsection/aboutcomputer/model.py
blob: 59bae0082fa8703fe7abd9569d4535347e0a0fba (plain)
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
# Copyright (C) 2008 One Laptop Per Child
#
# This program 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; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#

import os
import logging
import re
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

_OFW_TREE = '/ofw'
_PROC_TREE = '/proc/device-tree'
_DMI_DIRECTORY = '/sys/class/dmi/id'
_SN = 'serial-number'
_MODEL = 'openprom/model'

_logger = logging.getLogger('ControlPanel - AboutComputer')
_not_available = _('Not available')


def get_aboutcomputer():
    msg = 'Serial Number: %s \nBuild Number: %s \nFirmware Number: %s \n' \
            % (get_serial_number(), get_build_number(), get_firmware_number())
    return msg


def print_aboutcomputer():
    print get_aboutcomputer()


def get_serial_number():
    serial_no = None
    if os.path.exists(os.path.join(_OFW_TREE, _SN)):
        serial_no = _read_file(os.path.join(_OFW_TREE, _SN))
    elif os.path.exists(os.path.join(_PROC_TREE, _SN)):
        serial_no = _read_file(os.path.join(_PROC_TREE, _SN))
    if serial_no is None:
        serial_no = _not_available
    return serial_no


def print_serial_number():
    serial_no = get_serial_number()
    if serial_no is None:
        serial_no = _not_available
    print serial_no


def get_build_number():
    build_no = _read_file('/boot/olpc_build')

    if build_no is None:
        build_no = _read_file('/etc/redhat-release')

    if build_no is None:
        try:
            popen = subprocess.Popen(['lsb_release', '-ds'],
                                     stdout=subprocess.PIPE)
        except OSError, e:
            if e.errno != errno.ENOENT:
                raise
        else:
            build_no, stderr_ = popen.communicate()

    if build_no is None or not build_no:
        build_no = _not_available

    return build_no


def print_build_number():
    print get_build_number()


def _parse_firmware_number(firmware_no):
    if firmware_no is None:
        firmware_no = _not_available
    else:
        # try to extract Open Firmware version from OLPC style version
        # string, e.g. "CL2   Q4B11  Q4B"
        if firmware_no.startswith('CL'):
            firmware_no = firmware_no[6:13]
    return firmware_no


def get_firmware_number():
    firmware_no = None
    if os.path.exists(os.path.join(_OFW_TREE, _MODEL)):
        firmware_no = _read_file(os.path.join(_OFW_TREE, _MODEL))
        firmware_no = _parse_firmware_number(firmware_no)
    elif os.path.exists(os.path.join(_PROC_TREE, _MODEL)):
        firmware_no = _read_file(os.path.join(_PROC_TREE, _MODEL))
        firmware_no = _parse_firmware_number(firmware_no)
    elif os.path.exists(os.path.join(_DMI_DIRECTORY, 'bios_version')):
        firmware_no = _read_file(os.path.join(_DMI_DIRECTORY, 'bios_version'))
        if firmware_no is None:
            firmware_no = _not_available
    return firmware_no


def print_firmware_number():
    print get_firmware_number()


def _get_wireless_interfaces():
    try:
        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

    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():
    print get_wireless_firmware()


def _read_file(path):
    if os.access(path, os.R_OK) == 0:
        return None

    fd = open(path, 'r')
    value = fd.read()
    fd.close()
    if value:
        value = value.strip('\n')
        return value
    else:
        _logger.debug('No information in file or directory: %s', path)
        return None


def get_license():
    license_file = os.path.join(config.data_path, 'GPLv2')
    lang = os.environ['LANG']
    if lang.endswith('UTF-8'):
        lang = lang[:-6]

    try_file = license_file + '.' + lang
    if os.path.isfile(try_file):
        license_file = try_file
    else:
        try_file = license_file + '.' + lang.split('_')[0]
        if os.path.isfile(try_file):
            license_file = try_file

    try:
        fd = open(license_file)
        # remove 0x0c page breaks which can't be rendered in text views
        license_text = fd.read().replace('\x0c', '')
        fd.close()
    except IOError:
        license_text = _not_available
    return license_text


def get_last_updated_on_field():

    # Get the number of UNIX seconds of the last update date.
    last_update_unix_seconds = {}
    try:
        last_update_unix_seconds = int(os.stat('/var/lib/rpm/Packages').st_mtime)
    except:
        msg_str = _('Information not available.')
        _logger.exception(msg_str)
        return msg_str


    NO_UPDATE_MESSAGE = _('No update yet!')

    # Check once again that 'last_update_unix_seconds' is not empty.
    # You never know !
    if not last_update_unix_seconds:
        return NO_UPDATE_MESSAGE

    if int(last_update_unix_seconds) == 1194004800:
        return NO_UPDATE_MESSAGE


    # If we reached here, we have the last-update-time, but it's in
    # timestamp format.
    # Using python-subprocess-module (no shell involved), to convert
    # it into readable date-format; the hack being used (after
    # removing '-u' option) is the first one mentioned at :
    # http://www.commandlinefu.com/commands/view/3719/convert-unix-timestamp-to-date
    environment = os.environ.copy()
    environment['PATH'] = '%s:/usr/sbin' % (environment['PATH'], )

    last_update_readable_format = {}
    try:
        last_update_readable_format = \
                 subprocess.Popen(['date', '-d',
                                   '1970-01-01 + ' +
                                   str(last_update_unix_seconds) +
                                   ' seconds'],
                                   stdout=subprocess.PIPE,
                                   env=environment).stdout.readlines()[0]
    except:
        msg_str = _('Information not available.')
        _logger.exception(msg_str)
        return msg_str

    if not last_update_readable_format:
        return _('Information not available.')

    # Everything should be fine (hopefully :-) )
    return last_update_readable_format