rewrote modcalc in python
[grml-terminalserver.git] / modcalc.py
1 #!/usr/bin/python2.4
2
3 import sys
4 import os
5 import fileinput
6
7
8 def modulesFromSubprocess():
9     import subprocess
10     return subprocess.Popen('awk "/ethernet/{print \$3}" /lib/discover/pci.lst |sort |uniq',
11             stdout=subprocess.PIPE, shell=True).communicate()[0].splitlines()
12
13 def modulesFromOwnFileinput():
14     modlist = []
15     for i in fileinput.input('/lib/discover/pci.lst'):
16         #i = i.lstrip(' \t')
17         tmplist = i.split('\t', 3)
18         try:
19             type = tmplist[2]
20             mod = tmplist[3]
21         except:
22             continue
23         if type == 'ethernet' and mod != 'unknown':
24             modlist.append(mod)
25     fileinput.close()
26     return modlist
27
28 def modulesFromOwn():
29     modlist = set()
30     f = open('/lib/discover/pci.lst', 'r')
31     for i in f:
32         #i = i.lstrip(' \t')
33         tmplist = i.split('\t', 4)
34         try:
35             type = tmplist[2]
36             mod = tmplist[3]
37         except:
38             continue
39         if type == 'ethernet' and mod != 'unknown':
40             modlist.add(mod)
41     f.close()
42     return modlist
43
44 def modulesFromOwn2():
45     f = open('/lib/discover/pci.lst', 'r')
46     import re
47     ethlinefilter = re.compile('ethernet')
48     modlist = filter(ethlinefilter.search, f)
49     new_modlist = set()
50     for i in modlist:
51         tmplist = i.split('\t', 4)
52         try:
53             type = tmplist[2]
54             mod = tmplist[3]
55         except:
56             continue
57         if type == 'ethernet' and mod != 'unknown':
58             new_modlist.add(mod)
59     f.close()
60     return new_modlist
61
62
63 def basename(p):
64     i = p.rfind('/') + 1
65     return p[i:]
66
67 def osbasename(p):
68     return os.path.split(p)[1]
69
70 def generateModDep_basename():
71     moddep = {}
72     moddepfile = open(sys.argv[1], 'r')
73     for i in moddepfile:
74         tmplist = i.split()
75         mainmod = tmplist.pop(0)
76         mainmod = mainmod.rstrip(':')
77         rawmod = mainmod.rstrip('.ko')
78         rawmod = osbasename(rawmod)
79         newlist = []
80         newlist.append((rawmod, mainmod))
81         for i in tmplist:
82             a = i.rstrip('.ko')
83             a = osbasename(a)
84             newlist.append((a, i))
85         moddep[rawmod] = newlist
86     moddepfile.close()
87     return moddep
88
89 def generateModDep_mybasename():
90     moddep = {}
91     moddepfile = open(sys.argv[1], 'r')
92     for i in moddepfile:
93         tmplist = i.split()
94         mainmod = tmplist.pop(0)
95         mainmod = mainmod.rstrip(':')
96         rawmod = mainmod.rstrip('.ko')
97         rawmod = basename(rawmod)
98         newlist = []
99         newlist.append((rawmod, mainmod))
100         for i in tmplist:
101             a = i.rstrip('.ko')
102             a = basename(a)
103             newlist.append((a, i))
104         moddep[rawmod] = newlist
105     moddepfile.close()
106     return moddep
107
108 def ossplit(p):
109     i = p.rfind('/') + 1
110     head, tail = p[:i], p[i:]
111     if head and head != '/'*len(head):
112         head = head.rstrip('/')
113     return head, tail
114
115 def split(p):
116     i = p.rfind('/') + 1
117     head, tail = p[:i], p[i:]
118     head = head.rstrip('/')
119     if not head:
120         head = '/'
121     return head, tail
122
123 def generateModDep_split():
124     moddep = {}
125     moddepfile = open(sys.argv[1], 'r')
126     for i in moddepfile:
127         tmplist = i.split()
128         mainmod = tmplist.pop(0)
129         mainmod = mainmod.rstrip(':')
130         rawmod = mainmod.rstrip('.ko')
131         (a, rawmod) = ossplit(rawmod)
132         newlist = []
133         newlist.append((rawmod, mainmod))
134         for i in tmplist:
135             a = i.rstrip('.ko')
136             (x, a) = ossplit(a)
137             newlist.append((a, i))
138         moddep[rawmod] = newlist
139     moddepfile.close()
140     return moddep
141
142 def generateModDep_mysplit():
143     moddep = {}
144     moddepfile = open(sys.argv[1], 'r')
145     for i in moddepfile:
146         tmplist = i.split()
147         mainmod = tmplist.pop(0)
148         mainmod = mainmod.rstrip(':')
149         rawmod = mainmod.rstrip('.ko')
150         (a, rawmod) = split(rawmod)
151         newlist = []
152         newlist.append((rawmod, mainmod))
153         for i in tmplist:
154             a = i.rstrip('.ko')
155             (x, a) = split(a)
156             newlist.append((a, i))
157         moddep[rawmod] = newlist
158     moddepfile.close()
159     return moddep
160
161 def generateOutput(modlist, moddep):
162     output = {}
163     for i in modlist:
164         try:
165             for (j, k) in moddep[i]:
166                 output[j] = k
167         except:
168             continue
169     return output
170
171 def calculateModuleDep():
172     #modlist = modulesFromSubprocess()
173     #modlist = modulesFromOwnFileinput()
174     #modlist = modulesFromOwn()
175     modlist = modulesFromOwn2()
176
177     #moddep = generateModDep_basename()
178     #moddep = generateModDep_split()
179     #moddep = generateModDep_mysplit()
180     moddep = generateModDep_mybasename()
181     
182     output = generateOutput(modlist, moddep)
183     return output.values()
184
185 #############################################################################
186
187 if __name__ == '__main__':
188     if len(sys.argv) != 2:
189         print 'Error: you should give me the path to your modules.dep'
190         sys.exit(1)
191
192     for i in calculateModuleDep():
193         print i