File: dkms_info.py

package info (click to toggle)
checkbox-support 0.22-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,620 kB
  • sloc: xml: 14,952; python: 7,921; sh: 4; makefile: 3
file content (118 lines) | stat: -rw-r--r-- 4,206 bytes parent folder | download
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
# This file is part of Checkbox.
#
# Copyright 2015 Canonical Ltd.
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# Checkbox 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 Checkbox.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import io
import json


class DkmsInfoResult():

    """A simple class to hold results for the DkmsInfoParser."""

    def __init__(self):
        self.dkms_info = {}

    def addDkmsInfo(self, pkg, details):
        self.dkms_info[pkg] = details


class DkmsInfoParser(object):

    """
    Parser for output from the dkms_info script.

    This is a very simple parser because the dkms_info script
    is designed to output json which we can simply json.loads().
    No attempt is made to hammer a noncompliant input into submission:
    if it doesn't json.load(), it's simply dropped on the floor.

    The name is slightly misleading as the dkms_info script also shows
    "non-dkms" packages which have a modaliases header (meaning they will
    be matched to specific devices) but don't provide an actual dkms module.
    """

    def __init__(self, stream):
        """
        Instantiate the parser with the stream to parse.

        :param stream: a file-like object in text-mode (so strings
        can be read directly from it)
        """
        self.stream = stream

    def run(self, result):
        """
        Parse the dkms_info output.

        Add each package found to the result instance. The addDkmsInfo method
        will be called with package, details.

        :param result: an object with an addDkmsInfo method.
        """
        try:
            data = json.loads(self.stream.read())
        except ValueError:
            # Return silently; result will be empty
            return
        # dkms output is a dict with lists of packages in each
        # "category". Category examples are "dkms" and "non-dkms".
        for kind, elements in data.items():
            # Elements can either be a list (for dkms) or a dict
            # keyed by package name (for non-dkms)
            try:
                # Assuming it's a dict...
                for package, data in elements.items():
                    # Validate that it contains at least
                    # modaliases and version, otherwise it's probably
                    # incomplete and useless information
                    if 'modaliases' in data and 'version' in data:
                        resdict = {'dkms-status': kind}
                        resdict.update(data)
                        result.addDkmsInfo(package, resdict)
            except AttributeError:
                # Oops, not a dict, maybe it's the list from dkms packages
                try:
                    for data in elements:
                        # Validate that it contains a dkms_name, dkms_ver,
                        # and pkg_name.
                        if all(k in data for k in ('dkms_name',
                                                   'dkms_ver',
                                                   'pkg_name')):
                            package = data['pkg_name']
                            resdict = {'dkms-status': kind}
                            resdict.update(data)
                            result.addDkmsInfo(package, resdict)
                except TypeError:
                    # Not a list either, ignore it silently.
                    pass


def parse_dkms_info(output):
    """
    Parse output of `dkms_info --format json`.

    :returns: no idea.
    """
    stream = io.StringIO(output)
    modparser = DkmsInfoParser(stream)
    result = DkmsInfoResult()
    modparser.run(result)
    return result