| 1 |
#!/usr/bin/python3 -sB |
| 2 |
|
| 3 |
## udev-add-printer |
| 4 |
|
| 5 |
## Copyright (C) 2009, 2010, 2014, 2015 Red Hat, Inc. |
| 6 |
## Author: Tim Waugh <[email protected]> |
| 7 |
|
| 8 |
## This program is free software; you can redistribute it and/or modify |
| 9 |
## it under the terms of the GNU General Public License as published by |
| 10 |
## the Free Software Foundation; either version 2 of the License, or |
| 11 |
## (at your option) any later version. |
| 12 |
|
| 13 |
## This program is distributed in the hope that it will be useful, |
| 14 |
## but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
## GNU General Public License for more details. |
| 17 |
|
| 18 |
## You should have received a copy of the GNU General Public License |
| 19 |
## along with this program; if not, write to the Free Software |
| 20 |
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 |
|
| 22 |
import cups |
| 23 |
import cupshelpers |
| 24 |
import dbus |
| 25 |
import os |
| 26 |
import sys |
| 27 |
import traceback |
| 28 |
from syslog import * |
| 29 |
from functools import reduce |
| 30 |
|
| 31 |
MFG_BLACKLIST=[ |
| 32 |
"graphtec", |
| 33 |
] |
| 34 |
|
| 35 |
def create_queue (c, printers, name, device_uri, ppdname, info, installer): |
| 36 |
# Make sure the name is unique. |
| 37 |
namel = str (name.lower ()) |
| 38 |
unique = False |
| 39 |
suffix = 1 |
| 40 |
while not unique: |
| 41 |
unique = True |
| 42 |
for printer in list(printers.values ()): |
| 43 |
if (not printer.discovered and |
| 44 |
((suffix == 1 and printer.name.lower () == namel) or |
| 45 |
(suffix > 1 and |
| 46 |
printer.name.lower () == namel + "-" + str (suffix)))): |
| 47 |
unique = False |
| 48 |
break |
| 49 |
|
| 50 |
if not unique: |
| 51 |
suffix += 1 |
| 52 |
if suffix == 100: |
| 53 |
break |
| 54 |
|
| 55 |
if suffix > 1: |
| 56 |
name += "-" + str (suffix) |
| 57 |
|
| 58 |
c.addPrinter (name, |
| 59 |
device=device_uri, |
| 60 |
ppdname=ppdname, |
| 61 |
info=info, |
| 62 |
location=os.uname ()[1]) |
| 63 |
|
| 64 |
if not installer: |
| 65 |
# There is no session applet running to deal with installing |
| 66 |
# drivers so there is a good chance that this queue won't work |
| 67 |
# right now. If that's the case, delete it. The user can |
| 68 |
# reconnect the printer when they log in, and everything will |
| 69 |
# be set up correctly for them at that point. |
| 70 |
try: |
| 71 |
ppdfile = c.getPPD (name) |
| 72 |
ppd = cups.PPD (ppdfile) |
| 73 |
os.unlink (ppdfile) |
| 74 |
|
| 75 |
(pkgs, exes) = cupshelpers.missingPackagesAndExecutables (ppd) |
| 76 |
if pkgs or exes: |
| 77 |
# There are filters missing. Delete the queue. |
| 78 |
syslog (LOG_ERROR, "PPD %s requires %s" % (ppdname, |
| 79 |
repr ((pkgs, exes)))) |
| 80 |
syslog (LOG_ERROR, "Deleting non-functional queue") |
| 81 |
c.deletePrinter (name) |
| 82 |
name = None |
| 83 |
except cups.IPPError: |
| 84 |
pass |
| 85 |
except RuntimeError: |
| 86 |
pass |
| 87 |
|
| 88 |
if name: |
| 89 |
cupshelpers.activateNewPrinter (c, name) |
| 90 |
|
| 91 |
return name |
| 92 |
|
| 93 |
def add_queue (device_id, device_uris, fax_basename=False): |
| 94 |
""" |
| 95 |
Create a CUPS queue. |
| 96 |
|
| 97 |
device_id: the IEEE 1284 Device ID of the device to add a queue for. |
| 98 |
device_uris: device URIs, best first, for this device |
| 99 |
fax_basename: False if this is not a fax queue, else name prefix |
| 100 |
""" |
| 101 |
|
| 102 |
id_dict = cupshelpers.parseDeviceID (device_id) |
| 103 |
if id_dict["MFG"].lower () in MFG_BLACKLIST: |
| 104 |
syslog (LOG_DEBUG, "Ignoring blacklisted manufacturer %s", id_dict["MFG"]) |
| 105 |
return |
| 106 |
|
| 107 |
syslog (LOG_DEBUG, "add_queue: URIs=%s" % device_uris) |
| 108 |
installer = None |
| 109 |
if fax_basename != False: |
| 110 |
notification = None |
| 111 |
else: |
| 112 |
try: |
| 113 |
bus = dbus.SystemBus () |
| 114 |
obj = bus.get_object ("com.redhat.NewPrinterNotification", |
| 115 |
"/com/redhat/NewPrinterNotification") |
| 116 |
notification = dbus.Interface (obj, |
| 117 |
"com.redhat.NewPrinterNotification") |
| 118 |
notification.GetReady () |
| 119 |
except dbus.DBusException as e: |
| 120 |
syslog (LOG_DEBUG, "D-Bus method call failed: %s" % e) |
| 121 |
notification = None |
| 122 |
|
| 123 |
try: |
| 124 |
obj = bus.get_object ("com.redhat.PrinterDriversInstaller", |
| 125 |
"/com/redhat/PrinterDriversInstaller") |
| 126 |
installer = dbus.Interface (obj, |
| 127 |
"com.redhat.PrinterDriversInstaller") |
| 128 |
except dbus.DBusException as e: |
| 129 |
#syslog (LOG_DEBUG, "Failed to get D-Bus object for " |
| 130 |
# "PrinterDriversInstaller: %s" % e) |
| 131 |
pass |
| 132 |
|
| 133 |
id_dict = cupshelpers.parseDeviceID (device_id) |
| 134 |
if installer: |
| 135 |
cmd = id_dict["CMD"] |
| 136 |
if cmd: |
| 137 |
cmd = reduce (lambda x, y: x + ',' + y, cmd) |
| 138 |
else: |
| 139 |
cmd = "" |
| 140 |
|
| 141 |
try: |
| 142 |
installer.InstallDrivers (id_dict["MFG"], id_dict["MDL"], cmd, |
| 143 |
timeout=3600) |
| 144 |
except dbus.DBusException as e: |
| 145 |
syslog (LOG_DEBUG, "Failed to install drivers: %s" % repr (e)) |
| 146 |
|
| 147 |
c = cups.Connection () |
| 148 |
ppds = cupshelpers.ppds.PPDs (c.getPPDs ()) |
| 149 |
(status, ppdname) = ppds.getPPDNameFromDeviceID (id_dict["MFG"], |
| 150 |
id_dict["MDL"], |
| 151 |
id_dict["DES"], |
| 152 |
id_dict["CMD"], |
| 153 |
device_uris[0]) |
| 154 |
syslog (LOG_DEBUG, "PPD: %s; Status: %d" % (ppdname, status)) |
| 155 |
|
| 156 |
if status == 0: |
| 157 |
# Think of a name for it. |
| 158 |
name = id_dict["MDL"] |
| 159 |
name = name.replace (" ", "-") |
| 160 |
name = name.replace ("/", "-") |
| 161 |
name = name.replace ("#", "-") |
| 162 |
|
| 163 |
if fax_basename != False: |
| 164 |
name = fax_basename + "-" + name |
| 165 |
|
| 166 |
printers = cupshelpers.getPrinters (c) |
| 167 |
uniquename = create_queue (c, printers, name, device_uris[0], ppdname, |
| 168 |
"%s %s" % (id_dict["MFG"], id_dict["MDL"]), |
| 169 |
installer) |
| 170 |
|
| 171 |
if uniquename != None and fax_basename == False: |
| 172 |
# Look for a corresponding fax queue. We can only |
| 173 |
# identify these by looking for device URIs that are the |
| 174 |
# same as this one but with a different scheme. If we |
| 175 |
# find one whose scheme ends in "fax", use that as a fax |
| 176 |
# queue. Note that the HPLIP backends do follow this |
| 177 |
# pattern (hp and hpfax). |
| 178 |
used_uris = [x.device_uri for x in list(printers.values ())] |
| 179 |
for uri in device_uris[1:]: |
| 180 |
if uri.find (":") == -1: |
| 181 |
continue |
| 182 |
|
| 183 |
(scheme, rest) = uri.split (":", 1) |
| 184 |
if scheme.endswith ("fax"): |
| 185 |
# Now see if the non-scheme parts of the URI match |
| 186 |
# any of the URIs we were given. |
| 187 |
for each_uri in device_uris: |
| 188 |
if each_uri == uri: |
| 189 |
continue |
| 190 |
(s, device_uri_rest) = each_uri.split (":", 1) |
| 191 |
if rest == device_uri_rest: |
| 192 |
# This one matches. Check there is not |
| 193 |
# already a queue using this URI. |
| 194 |
if uri in used_uris: |
| 195 |
break |
| 196 |
|
| 197 |
try: |
| 198 |
devices = c.getDevices(include_schemes=[scheme]) |
| 199 |
except TypeError: |
| 200 |
# include_schemes requires pycups 1.9.46 |
| 201 |
devices = c.getDevices () |
| 202 |
|
| 203 |
device_dict = devices.get (uri) |
| 204 |
if device_dict == None: |
| 205 |
break |
| 206 |
|
| 207 |
add_queue (device_dict.get ("device-id", ""), |
| 208 |
[uri], fax_basename=uniquename) |
| 209 |
else: |
| 210 |
# Not an exact match. |
| 211 |
uniquename = device_uris[0] |
| 212 |
|
| 213 |
if uniquename != None and notification: |
| 214 |
try: |
| 215 |
cmd = id_dict["CMD"] |
| 216 |
if cmd: |
| 217 |
cmd = reduce (lambda x, y: x + ',' + y, cmd) |
| 218 |
else: |
| 219 |
cmd = "" |
| 220 |
|
| 221 |
notification.NewPrinter (status, uniquename, id_dict["MFG"], |
| 222 |
id_dict["MDL"], id_dict["DES"], cmd) |
| 223 |
except dbus.DBusException as e: |
| 224 |
syslog (LOG_DEBUG, "D-Bus method call failed: %s" % e) |
| 225 |
|
| 226 |
if len (sys.argv) < 3: |
| 227 |
print("Syntax: %s {Device ID} {Device URI} [other device URIs...]") |
| 228 |
sys.exit (1) |
| 229 |
|
| 230 |
openlog ("udev-add-printer", 0, LOG_LPR) |
| 231 |
try: |
| 232 |
add_queue (sys.argv[1], sys.argv[2:]) |
| 233 |
except SystemExit as e: |
| 234 |
sys.exit (e) |
| 235 |
except: |
| 236 |
(type, value, tb) = sys.exc_info () |
| 237 |
tblast = traceback.extract_tb (tb, limit=None) |
| 238 |
if len (tblast): |
| 239 |
tblast = tblast[:len (tblast) - 1] |
| 240 |
for line in traceback.format_tb (tb): |
| 241 |
syslog (LOG_ERR, line.strip ()) |
| 242 |
extxt = traceback.format_exception_only (type, value) |
| 243 |
syslog (LOG_ERR, extxt[0].strip ()) |