rewrote modcalc in python
authorMichael Gebetsroither <michael.geb@gmx.at>
Sun, 17 Sep 2006 21:35:52 +0000 (23:35 +0200)
committerMichael Gebetsroither <michael.geb@gmx.at>
Sun, 17 Sep 2006 21:35:52 +0000 (23:35 +0200)
modcalc.py [new file with mode: 0755]
modcalc_speed.py [new file with mode: 0755]
terminalserver/__init__.py
terminalserver/chooser_gui.py

diff --git a/modcalc.py b/modcalc.py
new file mode 100755 (executable)
index 0000000..68c81fc
--- /dev/null
@@ -0,0 +1,193 @@
+#!/usr/bin/python2.4
+
+import sys
+import os
+import fileinput
+
+
+def modulesFromSubprocess():
+    import subprocess
+    return subprocess.Popen('awk "/ethernet/{print \$3}" /lib/discover/pci.lst |sort |uniq',
+            stdout=subprocess.PIPE, shell=True).communicate()[0].splitlines()
+
+def modulesFromOwnFileinput():
+    modlist = []
+    for i in fileinput.input('/lib/discover/pci.lst'):
+        #i = i.lstrip(' \t')
+        tmplist = i.split('\t', 3)
+        try:
+            type = tmplist[2]
+            mod = tmplist[3]
+        except:
+            continue
+        if type == 'ethernet' and mod != 'unknown':
+            modlist.append(mod)
+    fileinput.close()
+    return modlist
+
+def modulesFromOwn():
+    modlist = set()
+    f = open('/lib/discover/pci.lst', 'r')
+    for i in f:
+        #i = i.lstrip(' \t')
+        tmplist = i.split('\t', 4)
+        try:
+            type = tmplist[2]
+            mod = tmplist[3]
+        except:
+            continue
+        if type == 'ethernet' and mod != 'unknown':
+            modlist.add(mod)
+    f.close()
+    return modlist
+
+def modulesFromOwn2():
+    f = open('/lib/discover/pci.lst', 'r')
+    import re
+    ethlinefilter = re.compile('ethernet')
+    modlist = filter(ethlinefilter.search, f)
+    new_modlist = set()
+    for i in modlist:
+        tmplist = i.split('\t', 4)
+        try:
+            type = tmplist[2]
+            mod = tmplist[3]
+        except:
+            continue
+        if type == 'ethernet' and mod != 'unknown':
+            new_modlist.add(mod)
+    f.close()
+    return new_modlist
+
+
+def basename(p):
+    i = p.rfind('/') + 1
+    return p[i:]
+
+def osbasename(p):
+    return os.path.split(p)[1]
+
+def generateModDep_basename():
+    moddep = {}
+    moddepfile = open(sys.argv[1], 'r')
+    for i in moddepfile:
+        tmplist = i.split()
+        mainmod = tmplist.pop(0)
+        mainmod = mainmod.rstrip(':')
+        rawmod = mainmod.rstrip('.ko')
+        rawmod = osbasename(rawmod)
+        newlist = []
+        newlist.append((rawmod, mainmod))
+        for i in tmplist:
+            a = i.rstrip('.ko')
+            a = osbasename(a)
+            newlist.append((a, i))
+        moddep[rawmod] = newlist
+    moddepfile.close()
+    return moddep
+
+def generateModDep_mybasename():
+    moddep = {}
+    moddepfile = open(sys.argv[1], 'r')
+    for i in moddepfile:
+        tmplist = i.split()
+        mainmod = tmplist.pop(0)
+        mainmod = mainmod.rstrip(':')
+        rawmod = mainmod.rstrip('.ko')
+        rawmod = basename(rawmod)
+        newlist = []
+        newlist.append((rawmod, mainmod))
+        for i in tmplist:
+            a = i.rstrip('.ko')
+            a = basename(a)
+            newlist.append((a, i))
+        moddep[rawmod] = newlist
+    moddepfile.close()
+    return moddep
+
+def ossplit(p):
+    i = p.rfind('/') + 1
+    head, tail = p[:i], p[i:]
+    if head and head != '/'*len(head):
+        head = head.rstrip('/')
+    return head, tail
+
+def split(p):
+    i = p.rfind('/') + 1
+    head, tail = p[:i], p[i:]
+    head = head.rstrip('/')
+    if not head:
+        head = '/'
+    return head, tail
+
+def generateModDep_split():
+    moddep = {}
+    moddepfile = open(sys.argv[1], 'r')
+    for i in moddepfile:
+        tmplist = i.split()
+        mainmod = tmplist.pop(0)
+        mainmod = mainmod.rstrip(':')
+        rawmod = mainmod.rstrip('.ko')
+        (a, rawmod) = ossplit(rawmod)
+        newlist = []
+        newlist.append((rawmod, mainmod))
+        for i in tmplist:
+            a = i.rstrip('.ko')
+            (x, a) = ossplit(a)
+            newlist.append((a, i))
+        moddep[rawmod] = newlist
+    moddepfile.close()
+    return moddep
+
+def generateModDep_mysplit():
+    moddep = {}
+    moddepfile = open(sys.argv[1], 'r')
+    for i in moddepfile:
+        tmplist = i.split()
+        mainmod = tmplist.pop(0)
+        mainmod = mainmod.rstrip(':')
+        rawmod = mainmod.rstrip('.ko')
+        (a, rawmod) = split(rawmod)
+        newlist = []
+        newlist.append((rawmod, mainmod))
+        for i in tmplist:
+            a = i.rstrip('.ko')
+            (x, a) = split(a)
+            newlist.append((a, i))
+        moddep[rawmod] = newlist
+    moddepfile.close()
+    return moddep
+
+def generateOutput(modlist, moddep):
+    output = {}
+    for i in modlist:
+        try:
+            for (j, k) in moddep[i]:
+                output[j] = k
+        except:
+            continue
+    return output
+
+def calculateModuleDep():
+    #modlist = modulesFromSubprocess()
+    #modlist = modulesFromOwnFileinput()
+    #modlist = modulesFromOwn()
+    modlist = modulesFromOwn2()
+
+    #moddep = generateModDep_basename()
+    #moddep = generateModDep_split()
+    #moddep = generateModDep_mysplit()
+    moddep = generateModDep_mybasename()
+    
+    output = generateOutput(modlist, moddep)
+    return output.values()
+
+#############################################################################
+
+if __name__ == '__main__':
+    if len(sys.argv) != 2:
+        print 'Error: you should give me the path to your modules.dep'
+        sys.exit(1)
+
+    for i in calculateModuleDep():
+        print i
diff --git a/modcalc_speed.py b/modcalc_speed.py
new file mode 100755 (executable)
index 0000000..54e6fc4
--- /dev/null
@@ -0,0 +1,21 @@
+#!/usr/bin/python2.4
+
+import timeit
+
+from_subprocess = timeit.Timer('modcalc.modulesFromSubprocess()', 'import modcalc')
+
+from_own = timeit.Timer('modcalc.modulesFromOwn()', 'import modcalc')
+from_own_fileinput = timeit.Timer('modcalc.modulesFromOwnFileinput()', 'import modcalc')
+
+def printTime(name, timelist):
+    mintime = min(timelist)
+    print name + ':', mintime, mintime/5
+
+time_sub = from_subprocess.repeat(10, 5)
+printTime('modulesFromSubprocess()', time_sub)
+
+time_own = from_own.repeat(10, 5)
+printTime('modulesFromOwn()', time_own)
+
+time_own_fileinput = from_own_fileinput.repeat(10, 5)
+printTime('modulesFromOwnFileinput()', time_own_fileinput)
index 1d6e689..ed47b97 100644 (file)
@@ -42,7 +42,7 @@ def run(dir):
         print 'Error: no such file or directory, using current directory'
         files = os.listdir('.')
     dir = os.path.abspath(dir)
-    files = [ f for f in files if os.path.isfile(os.path.join(dir, f)) ]
+    files = ( f for f in files if os.path.isfile(os.path.join(dir, f)) )
 
     screen = snack.SnackScreen()
     gui = ChooserGui(screen)