Use source based routing if secondary interfaces specify different gateway.
[pyplnet.git] / modprobe.py
1 #
2 # $Id$
3 #
4
5 """Modprobe is a utility to read/modify/write /etc/modprobe.conf"""
6
7 import os
8 import tempfile
9
10 class Modprobe:
11     def __init__(self,filename="/etc/modprobe.conf"):
12         self.conffile = {}
13         for keyword in ("include","alias","options","install","remove","blacklist","MODULES"):
14             self.conffile[keyword]={}
15         self.filename = filename
16
17     def input(self,filename=None):
18         if filename==None: filename=self.filename
19
20         # list of file names; loop itself might add filenames
21         filenames = [filename]
22
23         for filename in filenames:
24             if not os.path.exists(filename):
25                 continue
26
27             fb = file(filename,"r")
28             for line in fb.readlines():
29                 def __default():
30                     modulename=parts[1]
31                     rest=" ".join(parts[2:])
32                     self._set(command,modulename,rest)
33
34                 def __alias():
35                     wildcard=parts[1]
36                     modulename=parts[2]
37                     self.aliasset(wildcard,modulename)
38                     options=''
39                     if len(parts)>3:
40                         options=" ".join(parts[3:])
41                         self.optionsset(modulename,options)
42                     self.conffile['MODULES'][modulename]=options
43
44                 def __options():
45                     modulename=parts[1]
46                     rest=" ".join(parts[2:])
47                     self.conffile['MODULES'][modulename]=rest
48                     __default()
49
50                 def __blacklist():
51                     modulename=parts[1]
52                     self.blacklistset(modulename,'')
53
54                 def __include():
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))
60                         else:
61                             filenames.append(newfilename)
62
63                 funcs = {"alias":__alias,
64                          "options":__options,
65                          "blacklis":__blacklist,
66                          "include":__include}
67
68                 parts = line.split()
69
70                 # skip empty lines or those that are comments
71                 if len(parts) == 0 or parts[0] == "#":
72                     continue
73
74                 # lower case first word
75                 command = parts[0].lower()
76
77                 # check if its a command we support
78                 if not self.conffile.has_key(command):
79                     print "WARNING: command %s not recognized." % command
80                     continue
81
82                 func = funcs.get(command,__default)
83                 func()
84             
85             fb.close()
86
87     def _get(self,command,key):
88         return self.conffile[command].get(key,None)
89
90     def _set(self,command,key,value):
91         self.conffile[command][key]=value
92
93     def aliasget(self,key):
94         return self._get('alias',key)
95
96     def optionsget(self,key):
97         return self._get('options',key)
98
99     def blacklistget(self,key):
100         return self._get('blacklist',key)
101
102     def aliasset(self,key,value):
103         self._set("alias",key,value)
104
105     def optionsset(self,key,value):
106         self._set("options",key,value)
107
108     def blacklistset(self,key,value):
109         self._set("blacklist",key,value)
110         
111     def _comparefiles(self,a,b):
112         try:
113             if not os.path.exists(a): return False
114             fb = open(a)
115             buf_a = fb.read()
116             fb.close()
117
118             if not os.path.exists(b): return False
119             fb = open(b)
120             buf_b = fb.read()
121             fb.close()
122
123             return buf_a == buf_b
124         except IOError, e:
125             return False
126
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)
131
132         for command in ("alias","options","install","remove","blacklist"):
133             table = self.conffile[command]
134             keys = table.keys()
135             keys.sort()
136             for k in keys:
137                 v = table[k]
138                 fb.write("%s %s %s\n" % (command,k,v))
139
140         fb.close()
141         if not self._comparefiles(tmpnam,filename):
142             os.rename(tmpnam,filename)
143             os.chmod(filename,0644)
144             return True
145         else:
146             os.unlink(tmpnam)
147             return False
148
149     def probe(self,name):
150         o = os.popen("/sbin/modprobe %s" % name)
151         o.close()
152
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):
159                 ps=os.listdir(path)
160                 parameters={}
161                 for p in ps:
162                     fb = file("%s/%s"%(path,p),"r")
163                     parameters[p]=fb.readline()
164                     fb.close()
165          
166 if __name__ == '__main__':
167     import sys
168     m = Modprobe()
169     if len(sys.argv)>1:
170         fn = sys.argv[1]
171     else:
172         fn = "/etc/modprobe.conf"
173
174     m.input()
175
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)
181
182     m.output("/tmp/%s-tmp"%os.path.basename(fn),"TEST")
183     blacklist.output("/tmp/blacklist-tmp.txt","TEST")