[debian-edu-commits] r78727 - in trunk/src/hw-support-handler: . debian hwsupport

pere at alioth.debian.org pere at alioth.debian.org
Sun Jan 13 23:13:24 UTC 2013


Author: pere
Date: 2013-01-13 23:13:24 +0000 (Sun, 13 Jan 2013)
New Revision: 78727

Added:
   trunk/src/hw-support-handler/hwsupport/
   trunk/src/hw-support-handler/hwsupport/__init__.py
   trunk/src/hw-support-handler/hwsupport/lookup.py
Modified:
   trunk/src/hw-support-handler/debian/hw-support-handler.install
   trunk/src/hw-support-handler/hw-support-handlerd
Log:
Move lookup code into separate file.

Modified: trunk/src/hw-support-handler/debian/hw-support-handler.install
===================================================================
--- trunk/src/hw-support-handler/debian/hw-support-handler.install	2013-01-13 17:32:42 UTC (rev 78726)
+++ trunk/src/hw-support-handler/debian/hw-support-handler.install	2013-01-13 23:13:24 UTC (rev 78727)
@@ -1,3 +1,5 @@
 hw-support-handlerd usr/bin
 hw-support-handlerd.desktop usr/share/autostart
 modaliases usr/share/hw-support-handler
+hwsupport/__init__.py usr/lib/python2.6/dist-packages/hwsupport
+hwsupport/lookup.py   usr/lib/python2.6/dist-packages/hwsupport

Modified: trunk/src/hw-support-handler/hw-support-handlerd
===================================================================
--- trunk/src/hw-support-handler/hw-support-handlerd	2013-01-13 17:32:42 UTC (rev 78726)
+++ trunk/src/hw-support-handler/hw-support-handlerd	2013-01-13 23:13:24 UTC (rev 78727)
@@ -3,16 +3,14 @@
 import string
 import dbus
 from dbus.mainloop.glib import DBusGMainLoop
-import pygtk
-import gtk
+#import pygtk
+#import gtk
 import gobject
 import pynotify
 import subprocess
 import glob
 import fnmatch
-import sys
-import apt
-import os
+import hwsupport.lookup
 
 hw_seen = {}
 pkg_suggested = {}
@@ -54,16 +52,16 @@
 def is_pkg_installed(packagename):
     cmd = ["dpkg", "-l", packagename]
     p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+    retval = False
     while True:
         retcode = p.poll()
         line = p.stdout.readline()
+        if 0 == line.find("ii "):
+            retval = True
         if(retcode is not None):
-            line = None
-            return 0 == retcode
-    line = None
-    return False
+            break
+    return retval
 
-
 # While get_pkg_suggestions always return all suggestions, start by
 # igoring those plugged in when we start.
 def ignore_initial_suggestions():
@@ -102,73 +100,22 @@
         f.close()
     return modalias
 
-def modalias_match(alias, candidates):
-    for part in candidates.split(')'):
-        part = part.strip(', ')
-        if not part:
-            continue
-        module, lst = part.split('(')
-        for candidate in lst.split(','):
-            candidate = candidate.strip()
-            bus = candidate.split(':', 1)[0]
-#            print bus, alias, modalias, pkg.name
-            if fnmatch.fnmatch(alias, candidate):
-#                print "Match for %s" % pkg.name
-                return True
-    return False
-
 def get_pkg_suggestions_aptmodaliases(bus, vendor, device):
     print "info: checking apt modaliases info"
     modalias = devid2modalias(bus, vendor, device)
 
     if modalias is None:
         return []
+    return hwsupport.lookup.pkgs_handling_apt_modaliases([modalias])
 
-    cache = apt.Cache()
-    cache.open(None)
-    thepkgs = []
-    for pkg in cache:
-        version = pkg.candidate
-        if version is None:
-            version = pkg.installed
-        if version is None:
-            continue
-        record = version.record
-        if not record.has_key('Modaliases'):
-            continue
-        entries = record['Modaliases']
-#        print pkg.name
-        if modalias_match(modalias, entries):
-            thepkgs.append(pkg.name)
-    return thepkgs
-
 def get_pkg_suggestions_mymodaliases(bus, vendor, device):
     print "info: checking my modaliases file"
     modalias = devid2modalias(bus, vendor, device)
 
     if modalias is None:
         return []
+    return hwsupport.lookup.pkgs_handling_extra_modaliases([modalias])
 
