~ubuntu-branches/ubuntu/lucid/system-config-printer/lucid-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env python

## check-device-ids

## Copyright (C) 2010 Red Hat, Inc.
## Authors:
##  Tim Waugh <twaugh@redhat.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., 675 Mass Ave, Cambridge, MA 02139, USA.

import dbus
import cups
import cupshelpers
from cupshelpers.ppds import PPDs, ppdMakeModelSplit
import sys

cups.setUser ('root')
c = cups.Connection ()

devices = None
if len (sys.argv) > 1 and sys.argv[1] == '--help':
    print "Syntax: check-device-ids <device-make-and-model> <device-id>"
    sys.exit (1)

if len (sys.argv) == 3:
    id_dict = cupshelpers.parseDeviceID (sys.argv[2])
    if id_dict.has_key ("MFG") and id_dict.has_key ("MDL"):
        devices = { 'user-specified:':
                        { 'device-make-and-model': sys.argv[1],
                          'device-id': sys.argv[2] }
                    }
else:
    print ("\nIf you have not already done so, you may get more results\n"
           "by temporarily disabling your firewall (or by allowing\n"
           "incoming UDP packets on port 161).\n")

if devices == None:
    print "Examining connected devices"
    try:
        devices = c.getDevices (exclude_schemes=["dnssd"])
    except cups.IPPError, (e, m):
        if e == cups.IPP_FORBIDDEN:
            print "Run this as root to examine IDs from attached devices."
            sys.exit (1)

        if e == cups.IPP_NOT_AUTHORIZED:
            print "Not authorized."
            sys.exit (1)

if len (devices) == 0:
    print "No attached devices."
    sys.exit (0)

n = 0
device_ids = []
for device, attrs in devices.iteritems ():
    if device.find (":") == -1:
        continue

    make_and_model = attrs.get ('device-make-and-model')
    device_id = attrs.get ('device-id')
    if make_and_model and not device_id:
        try:
            hostname = None
            if device.startswith ("socket://"):
                hostname = device[9:]
                c = hostname.find (":")
                if c != -1:
                    hostname = hostname[:c]

            if hostname:
                devs = []

                def got_device (dev):
                    if dev != None:
                        devs.append (dev)

                import probe_printer
                pf = probe_printer.PrinterFinder ()
                pf.hostname = hostname
                pf.callback_fn = got_device
                pf._cached_attributes = dict()
                print "Sending SNMP request to %s for device-id" % hostname
                pf._probe_snmp ()

                for dev in devs:
                    if dev.id:
                        device_id = dev.id
                        attrs.update ({'device-id': dev.id})
                        break

        except Exception, e:
            print "Exception: %s" % repr (e)

    if not (make_and_model and device_id):
        print "Skipping %s, insufficient data" % device
        continue

    id_fields = cupshelpers.parseDeviceID (device_id)
    this_id = "MFG:%s;MDL:%s;" % (id_fields['MFG'], id_fields['MDL'])
    device_ids.append (this_id)
    n += 1

if not device_ids:
    print "No Device IDs available."
    sys.exit (0)

try:
    bus = dbus.SessionBus ()

    print "Installing relevant drivers using session service"
    try:
        obj = bus.get_object ("org.freedesktop.PackageKit",
                              "/org/freedesktop/PackageKit")
        proxy = dbus.Interface (obj, "org.freedesktop.PackageKit.Modify")
        proxy.InstallPrinterDrivers (0, device_ids,
                                     "hide-finished", timeout=3600)
    except dbus.exceptions.DBusException, e:
        print "Ignoring exception: %s" % e
except dbus.exceptions.DBusException:
    try:
        bus = dbus.SystemBus ()

        print "Installing relevant drivers using system service"
        try:
            obj = bus.get_object ("com.redhat.PrinterDriversInstaller",
                                  "/com/redhat/PrinterDriversInstaller")
            proxy = dbus.Interface (obj,
                                    "com.redhat.PrinterDriversInstaller")
            for device_id in device_ids:
                id_dict = cupshelpers.parseDeviceID (device_id)
                proxy.InstallDrivers (id_dict['MFG'], id_dict['MDL'], '',
                                      timeout=3600)
        except dbus.exceptions.DBusException, e:
            print "Ignoring exception: %s" % e
    except dbus.exceptions.DBusException:
        print "D-Bus not available so skipping package installation"


