| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
## check-device-ids |
| 4 |
|
| 5 |
## Copyright (C) 2010, 2011, 2012, 2013 Red Hat, Inc. |
| 6 |
## Authors: |
| 7 |
## Tim Waugh <[email protected]> |
| 8 |
|
| 9 |
## This program is free software; you can redistribute it and/or modify |
| 10 |
## it under the terms of the GNU General Public License as published by |
| 11 |
## the Free Software Foundation; either version 2 of the License, or |
| 12 |
## (at your option) any later version. |
| 13 |
|
| 14 |
## This program is distributed in the hope that it will be useful, |
| 15 |
## but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
## GNU General Public License for more details. |
| 18 |
|
| 19 |
## You should have received a copy of the GNU General Public License |
| 20 |
## along with this program; if not, write to the Free Software |
| 21 |
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 22 |
|
| 23 |
import dbus |
| 24 |
import cups |
| 25 |
import cupshelpers |
| 26 |
from cupshelpers.ppds import PPDs, ppdMakeModelSplit |
| 27 |
import sys |
| 28 |
from functools import reduce |
| 29 |
|
| 30 |
c = cups.Connection () |
| 31 |
|
| 32 |
devices = None |
| 33 |
if len (sys.argv) > 1 and sys.argv[1] == '--help': |
| 34 |
print("Syntax: check-device-ids <device-make-and-model> <device-id>") |
| 35 |
print(" or: check-device-ids <device-uri>") |
| 36 |
print(" or: check-device-ids <queue-name>") |
| 37 |
print(" or: check-device-ids") |
| 38 |
sys.exit (1) |
| 39 |
|
| 40 |
SPECIFIC_URI = None |
| 41 |
if len (sys.argv) == 3: |
| 42 |
id_dict = cupshelpers.parseDeviceID (sys.argv[2]) |
| 43 |
if id_dict.get ("MFG") and id_dict.get ("MDL"): |
| 44 |
devices = { 'user-specified:': |
| 45 |
{ 'device-make-and-model': sys.argv[1], |
| 46 |
'device-id': sys.argv[2] } |
| 47 |
} |
| 48 |
elif len (sys.argv) == 2: |
| 49 |
if sys.argv[1].find (":/") != -1: |
| 50 |
SPECIFIC_URI = sys.argv[1] |
| 51 |
else: |
| 52 |
# This is a queue name. Work out the URI from that. |
| 53 |
try: |
| 54 |
attrs = c.getPrinterAttributes (sys.argv[1]) |
| 55 |
except cups.IPPError as e: |
| 56 |
(e, m) = e.args |
| 57 |
print("Error getting printer attibutes: %s" % m) |
| 58 |
sys.exit (1) |
| 59 |
|
| 60 |
SPECIFIC_URI = attrs['device-uri'] |
| 61 |
print("URI for queue %s is %s" % (sys.argv[1], SPECIFIC_URI)) |
| 62 |
else: |
| 63 |
print ("\nIf you have not already done so, you may get more results\n" |
| 64 |
"by temporarily disabling your firewall (or by allowing\n" |
| 65 |
"incoming UDP packets on port 161).\n") |
| 66 |
|
| 67 |
if devices is None: |
| 68 |
if not SPECIFIC_URI: |
| 69 |
print("Examining connected devices") |
| 70 |
|
| 71 |
cups.setUser ('root') |
| 72 |
c = cups.Connection () |
| 73 |
|
| 74 |
try: |
| 75 |
if SPECIFIC_URI: |
| 76 |
scheme = str (SPECIFIC_URI.split (":", 1)[0]) |
| 77 |
devices = c.getDevices (include_schemes=[scheme]) |
| 78 |
else: |
| 79 |
devices = c.getDevices (exclude_schemes=["dnssd", "hal", "hpfax"]) |
| 80 |
except cups.IPPError as e: |
| 81 |
(e, m) = e.args |
| 82 |
if e == cups.IPP_FORBIDDEN: |
| 83 |
print("Run this as root to examine IDs from attached devices.") |
| 84 |
sys.exit (1) |
| 85 |
if e in (cups.IPP_NOT_AUTHORIZED, cups.IPP_AUTHENTICATION_CANCELED): |
| 86 |
print("Not authorized.") |
| 87 |
sys.exit (1) |
| 88 |
|
| 89 |
if SPECIFIC_URI: |
| 90 |
if devices.get (SPECIFIC_URI) is None: |
| 91 |
devices = { SPECIFIC_URI: |
| 92 |
{ 'device-make-and-model': '', |
| 93 |
'device-id': ''} } |
| 94 |
if len (devices) == 0: |
| 95 |
print("No attached devices.") |
| 96 |
sys.exit (0) |
| 97 |
|
| 98 |
n = 0 |
| 99 |
device_ids = [] |
| 100 |
for device, attrs in devices.items (): |
| 101 |
if device.find (":") == -1: |
| 102 |
continue |
| 103 |
|
| 104 |
if SPECIFIC_URI and device != SPECIFIC_URI: |
| 105 |
continue |
| 106 |
|
| 107 |
make_and_model = attrs.get ('device-make-and-model') |
| 108 |
device_id = attrs.get ('device-id') |
| 109 |
if (SPECIFIC_URI or make_and_model) and not device_id: |
| 110 |
try: |
| 111 |
hostname = None |
| 112 |
if (device.startswith ("socket://") or |
| 113 |
device.startswith ("lpd://") or |
| 114 |
device.startswith ("ipp://") or |
| 115 |
device.startswith ("http://") or |
| 116 |
device.startswith ("https://")): |
| 117 |
hostname = device[device.find ("://") + 3:] |
| 118 |
colon = hostname.find (":") |
| 119 |
if colon != -1: |
| 120 |
hostname = hostname[:colon] |
| 121 |
|
| 122 |
if hostname: |
| 123 |
devs = [] |
| 124 |
|
| 125 |
def got_device (dev): |
| 126 |
if dev is not None: |
| 127 |
devs.append (dev) |
| 128 |
|
| 129 |
import probe_printer |
| 130 |
pf = probe_printer.PrinterFinder () |
| 131 |
pf.hostname = hostname |
| 132 |
pf.callback_fn = got_device |
| 133 |
pf._cached_attributes = dict() |
| 134 |
print("Sending SNMP request to %s for device-id" % hostname) |
| 135 |
pf._probe_snmp () |
| 136 |
|
| 137 |
for dev in devs: |
| 138 |
if dev.id: |
| 139 |
device_id = dev.id |
| 140 |
attrs.update ({'device-id': dev.id}) |
| 141 |
|
| 142 |
if not make_and_model and dev.make_and_model: |
| 143 |
make_and_model = dev.make_and_model |
| 144 |
attrs.update ({'device-make-and-model': |
| 145 |
dev.make_and_model}) |
| 146 |
|
| 147 |
except Exception as e: |
| 148 |
print("Exception: %s" % repr (e)) |
| 149 |
|
| 150 |
if not (make_and_model and device_id): |
| 151 |
print("Skipping %s, insufficient data" % device) |
| 152 |
continue |
| 153 |
|
| 154 |
id_fields = cupshelpers.parseDeviceID (device_id) |
| 155 |
this_id = "MFG:%s;MDL:%s;" % (id_fields['MFG'], id_fields['MDL']) |
| 156 |
device_ids.append (this_id) |
| 157 |
n += 1 |
| 158 |
|
| 159 |
if not device_ids: |
| 160 |
print("No Device IDs available.") |
| 161 |
sys.exit (0) |
| 162 |
|
| 163 |
try: |
| 164 |
bus = dbus.SessionBus () |
| 165 |
|
| 166 |
print("Installing relevant drivers using session service") |
| 167 |
try: |
| 168 |
obj = bus.get_object ("org.freedesktop.PackageKit", |
| 169 |
"/org/freedesktop/PackageKit") |
| 170 |
proxy = dbus.Interface (obj, "org.freedesktop.PackageKit.Modify") |
| 171 |
proxy.InstallPrinterDrivers (0, device_ids, |
| 172 |
"hide-finished", timeout=3600) |
| 173 |
except dbus.exceptions.DBusException as e: |
| 174 |
print("Ignoring exception: %s" % e) |
| 175 |
except dbus.exceptions.DBusException: |
| 176 |
try: |
| 177 |
bus = dbus.SystemBus () |
| 178 |
|
| 179 |
print("Installing relevant drivers using system service") |
| 180 |
try: |
| 181 |
obj = bus.get_object ("com.redhat.PrinterDriversInstaller", |
| 182 |
"/com/redhat/PrinterDriversInstaller") |
| 183 |
proxy = dbus.Interface (obj, |
| 184 |
"com.redhat.PrinterDriversInstaller") |
| 185 |
for device_id in device_ids: |
| 186 |
id_dict = cupshelpers.parseDeviceID (device_id) |
| 187 |
proxy.InstallDrivers (id_dict['MFG'], id_dict['MDL'], '', |
| 188 |
timeout=3600) |
| 189 |
except dbus.exceptions.DBusException as e: |
| 190 |
print("Ignoring exception: %s" % e) |
| 191 |
except dbus.exceptions.DBusException: |
| 192 |
print("D-Bus not available so skipping package installation") |
| 193 |
|
| 194 |
|
| 195 |
print("Fetching driver list") |
| 196 |
ppds = PPDs (c.getPPDs ()) |
| 197 |
ppds._init_ids () |
| 198 |
makes = ppds.getMakes () |
| 199 |
|
| 200 |
def driver_uri_to_filename (uri): |
| 201 |
schemeparts = uri.split (':', 2) |
| 202 |
if len (schemeparts) < 2: |
| 203 |
if uri.startswith ("lsb/usr/"): |
| 204 |
return "/usr/share/ppd/" + uri[8:] |
| 205 |
elif uri.startswith ("lsb/opt/"): |
| 206 |
return "/opt/share/ppd/" + uri[8:] |
| 207 |
elif uri.startswith ("lsb/local/"): |
| 208 |
return "/usr/local/share/ppd/" + uri[10:] |
| 209 |
|
| 210 |
return "/usr/share/cups/model/" + uri |
| 211 |
|
| 212 |
scheme = schemeparts[0] |
| 213 |
if scheme != "drv": |
| 214 |
return "/usr/lib/cups/driver/" + scheme |
| 215 |
|
| 216 |
rest = schemeparts[1] |
| 217 |
rest = rest.lstrip ('/') |
| 218 |
parts = rest.split ('/') |
| 219 |
if len (parts) > 1: |
| 220 |
parts = parts[:len (parts) - 1] |
| 221 |
|
| 222 |
return "/usr/share/cups/drv/" + reduce (lambda x, y: x + "/" + y, parts) |
| 223 |
|
| 224 |
def driver_uri_to_pkg (uri): |
| 225 |
filename = driver_uri_to_filename (uri) |
| 226 |
|
| 227 |
try: |
| 228 |
import packagekit.client, packagekit.enums |
| 229 |
client = packagekit.client.PackageKitClient () |
| 230 |
packages = client.search_file ([filename], |
| 231 |
packagekit.enums.FILTER_INSTALLED) |
| 232 |
return packages[0].name |
| 233 |
except: |
| 234 |
return filename |
| 235 |
|
| 236 |
i = 1 |
| 237 |
if sys.stdout.encoding == 'UTF-8': |
| 238 |
item = chr (0x251c) + chr (0x2500) + chr (0x2500) |
| 239 |
last = chr (0x2514) + chr (0x2500) + chr (0x2500) |
| 240 |
else: |
| 241 |
item = "|--" |
| 242 |
last = "`--" |
| 243 |
|
| 244 |
for device, attrs in devices.items (): |
| 245 |
make_and_model = attrs.get ('device-make-and-model') |
| 246 |
device_id = attrs.get ('device-id') |
| 247 |
if device.find (":") == -1: |
| 248 |
continue |
| 249 |
|
| 250 |
if not (make_and_model and device_id): |
| 251 |
continue |
| 252 |
|
| 253 |
id_fields = cupshelpers.parseDeviceID (device_id) |
| 254 |
if i < n: |
| 255 |
line = item |
| 256 |
else: |
| 257 |
line = last |
| 258 |
|
| 259 |
cmd = id_fields['CMD'] |
| 260 |
if cmd: |
| 261 |
cmd = "CMD:%s;" % reduce (lambda x, y: x + ',' + y, cmd) |
| 262 |
else: |
| 263 |
cmd = "" |
| 264 |
|
| 265 |
scheme = device.split (":", 1)[0] |
| 266 |
print("%s %s (%s): MFG:%s;MDL:%s;%s" % (line, make_and_model, |
| 267 |
scheme, |
| 268 |
id_fields['MFG'], |
| 269 |
id_fields['MDL'], |
| 270 |
cmd)) |
| 271 |
|
| 272 |
try: |
| 273 |
drivers = ppds.ids[id_fields['MFG'].lower ()][id_fields['MDL'].lower ()] |
| 274 |
except KeyError: |
| 275 |
drivers = [] |
| 276 |
|
| 277 |
if i < n: |
| 278 |
more = chr (0x2502) |
| 279 |
else: |
| 280 |
more = " " |
| 281 |
|
| 282 |
if drivers: |
| 283 |
drivers = ppds.orderPPDNamesByPreference (drivers) |
| 284 |
n_drivers = len (drivers) |
| 285 |
j = 1 |
| 286 |
for driver in drivers: |
| 287 |
if j < n_drivers: |
| 288 |
print("%s %s %s [%s]" % (more, item, driver, |
| 289 |
driver_uri_to_pkg (driver))) |
| 290 |
else: |
| 291 |
print("%s %s %s [%s]" % (more, last, driver, |
| 292 |
driver_uri_to_pkg (driver))) |
| 293 |
|
| 294 |
j += 1 |
| 295 |
else: |
| 296 |
print("%s (No drivers)" % more) |
| 297 |
|
| 298 |
(mfr, mdl) = ppdMakeModelSplit (make_and_model) |
| 299 |
matches = set (ppds.getInfoFromModel (mfr, mdl)) |
| 300 |
mfrl = mfr.lower () |
| 301 |
mdls = None |
| 302 |
for make in makes: |
| 303 |
if make.lower () == mfrl: |
| 304 |
mdls = ppds.makes[make] |
| 305 |
break |
| 306 |
if mdls: |
| 307 |
(s, bestmatches) = ppds._findBestMatchPPDs (mdls, mdl) |
| 308 |
if s == ppds.FIT_EXACT: |
| 309 |
matches = matches.union (set (bestmatches)) |
| 310 |
|
| 311 |
missing = set (matches) - set (drivers) |
| 312 |
for each in missing: |
| 313 |
try: |
| 314 |
ppd_device_id = ppds.getInfoFromPPDName (each).get ('ppd-device-id') |
| 315 |
except Exception as e: |
| 316 |
print(e) |
| 317 |
ppd_device_id = None |
| 318 |
|
| 319 |
if ppd_device_id: |
| 320 |
ppd_id_fields = cupshelpers.parseDeviceID (ppd_device_id) |
| 321 |
else: |
| 322 |
ppd_id_fields = {} |
| 323 |
|
| 324 |
if ppd_id_fields.get ("MFG") and ppd_id_fields.get ("MDL"): |
| 325 |
print("%s WRONG %s [%s]" % (more, each, |
| 326 |
driver_uri_to_pkg (each))) |
| 327 |
for field in ["MFG", "MDL"]: |
| 328 |
value = id_fields[field] |
| 329 |
ppd_value = ppd_id_fields[field] |
| 330 |
if value.lower () != ppd_value.lower (): |
| 331 |
print("%s %s:%s;" % (more, field, ppd_value)) |
| 332 |
print("%s should be:%s;" % (more, value)) |
| 333 |
else: |
| 334 |
print("%s MISSING %s [%s]" % (more, each, |
| 335 |
driver_uri_to_pkg (each))) |
| 336 |
|
| 337 |
i += 1 |