-    thepkgs = []
-    filename = "/usr/share/hw-support-handler/modaliases"
-    try:
-        f = open(filename)
-    except IOError: # No global file, try the local one
-        # FIXME remove this code in production, only for testing
-        filename = "modaliases"
-        f = open(filename)
-    line = f.readline()
-    while line:
-        header, sep, packagename = line.strip().partition(": ")
-        header, sep, modaliases = f.readline().strip().partition(": ")
-        blank = f.readline()
-#            print packagename, modaliases
-        if modalias_match(modalias, modaliases):
-            thepkgs.append(packagename)
-        line = f.readline()
-    f.close()
-    return thepkgs
-
 def get_pkg_suggestions(bus, vendor, device):
     pkgs = []
 #    discoverpkgs = get_pkg_suggestions_discover(bus, vendor, device)
@@ -196,7 +143,7 @@
             print "info: discovered USB device %04x:%04x" % (vendor, device)
             if (vendor, device) not in hw_seen:
                 pkgs = get_pkg_suggestions(bus, vendor, device)
-#                print pkgs
+#                print "Suggestions: ", pkgs
                 newpkg = []
                 for pkg in pkgs:
                     if pkg not in pkg_suggested and not is_pkg_installed(pkg):

Added: trunk/src/hw-support-handler/hwsupport/__init__.py
===================================================================
Added: trunk/src/hw-support-handler/hwsupport/lookup.py
===================================================================
--- trunk/src/hw-support-handler/hwsupport/lookup.py	                        (rev 0)
+++ trunk/src/hw-support-handler/hwsupport/lookup.py	2013-01-13 23:13:24 UTC (rev 78727)
@@ -0,0 +1,92 @@
+#!/usr/bin/python
+
+import sys
+import apt
+import fnmatch
+import urllib
+import subprocess
+
+def my_modaliases():
+    modaliases = []
+    # FIXME Rewrite in python
+    cmd = ["/bin/sh", "-c", "cat $(find /sys -name modalias) | sort -u"]
+    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+    while True:
+        retcode = p.poll()
+        modalias = p.stdout.readline().strip()
+        if modalias != "":
+            modaliases.append(modalias)
+        if(retcode is not None):
+            break
+    return modaliases
+
+def modalias_match(alias, candidates):
+#    print "modalias_match('%s', %s)" % (alias, candidates)
+    for part in candidates.split(')'):
+        part = part.strip(', ')
+        if not part:
+            continue
+        module, lst = part.split('(')
+        for candidate in lst.split(','):
+            candidate = candidate.strip()
+            bus = candidate.split(':', 1)[0]
+#            print bus, alias, candidate
+#            print candidate, alias
+            if fnmatch.fnmatch(alias, candidate):
+#                print "Match"
+                return True
+    return False
+
+def pkgs_handling_apt_modaliases(modaliaslist):
+    cache = apt.Cache()
+    cache.open(None)
+    thepkgs = []
+    for pkg in cache:
+        version = pkg.candidate
+        if version is None:
+            version = pkg.installed
+        if version is None:
+            continue
+        record = version.record
+        if not record.has_key('Modaliaes'):
+            continue
+        modaliases = record['Modaliases']
+        for modalias in modaliaslist:
+            if modalias_match(modalias, modaliases):
+                thepkgs.append(pkg.name)
+    return thepkgs
+
+def pkgs_handling_extra_modaliases(modaliaslist):
+    thepkgs = []
+    url = "http://anonscm.debian.org/viewvc/debian-edu/trunk/src/hw-support-handler/modaliases?view=co"
+    f = urllib.urlopen(url)
+    line = f.readline()
+    while line:
+        header, sep, packagename = line.strip().partition(": ")
+        header, sep, modaliases = f.readline().strip().partition(": ")
+        blank = f.readline()
+        for modalias in modaliaslist:
+#            print modalias, modaliases
+            if modalias_match(modalias, modaliases):
+                thepkgs.append(packagename)
+        line = f.readline()
+    f.close()
+#    print thepkgs
+    return thepkgs
+
+def main():
+    hwaliases = my_modaliases()
+
+    if 1 < len(sys.argv):
+        hwaliases = sys.argv[1]
+
+    print "Locating packages supporting this hardware (APT):"
+    for pkg in pkgs_handling_apt_modaliases(hwaliases):
+        print "  %s" %pkg
+
+    print "Locating packages supporting this hardware (Extra):"
+    for pkg in pkgs_handling_extra_modaliases(hwaliases):
+        print "  %s" %pkg
+
+if __name__ == '__main__':
+    main()




More information about the debian-edu-commits mailing list