1 # Copyright (C) 2012 Red Hat, Inc.
2 # W. Trevor King <wking@tremily.us>
4 # This file is part of python-kmod.
6 # python-kmod is free software: you can redistribute it and/or modify it under
7 # the terms of the GNU Lesser General Public License version 2.1 as published
8 # by the Free Software Foundation.
10 # python-kmod is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with python-kmod. If not, see <http://www.gnu.org/licenses/>.
18 "Define the Kmod class"
20 cimport cython as _cython
22 from error import KmodError as _KmodError
23 cimport module as _module
24 import module as _module
29 cdef class Kmod (object):
30 "Wrap a struct kmod_ctx* item"
35 def __dealloc__(self):
38 def __init__(self, mod_dir=None):
39 self.set_mod_dir(mod_dir=mod_dir)
41 def set_mod_dir(self, mod_dir=None):
42 self.mod_dir = mod_dir
46 cdef char *mod_dir = NULL
49 mod_dir = self.mod_dir
50 self._kmod_ctx = _libkmod_h.kmod_new(mod_dir, NULL);
51 if self._kmod_ctx is NULL:
52 raise _KmodError('Could not initialize')
53 _libkmod_h.kmod_load_resources(self._kmod_ctx)
56 if self._kmod_ctx is not NULL:
57 _libkmod_h.kmod_unload_resources(self._kmod_ctx);
61 "iterate through currently loaded modules"
62 cdef _list.ModList ml = _list.ModList()
63 cdef _list.ModListItem mli
64 err = _libkmod_h.kmod_module_new_from_loaded(self._kmod_ctx, &ml.list)
66 raise _KmodError('Could not get loaded modules')
68 mli = <_list.ModListItem> item
69 mod = _module.Module()
70 mod.from_mod_list_item(item)
73 def lookup(self, alias_name, flags=_libkmod_h.KMOD_PROBE_APPLY_BLACKLIST):
74 "iterate through modules matching `alias_name`"
75 cdef _list.ModList ml = _list.ModList()
76 cdef _list.ModListItem mli
77 if hasattr(alias_name, 'encode'):
78 alias_name = alias_name.encode('ascii')
79 err = _libkmod_h.kmod_module_new_from_lookup(
80 self._kmod_ctx, alias_name, &ml.list)
82 raise _KmodError('Could not modprobe')
84 mli = <_list.ModListItem> item
85 mod = _module.Module()
86 mod.from_mod_list_item(item)
89 @_cython.always_allow_keywords(True)
90 def module_from_name(self, name):
91 cdef _module.Module mod = _module.Module()
92 if hasattr(name, 'encode'):
93 name = name.encode('ascii')
94 err = _libkmod_h.kmod_module_new_from_name(
95 self._kmod_ctx, name, &mod.module)
97 raise _KmodError('Could not get module')
101 "iterate through currently loaded modules and sizes"
102 for mod in self.loaded():
103 yield (mod.name, mod.size)
105 def modprobe(self, name, quiet=False, *args, **kwargs):
107 Load a module (or alias) and all modules on which it depends.
108 The 'quiet' option defaults to False; set to True to mimic the behavior
109 of the '--quiet' commandline option.
111 mods = list(self.lookup(alias_name=name))
113 if not mods and not quiet:
114 raise _KmodError('Could not modprobe %s' % name)
117 mod.insert(*args, **kwargs)
119 def rmmod(self, module_name, *args, **kwargs):
121 remove module from current tree
122 e.g. km.rmmod("thinkpad_acpi")
124 mod = self.module_from_name(name=module_name)
125 mod.remove(*args, **kwargs)