5 """Modprobe is a utility to read/modify/write /etc/modprobe.conf"""
11 def __init__(self,filename="/etc/modprobe.conf"):
13 for keyword in ("include","alias","options","install","remove","blacklist","MODULES"):
14 self.conffile[keyword]={}
15 self.filename = filename
17 def input(self,filename=None):
18 if filename==None: filename=self.filename
20 # list of file names; loop itself might add filenames
21 filenames = [filename]
23 for filename in filenames:
24 if not os.path.exists(filename):
27 fb = file(filename,"r")
28 for line in fb.readlines():
31 rest=" ".join(parts[2:])
32 self._set(command,modulename,rest)
37 self.aliasset(wildcard,modulename)
40 options=" ".join(parts[3:])
41 self.optionsset(modulename,options)
42 self.conffile['MODULES'][modulename]=options
46 rest=" ".join(parts[2:])
47 self.conffile['MODULES'][modulename]=rest
52 self.blacklistset(modulename,'')
55 newfilename = parts[1]
56 if os.path.exists(newfilename):
57 if os.path.isdir(newfilename):
58 for e in os.listdir(newfilename):
59 filenames.append("%s/%s"%(newfilename,e))
61 filenames.append(newfilename)
63 funcs = {"alias":__alias,
65 "blacklis":__blacklist,
70 # skip empty lines or those that are comments
71 if len(parts) == 0 or parts[0] == "#":
74 # lower case first word
75 command = parts[0].lower()
77 # check if its a command we support
78 if not self.conffile.has_key(command):
79 print "WARNING: command %s not recognized." % command
82 func = funcs.get(command,__default)
87 def _get(self,command,key):
88 return self.conffile[command].get(key,None)
90 def _set(self,command,key,value):
91 self.conffile[command][key]=value
93 def aliasget(self,key):
94 return self._get('alias',key)
96 def optionsget(self,key):
97 return self._get('options',key)
99 def blacklistget(self,key):
100 return self._get('blacklist',key)
102 def aliasset(self,key,value):
103 self._set("alias",key,value)
105 def optionsset(self,key,value):
106 self._set("options",key,value)
108 def blacklistset(self,key,value):
109 self._set("blacklist",key,value)
111 def _comparefiles(self,a,b):
113 if not os.path.exists(a): return False
118 if not os.path.exists(b): return False
123 return buf_a == buf_b
127 def output(self,filename="/etc/modprobe.conf",program="NodeManager"):
128 (fd, tmpnam) = tempfile.mkstemp(dir=os.path.dirname(filename))
129 fb = os.fdopen(fd, "w")
130 fb.write("# Written out by %s\n" % program)
132 for command in ("alias","options","install","remove","blacklist"):
133 table = self.conffile[command]
138 fb.write("%s %s %s\n" % (command,k,v))
141 if not self._comparefiles(tmpnam,filename):
142 os.rename(tmpnam,filename)
143 os.chmod(filename,0644)
149 def probe(self,name):
150 o = os.popen("/sbin/modprobe %s" % name)
153 def checkmodules(self):
154 syspath="/sys/module"
155 modules = os.listdir(syspath)
156 for module in modules:
157 path="%/%s/parameters"%(syspath,module)
158 if os.path.exists(path):
162 fb = file("%s/%s"%(path,p),"r")
163 parameters[p]=fb.readline()
166 if __name__ == '__main__':
172 fn = "/etc/modprobe.conf"
176 blacklist = Modprobe()
177 blacklistfiles = ("blacklist","blacklist-compat","blacklist-firewire")
178 for blf in blacklistfiles:
179 if os.path.exists("/etc/modprobe.d/%s"%blf):
180 blacklist.input("/etc/modprobe.d/%s"%blf)
182 m.output("/tmp/%s-tmp"%os.path.basename(fn),"TEST")
183 blacklist.output("/tmp/blacklist-tmp.txt","TEST")