summaryrefslogtreecommitdiff
path: root/libkmod/python/kmod/kmod.pyx
blob: 8a13f3a3b2fc033e0a04e4f104e1aaad8b7d2e7d (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
# Copyright (C) 2012 Red Hat, Inc. All rights reserved.
#               2012 W. Trevor King
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions
# of the GNU Lesser General Public License v.2.1.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"Define the Kmod class"

cimport cython as _cython
cimport _libkmod_h
from error import KmodError as _KmodError
cimport module as _module
import module as _module
cimport list as _list
import list as _list


cdef class Kmod (object):
    "Wrap a struct kmod_ctx* item"
    def __cinit__(self):
        self._kmod_ctx = NULL

    def __dealloc__(self):
        self._cleanup()

    def __init__(self, mod_dir=None):
        self.set_mod_dir(mod_dir=mod_dir)

    def set_mod_dir(self, mod_dir=None):
        self.mod_dir = mod_dir
        self._setup()

    def _setup(self):
        cdef char *mod_dir = NULL
        self._cleanup()
        if self.mod_dir:
            mod_dir = self.mod_dir
        self._kmod_ctx = _libkmod_h.kmod_new(mod_dir, NULL);
        if self._kmod_ctx is NULL:
            raise _KmodError('Could not initialize')
        _libkmod_h.kmod_load_resources(self._kmod_ctx)

    def _cleanup(self):
        if self._kmod_ctx is not NULL:
            _libkmod_h.kmod_unload_resources(self._kmod_ctx);
            self._kmod_ctx = NULL

    def loaded(self):
        "iterate through currently loaded modules"
        cdef _list.ModList ml = _list.ModList()
        cdef _list.ModListItem mli
        err = _libkmod_h.kmod_module_new_from_loaded(self._kmod_ctx, &ml.list)
        if err < 0:
            raise _KmodError('Could not get loaded modules')
        for item in ml:
            mli = <_list.ModListItem> item
            mod = _module.Module()
            mod.from_mod_list_item(item)
            yield mod

    def lookup(self, alias_name, flags=_libkmod_h.KMOD_PROBE_APPLY_BLACKLIST):
        "iterate through modules matching `alias_name`"
        cdef _list.ModList ml = _list.ModList()
        cdef _list.ModListItem mli
        if hasattr(alias_name, 'encode'):
            alias_name = alias_name.encode('ascii')
        err = _libkmod_h.kmod_module_new_from_lookup(
            self._kmod_ctx, alias_name, &ml.list)
        if err < 0:
            raise _KmodError('Could not modprobe')
        for item in ml:
            mli = <_list.ModListItem> item
            mod = _module.Module()
            mod.from_mod_list_item(item)
            yield mod

    @_cython.always_allow_keywords(True)
    def module_from_name(self, name):
        cdef _module.Module mod = _module.Module()
        if hasattr(name, 'encode'):
            name = name.encode('ascii')
        err = _libkmod_h.kmod_module_new_from_name(
            self._kmod_ctx, name, &mod.module)
        if err < 0:
            raise _KmodError('Could not get module')
        return mod

    def list(self):
        "iterate through currently loaded modules and sizes"
        for mod in self.loaded():
            yield (mod.name, mod.size)

    def modprobe(self, alias_name, *args, **kwargs):
        for mod in self.lookup(alias_name=alias_name):
            mod.insert(*args, **kwargs)

    def rmmod(self, module_name, *args, **kwargs):
       mod = self.module_from_name(name=module_name)
       mod.remove(*args, **kwargs)