print "Fetching driver list"
ppds = PPDs (c.getPPDs ())
ppds._init_ids ()
makes = ppds.getMakes ()

def driver_uri_to_filename (uri):
    schemeparts = uri.split (':', 2)
    if len (schemeparts) < 2:
        if uri.startswith ("lsb/usr/"):
            return "/usr/share/ppd/" + uri[8:]
        elif uri.startswith ("lsb/opt/"):
            return "/opt/share/ppd/" + uri[8:]
        elif uri.startswith ("lsb/local/"):
            return "/usr/local/share/ppd/" + uri[10:]

        return "/usr/share/cups/model/" + uri

    scheme = schemeparts[0]
    if scheme != "drv":
        return "/usr/lib/cups/driver/" + scheme

    rest = schemeparts[1]
    rest = rest.lstrip ('/')
    parts = rest.split ('/')
    if len (parts) > 1:
        parts = parts[:len (parts) - 1]

    return "/usr/share/cups/drv/" + reduce (lambda x, y: x + "/" + y, parts)

def driver_uri_to_pkg (uri):
    filename = driver_uri_to_filename (uri)

    try:
        import packagekit.client, packagekit.enums
        client = packagekit.client.PackageKitClient ()
        packages = client.search_file ([filename],
                                       packagekit.enums.FILTER_INSTALLED)
        return packages[0].name
    except:
        return filename

i = 1
item = unichr (0x251c) + unichr (0x2500) + unichr (0x2500)
last = unichr (0x2514) + unichr (0x2500) + unichr (0x2500)
for device, attrs in devices.iteritems ():
    make_and_model = attrs.get ('device-make-and-model')
    device_id = attrs.get ('device-id')
    if device.find (":") == -1:
        continue

    if not (make_and_model and device_id):
        continue

    id_fields = cupshelpers.parseDeviceID (device_id)
    if i < n:
        line = item
    else:
        line = last

    cmd = id_fields['CMD']
    if cmd:
        cmd = "CMD:%s;" % reduce (lambda x, y: x + ',' + y, cmd)
    else:
        cmd = ""

    scheme = device.split (":", 1)[0]
    print "%s %s (%s): MFG:%s;MDL:%s;%s" % (line, make_and_model,
                                            scheme,
                                            id_fields['MFG'],
                                            id_fields['MDL'],
                                            cmd)
    
    try:
        drivers = ppds.ids[id_fields['MFG'].lower ()][id_fields['MDL'].lower ()]
    except KeyError:
        drivers = []

    if i < n:
        more = unichr (0x2502)
    else:
        more = " "

    if drivers:
        drivers = ppds.orderPPDNamesByPreference (drivers)
        n_drivers = len (drivers)
        j = 1
        for driver in drivers:
            if j < n_drivers:
                print "%s   %s %s [%s]" % (more, item, driver,
                                           driver_uri_to_pkg (driver))
            else:
                print "%s   %s %s [%s]" % (more, last, driver,
                                           driver_uri_to_pkg (driver))

            j += 1
    else:
        print "%s   (No drivers)" % more

    (mfr, mdl) = ppdMakeModelSplit (make_and_model)
    matches = set (ppds.getInfoFromModel (mfr, mdl))
    mfrl = mfr.lower ()
    mdls = None
    for make in makes:
        if make.lower () == mfrl:
            mdls = ppds.makes[make]
            break
    if mdls:
        (s, bestmatches) = ppds._findBestMatchPPDs (mdls, mdl)
        if s == ppds.STATUS_SUCCESS:
            matches = matches.union (set (bestmatches))

    missing = set (matches) - set (drivers)
    for each in missing:
        print "%s       MISSING  %s [%s]" % (more, each,
                                             driver_uri_to_pkg (each))

    i += 1