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