Added tag 0.98 for changeset 7b957b5b0c56
[grml-terminalserver.git] / modcalc.py
1 #!/usr/bin/python2.4
2
3 import sys
4 import os
5
6 def getNicModules():
7     """Get all NIC modules which discover is able to find"""
8     f = open('/lib/discover/pci.lst', 'r')
9     import re
10     ethlinefilter = re.compile('ethernet')
11     modlist = filter(ethlinefilter.search, f)
12     new_modlist = set()
13     for i in modlist:
14         tmplist = i.split('\t', 4)
15         try:
16             type = tmplist[2]
17             mod = tmplist[3]
18         except:
19             continue
20         if type == 'ethernet' and mod != 'unknown':
21             new_modlist.add(mod)
22     f.close()
23     return new_modlist
24
25 def basename(p):
26     """Quite faster implementation of os.path.basename, submitted and accepted"""
27     i = p.rfind('/') + 1
28     return p[i:]
29
30 def generateModDep():
31     """Generate an in-memory represenation of all module dependencies"""
32     moddep = {}
33     moddepfile = open(sys.argv[1], 'r')
34     for i in moddepfile:
35         tmplist = i.split()
36         mainmod = tmplist.pop(0)
37         mainmod = mainmod.rstrip(':')
38         rawmod = mainmod.rstrip('.ko')
39         rawmod = basename(rawmod)
40         newlist = []
41         newlist.append((rawmod, mainmod))
42         for i in tmplist:
43             a = i.rstrip('.ko')
44             a = basename(a)
45             newlist.append((a, i))
46         moddep[rawmod] = newlist
47     moddepfile.close()
48     return moddep
49
50 def generateOutput(modlist, moddep):
51     """Function to filter all keys from moddep not in modlist"""
52     output = {}
53     for i in modlist:
54         try:
55             for (j, k) in moddep[i]:
56                 output[j] = k
57         except:
58             continue
59     return output
60
61 def calculateModuleDep():
62     modlist = getNicModules()
63     moddep = generateModDep()
64     output = generateOutput(modlist, moddep)
65     return output.values()
66
67 #############################################################################
68
69 if __name__ == '__main__':
70     if len(sys.argv) != 2:
71         print 'Error: you should give me the path to your modules.dep'
72         sys.exit(1)
73
74     for i in calculateModuleDep():
75         print i