[PATCH][media-player-info] mpi2*: add support for udev's hwdb

Tom Gundersen teg at jklm.no
Thu Jul 18 07:41:06 PDT 2013


udev recently gained a hardware database that is intended to replace large
udev rule files such as the one shipped with media-player-info. This should
give a significant (>50%) speed-up in the processing of usb add events.

This patch adds support for converting mpi to hwdb (where applicable), and
restricts the mpi2udev tool to only output the entries that cannot be
represented in the hwdb format (currently there are only three of them).

Sample udev file: <https://dev.archlinux.org/~tomegun/40-usb-media-players.rules>
Sample hwdb file: <https://dev.archlinux.org/~tomegun/20-usb-media-players.hwdb>

Similar patches were recently submitted to
  gphoto2: <http://sourceforge.net/mailarchive/forum.php?thread_name=CAG-2HqVsNezxM9vh17uiTPQob4S0fWxoVmW%2BCO_iUzmM6SkiHw%40mail.gmail.com&forum_name=gphoto-devel>.
  sane: <http://lists.alioth.debian.org/pipermail/sane-devel/2013-July/031491.html>.
---
 tools/mpi2hwdb.py | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 tools/mpi2udev.py | 52 +++++++++++++++-----------------------
 2 files changed, 94 insertions(+), 32 deletions(-)
 create mode 100755 tools/mpi2hwdb.py

diff --git a/tools/mpi2hwdb.py b/tools/mpi2hwdb.py
new file mode 100755
index 0000000..b7ceb2a
--- /dev/null
+++ b/tools/mpi2hwdb.py
@@ -0,0 +1,74 @@
+#!/usr/bin/python
+# Generate hwdb file from music player identification (.mpi) files
+#
+# (C) 2009 Canonical Ltd.
+# (C) 2013 Tom Gundersen <teg at jklm.no>
+# Author: Martin Pitt <martin.pitt at ubuntu.com>
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+import sys, ConfigParser, os.path
+
+def parse_mpi(mpi):
+    '''Print udev rule for given ConfigParser object.'''
+
+    cp = ConfigParser.RawConfigParser()
+    assert cp.read(mpi)
+
+    try:
+        m = cp.get('Device', 'product')
+        print '#', m
+    except ConfigParser.NoOptionError:
+        pass
+
+    try:
+        usbids = {}
+        for usbid in cp.get('Device', 'devicematch').split(';'):
+            if len(usbid.split(':')) != 3:
+                continue
+            (subsystem, vid, pid) = usbid.split(':')
+            if subsystem != "usb":
+                continue
+            if usbids.has_key(vid):
+                usbids[vid].append(pid)
+            else:
+                usbids[vid] = [ pid ]
+
+        for vid, pids in usbids.iteritems():
+		for pid in pids:
+	            print 'usb:v%sp%s*\n'% (vid.upper(), pid.upper()),
+	            print ' ID_MEDIA_PLAYER=%s\n' % os.path.splitext(os.path.basename(mpi))[0],
+
+                    # do we have an icon?
+                    try:
+                        icon = cp.get('Device', 'icon')
+                        # breaks media player detection : https://bugs.launchpad.net/ubuntu/+source/gvfs/+bug/657609
+                        #print ' UDISKS_PRESENTATION_ICON_NAME=%s\n' % icon,
+                    except ConfigParser.NoOptionError:
+                        pass
+
+                    # terminate rule line
+                    print
+
+    except ConfigParser.NoOptionError:
+        pass
+
+#
+# main
+#
+
+# parse MPI files
+for f in sys.argv[1:]:
+    parse_mpi(f)
diff --git a/tools/mpi2udev.py b/tools/mpi2udev.py
index fa89c2c..f24d355 100755
--- a/tools/mpi2udev.py
+++ b/tools/mpi2udev.py
@@ -2,6 +2,7 @@
 # Generate udev rules from music player identification (.mpi) files
 #
 # (C) 2009 Canonical Ltd.
+# (C) 2013 Tom Gundersen <teg at jklm.no>
 # Author: Martin Pitt <martin.pitt at ubuntu.com>
 #
 # This program is free software; you can redistribute it and/or modify it
@@ -37,47 +38,34 @@ def parse_mpi(mpi):
     assert cp.read(mpi)
 
     try:
-        m = cp.get('Device', 'product')
-        print '#', m
+        cp.get('Device', 'devicematch')
+
     except ConfigParser.NoOptionError:
-        pass
-    for name in ['usbvendor', 'usbproduct', 'usbmodel', 'usbmanufacturer']:
         try:
-            value = cp.get('Device', name)
-            print mpi2udev[name] % value, ',',
+            m = cp.get('Device', 'product')
+            print '#', m
         except ConfigParser.NoOptionError:
-            continue
+            pass
 
-    try:
-        usbids = {}
-        for usbid in cp.get('Device', 'devicematch').split(';'):
-            if len(usbid.split(':')) != 3:
-                continue
-            (subsystem, vid, pid) = usbid.split(':')
-            if subsystem != "usb":
+        for name in ['usbvendor', 'usbproduct', 'usbmodel', 'usbmanufacturer']:
+            try:
+                value = cp.get('Device', name)
+                print mpi2udev[name] % value, ',',
+            except ConfigParser.NoOptionError:
                 continue
-            if usbids.has_key(vid):
-                usbids[vid].append(pid)
-            else:
-                usbids[vid] = [ pid ]
 
-        for vid, pids in usbids.iteritems():
-            print 'ATTRS{idVendor}=="%s" , ATTRS{idProduct}=="%s"'% (vid, '|'.join(pids)), ',',
-            print 'ENV{ID_MEDIA_PLAYER}="%s"' % os.path.splitext(os.path.basename(mpi))[0],
-
-    except ConfigParser.NoOptionError:
         print 'ENV{ID_MEDIA_PLAYER}="%s"' % os.path.splitext(os.path.basename(mpi))[0],
 
-    # do we have an icon?
-    try:
-        icon = cp.get('Device', 'icon')
-        # breaks media player detection : https://bugs.launchpad.net/ubuntu/+source/gvfs/+bug/657609
-        # print ', ENV{UDISKS_PRESENTATION_ICON_NAME}="%s"' % icon,
-    except ConfigParser.NoOptionError:
-        pass
+        # do we have an icon?
+        try:
+            icon = cp.get('Device', 'icon')
+            # breaks media player detection : https://bugs.launchpad.net/ubuntu/+source/gvfs/+bug/657609
+            # print ', ENV{UDISKS_PRESENTATION_ICON_NAME}="%s"' % icon,
+        except ConfigParser.NoOptionError:
+            pass
 
-    # terminate rule line
-    print
+        # terminate rule line
+        print
 
 #
 # main
-- 
1.8.3.3



More information about the devkit-devel mailing list