This file is indexed.

/usr/lib/python3/dist-packages/checkbox_support/parsers/modinfo.py is in python3-checkbox-support 0.22-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
# This file is part of Checkbox.
#
# Copyright 2011-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


class ModinfoResult():

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

    It has a dict keyed by module name, the data is another dict keyed by field
    with the contents of that field. Some fields can only appear once and the
    content will be a string. Some fields can appear multiple times so their
    key will contain a list of all the arguments they appeared with. So for
    each module, the value is exactly the dict that ModinfoParser.get_all will
    return.
    """

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

    def addModuleInfo(self, module, data):
        """Add the data dict as the value under the module's key."""
        self.mod_data[module] = data


class MultipleModinfoParser():

    """
    Parser for the modinfo_attachment.

    The modinfo_attachment contains records separated by newlines.
    Each record contains the module's name in the name: field (which
    should be the first one) and the rest of the record is, verbatim,
    the output of modinfo $MODULE_NAME.

    The modinfo data for each module can be parsed with ModinfoParser,
    while this parser takes care of splitting the records, running
    ModinfoParser with what it expects, and calling the addModuleInfo
    method for the result instance we were passed at run() time.
    """

    def __init__(self, stream):
        self.stream = stream

    def run(self, result):
        """
        Parse stream and add information for each module to the result.

        For each module found in the stream, its data will be parsed using
        ModinfoParser, and the result's addModuleInfo method will be called
        with the module name and a dict with all the data items parsed from
        it.
        """
        record = []
        mod_name = None
        for line in self.stream:
            # First line, extract module name from this
            # and don't add to the record
            if line.startswith("name:"):
                split_data = line.split(":")
                if len(split_data) == 2:
                    mod_name = split_data[1].strip()
                else:
                    mod_name = None
            else:
                if line.strip() == "" and record:
                    self._parse_record(record, result, mod_name)
                    # Reset the record and mod_name
                    record = []
                    mod_name = None
                else:
                    record.append(line)
        if record:
            # Process last record
            self._parse_record(record, result, mod_name)

    def _parse_record(self, record, result, module):
        """
        Parse the record and maybe add it to the result.

        The records parsed from "record" will be added to "result"
        as the data entry for "module".

        If module is empty or None, it means we couldn't get a module
        name, so don't really do the parsing as a name is strictly required.
        """
        if not module:
            return
        not_a_stream = "".join(record)
        parser = ModinfoParser(not_a_stream)
        data = parser.get_all()
        if any(data.values()):
            result.addModuleInfo(module, data)


class ModinfoParser(object):

    """
    Parser for modinfo information.

    This will take the stdout for modinfo output and return a dict populated
    with each field.

    Basic usage in your script:
    try:
        output = subprocess.check_output('/sbin/modinfo e1000e',
                                         stderr=subprocess.STDOUT,
                                         universal_newlines=True)
    except CalledProcessError as err:
        print("Error while running modinfo")
        print(err.output)
        return err.returncode

    parser = ModinfoParser(output)
    all_fields = parser.get_all()
    one_field = parser.get_field(field)
    """

    def __init__(self, stream):
        """
        Initialize the parser with the given stream.

        The stream should be a string.
        """
        self._modinfo = {'alias': [],
                         'author': '',
                         'depends': [],
                         'description': '',
                         'filename': '',
                         'firmware': [],
                         'intree': '',
                         'license': '',
                         'parm': [],
                         'srcversion': '',
                         'vermagic': '',
                         'version': ''}
        self._get_info(stream)

    def _get_info(self, stream):
        for line in stream.splitlines():
            # At this point, stream should be the stdout from the modinfo
            # command, in a list.
            try:
                key, data = line.split(':', 1)
            except ValueError:
                # Most likely this will be caused by a blank line in the
                # stream, so we just ignore it and move on.
                continue
            else:
                key = key.strip()
                data = data.strip()
                # First, we need to handle alias, parm, firmware, and depends
                # because there can be multiple lines of output for these.
                if key in ('alias', 'depend', 'firmware', 'parm',):
                    self._modinfo[key].append(data)
                else:
                    self._modinfo[key] = data

    def get_all(self):
        """
        Return all the module data as a dictionary.

        If there's no module data (i.e. all elements of the dict
        are empty), return an empty dict instead, which makes it
        easier for callers to verify emptiness while still returning
        a consistent data type.
        """
        if any(self._modinfo.values()):
            return self._modinfo
        else:
            return {}

    def get_field(self, field):
        """Return a specific field from the module data dictionary."""
        if field not in self._modinfo.keys():
            raise Exception("Key not found: %s" % field)
        else:
            return self._modinfo[field]


def parse_modinfo_attachment_output(output):
    r"""
    Parse modinfo_attachment-style output.

    The modinfo_attachment does this (which can also be used for testing)::

        for mod in $(lsmod | cut -f 1 -d " ")
        do
            printf "%-16s%s\n" "name:" "$mod"
            modinfo $mod
            echo
        done

    :returns: a dict with {'module': {'field': 'value', ...}} for each
    module listed. Note that the value can be either a string or a list of
    strings.
    """
    stream = io.StringIO(output)
    modparser = MultipleModinfoParser(stream)
    result = ModinfoResult()
    modparser.run(result)
    return result.mod_data