| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
## Copyright (C) 2008, 2009, 2010, 2012, 2014 Red Hat, Inc. |
| 4 |
## Authors: |
| 5 |
## Tim Waugh <[email protected]> |
| 6 |
|
| 7 |
## This program is free software; you can redistribute it and/or modify |
| 8 |
## it under the terms of the GNU General Public License as published by |
| 9 |
## the Free Software Foundation; either version 2 of the License, or |
| 10 |
## (at your option) any later version. |
| 11 |
|
| 12 |
## This program is distributed in the hope that it will be useful, |
| 13 |
## but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
## GNU General Public License for more details. |
| 16 |
|
| 17 |
## You should have received a copy of the GNU General Public License |
| 18 |
## along with this program; if not, write to the Free Software |
| 19 |
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 |
|
| 21 |
import config |
| 22 |
import gettext |
| 23 |
gettext.install(domain=config.PACKAGE, localedir=config.localedir) |
| 24 |
import cupshelpers |
| 25 |
import urllib.parse |
| 26 |
import ppdippstr |
| 27 |
import socket |
| 28 |
from debug import * |
| 29 |
|
| 30 |
class PhysicalDevice: |
| 31 |
def __init__(self, device): |
| 32 |
self.devices = None |
| 33 |
self._network_host = None |
| 34 |
self.dnssd_hostname = None |
| 35 |
self._cupsserver = False |
| 36 |
self.firsturi = None |
| 37 |
self.add_device (device) |
| 38 |
self._user_data = {} |
| 39 |
self._ppdippstr = ppdippstr.backends |
| 40 |
|
| 41 |
def _canonical_id (self, device): |
| 42 |
if hasattr (device, "id_dict"): |
| 43 |
mfg = device.id_dict.get ('MFG', '') |
| 44 |
mdl = device.id_dict.get ('MDL', '') |
| 45 |
|
| 46 |
if mfg == '' or mdl.lower ().startswith (mfg.lower ()): |
| 47 |
make_and_model = mdl |
| 48 |
else: |
| 49 |
make_and_model = "%s %s" % (mfg, mdl) |
| 50 |
else: |
| 51 |
make_and_model = device.make_and_model |
| 52 |
|
| 53 |
return cupshelpers.ppds.ppdMakeModelSplit (make_and_model) |
| 54 |
|
| 55 |
def _add_dot_local_if_needed(self, hostname): |
| 56 |
if (hostname is None): |
| 57 |
return None |
| 58 |
if ((not '.' in hostname) and (not ':' in hostname) and |
| 59 |
(hostname != 'localhost')): |
| 60 |
return hostname + '.local' |
| 61 |
else: |
| 62 |
return hostname |
| 63 |
|
| 64 |
def _get_address (self, hostname): |
| 65 |
try: |
| 66 |
address = socket.getaddrinfo(hostname, 0, |
| 67 |
family=socket.AF_INET)[0][4][0] |
| 68 |
except: |
| 69 |
try: |
| 70 |
address = socket.getaddrinfo(hostname, 0, |
| 71 |
family=socket.AF_INET6)[0][4][0] |
| 72 |
except: |
| 73 |
address = None |
| 74 |
return address |
| 75 |
|
| 76 |
def _get_host_from_uri (self, uri): |
| 77 |
hostport = None |
| 78 |
host = None |
| 79 |
dnssdhost = None |
| 80 |
(scheme, rest) = urllib.parse.splittype (uri) |
| 81 |
if scheme == 'hp' or scheme == 'hpfax': |
| 82 |
ipparam = None |
| 83 |
if rest.startswith ("/net/"): |
| 84 |
(rest, ipparam) = urllib.parse.splitquery (rest[5:]) |
| 85 |
|
| 86 |
if ipparam is not None: |
| 87 |
if ipparam.startswith("ip="): |
| 88 |
hostport = ipparam[3:] |
| 89 |
elif ipparam.startswith ("hostname="): |
| 90 |
hostport = ipparam[9:] |
| 91 |
elif ipparam.startswith("zc="): |
| 92 |
dnssdhost = ipparam[3:] |
| 93 |
else: |
| 94 |
return None, None |
| 95 |
else: |
| 96 |
return None, None |
| 97 |
elif scheme == 'dnssd' or scheme == 'mdns': |
| 98 |
# The URIs of the CUPS "dnssd" backend do not contain the host |
| 99 |
# name of the printer |
| 100 |
return None, None |
| 101 |
else: |
| 102 |
(hostport, rest) = urllib.parse.splithost (rest) |
| 103 |
if hostport is None: |
| 104 |
return None, None |
| 105 |
|
| 106 |
if hostport: |
| 107 |
(host, port) = urllib.parse.splitport (hostport) |
| 108 |
|
| 109 |
if (host): |
| 110 |
ip = None |
| 111 |
try: |
| 112 |
ip = self._get_address(host) |
| 113 |
if ip: |
| 114 |
host = ip |
| 115 |
except: |
| 116 |
pass |
| 117 |
elif (dnssdhost): |
| 118 |
try: |
| 119 |
host = self._get_address(dnssdhost) |
| 120 |
except: |
| 121 |
host = None |
| 122 |
|
| 123 |
return self._add_dot_local_if_needed(host), \ |
| 124 |
self._add_dot_local_if_needed(dnssdhost) |
| 125 |
|
| 126 |
def add_device (self, device): |
| 127 |
if self._network_host or self.dnssd_hostname: |
| 128 |
host, dnssdhost = self._get_host_from_uri (device.uri) |
| 129 |
if (hasattr (device, 'address')): |
| 130 |
host = self._add_dot_local_if_needed(device.address) |
| 131 |
if (hasattr (device, 'hostname') and dnssdhost is None): |
| 132 |
dnssdhost = self._add_dot_local_if_needed(device.hostname) |
| 133 |
if (host is None and dnssdhost is None) or \ |
| 134 |
not ((host and self._network_host and |
| 135 |
host == self._network_host) or |
| 136 |
(host and self.dnssd_hostname and |
| 137 |
host == self.dnssd_hostname) or |
| 138 |
(dnssdhost and self._network_host and |
| 139 |
dnssdhost == self._network_host) or |
| 140 |
(dnssdhost and self.dnssd_hostname and |
| 141 |
dnssdhost == self.dnssd_hostname)) or \ |
| 142 |
(host is None and self.dnssd_hostname is None) or \ |
| 143 |
(dnssdhost is None and self._network_host is None): |
| 144 |
raise ValueError |
| 145 |
else: |
| 146 |
(mfg, mdl) = self._canonical_id (device) |
| 147 |
if self.devices is None: |
| 148 |
self.mfg = mfg |
| 149 |
self.mdl = mdl |
| 150 |
self.mfg_lower = mfg.lower () |
| 151 |
self.mdl_lower = mdl.lower () |
| 152 |
self.sn = device.id_dict.get ('SN', '') |
| 153 |
self.devices = [] |
| 154 |
else: |
| 155 |
def nicest (a, b): |
| 156 |
def count_lower (s): |
| 157 |
l = s.lower () |
| 158 |
n = 0 |
| 159 |
for i in range (len (s)): |
| 160 |
if l[i] != s[i]: |
| 161 |
n += 1 |
| 162 |
return n |
| 163 |
if count_lower (b) < count_lower (a): |
| 164 |
return b |
| 165 |
return a |
| 166 |
|
| 167 |
self.mfg = nicest (self.mfg, mfg) |
| 168 |
self.mdl = nicest (self.mdl, mdl) |
| 169 |
|
| 170 |
sn = device.id_dict.get ('SN', '') |
| 171 |
if sn != '' and self.sn != '' and sn != self.sn: |
| 172 |
raise ValueError |
| 173 |
|
| 174 |
if device.type == "socket": |
| 175 |
# Remove default port to more easily find duplicate URIs |
| 176 |
device.uri = device.uri.replace (":9100", "") |
| 177 |
if (device.uri.startswith('ipp:') and \ |
| 178 |
device.uri.find('/printers/') != -1) or \ |
| 179 |
((device.uri.startswith('dnssd:') or \ |
| 180 |
device.uri.startswith('mdns:')) and \ |
| 181 |
device.uri.endswith('/cups')): |
| 182 |
# CUPS server |
| 183 |
self._cupsserver = True |
| 184 |
elif self._cupsserver: |
| 185 |
# Non-CUPS queue on a CUPS server, drop this one |
| 186 |
return |
| 187 |
for d in self.devices: |
| 188 |
if d.uri == device.uri: |
| 189 |
return |
| 190 |
|
| 191 |
# Use the URI of the very first device added as a kind of identifier |
| 192 |
# for this physical device record, to make debugging easier |
| 193 |
if not self.firsturi: |
| 194 |
self.firsturi = device.uri; |
| 195 |
|
| 196 |
self.devices.append (device) |
| 197 |
self.devices.sort () |
| 198 |
|
| 199 |
if (not self._network_host or not self.dnssd_hostname) and \ |
| 200 |
device.device_class == "network": |
| 201 |
# We just added a network device. |
| 202 |
self._network_host, dnssdhost = \ |
| 203 |
self._get_host_from_uri (device.uri) |
| 204 |
if dnssdhost: |
| 205 |
self.dnssd_hostname = dnssdhost; |
| 206 |
|
| 207 |
if (hasattr (device, 'address') and self._network_host is None): |
| 208 |
address = device.address |
| 209 |
if address: |
| 210 |
self._network_host = self._add_dot_local_if_needed(address) |
| 211 |
|
| 212 |
if (hasattr (device, 'hostname') and self.dnssd_hostname is None): |
| 213 |
hostname = device.hostname |
| 214 |
if hostname: |
| 215 |
self.dnssd_hostname = self._add_dot_local_if_needed(hostname) |
| 216 |
|
| 217 |
if (self.dnssd_hostname and self._network_host is None): |
| 218 |
try: |
| 219 |
self._network_host = self._get_address(hostname); |
| 220 |
except: |
| 221 |
self._network_host = None |
| 222 |
|
| 223 |
debugprint("Device %s added to physical device: %s" % |
| 224 |
(device.uri, repr(self))) |
| 225 |
|
| 226 |
def get_devices (self): |
| 227 |
return self.devices |
| 228 |
|
| 229 |
def get_info (self): |
| 230 |
# If the manufacturer/model is not known, or useless (in the |
| 231 |
# case of the hpfax backend or a dnssd URI pointing to a remote |
| 232 |
# CUPS queue), show the device-info field instead. |
| 233 |
if (self.devices[0].uri.startswith('ipp:') and \ |
| 234 |
self.devices[0].uri.find('/printers/') != -1) or \ |
| 235 |
((self.devices[0].uri.startswith('dnssd:') or \ |
| 236 |
self.devices[0].uri.startswith('mdns:')) and \ |
| 237 |
self.devices[0].uri.endswith('/cups')): |
| 238 |
if not self.dnssd_hostname: |
| 239 |
info = "%s" % self._network_host |
| 240 |
elif not self._network_host or self._network_host.find(":") != -1: |
| 241 |
info = "%s" % self.dnssd_hostname |
| 242 |
else: |
| 243 |
if self._network_host != self.dnssd_hostname: |
| 244 |
info = "%s (%s)" % (self.dnssd_hostname, self._network_host) |
| 245 |
else: |
| 246 |
info = "%s" % self._network_host |
| 247 |
elif self.mfg == '' or \ |
| 248 |
(self.mfg == "HP" and self.mdl.startswith("Fax")): |
| 249 |
info = self._ppdippstr.get (self.devices[0].info) |
| 250 |
else: |
| 251 |
info = "%s %s" % (self.mfg, self.mdl) |
| 252 |
if ((self._network_host and len (self._network_host) > 0) or \ |
| 253 |
(self.dnssd_hostname and len (self.dnssd_hostname) > 0)) and not \ |
| 254 |
((self.devices[0].uri.startswith('dnssd:') or \ |
| 255 |
self.devices[0].uri.startswith('mdns:')) and \ |
| 256 |
self.devices[0].uri.endswith('/cups')) and \ |
| 257 |
(not self._network_host or \ |
| 258 |
info.find(self._network_host) == -1) and \ |
| 259 |
(not self.dnssd_hostname or \ |
| 260 |
info.find(self.dnssd_hostname) == -1): |
| 261 |
if not self.dnssd_hostname: |
| 262 |
info += " (%s)" % self._network_host |
| 263 |
elif not self._network_host: |
| 264 |
info += " (%s)" % self.dnssd_hostname |
| 265 |
else: |
| 266 |
info += " (%s, %s)" % (self.dnssd_hostname, self._network_host) |
| 267 |
elif len (self.sn) > 0: |
| 268 |
info += " (%s)" % self.sn |
| 269 |
return info |
| 270 |
|
| 271 |
# User data |
| 272 |
def set_data (self, key, value): |
| 273 |
self._user_data[key] = value |
| 274 |
|
| 275 |
def get_data (self, key): |
| 276 |
return self._user_data.get (key) |
| 277 |
|
| 278 |
def __str__ (self): |
| 279 |
return "(description: %s)" % self.__repr__ () |
| 280 |
|
| 281 |
def __repr__ (self): |
| 282 |
return ("<PhysicalDevice.PhysicalDevice (%s,%s,%s,%s,%s,%s)>" % |
| 283 |
(self.mfg, self.mdl, self.sn, self._network_host, |
| 284 |
self.dnssd_hostname, self.firsturi)) |
| 285 |
|
| 286 |
def __eq__(self, other): |
| 287 |
if type (other) != type (self): |
| 288 |
return False |
| 289 |
|
| 290 |
if not (((not self._network_host or len (self._network_host) == 0) and |
| 291 |
(not other._network_host or len (other._network_host) == 0) and |
| 292 |
(not self.dnssd_hostname or len (self.dnssd_hostname) == 0) and |
| 293 |
(not other.dnssd_hostname or |
| 294 |
len (other.dnssd_hostname) == 0)) or |
| 295 |
(self._network_host and len (self._network_host) > 0 and |
| 296 |
other._network_host and len (other._network_host) > 0 and |
| 297 |
self._network_host == other._network_host) or |
| 298 |
(self.dnssd_hostname and len (self.dnssd_hostname) > 0 and |
| 299 |
other.dnssd_hostname and len (other.dnssd_hostname) > 0 and |
| 300 |
self.dnssd_hostname == other.dnssd_hostname) or |
| 301 |
(self._network_host and len (self._network_host) > 0 and |
| 302 |
other.dnssd_hostname and len (other.dnssd_hostname) > 0 and |
| 303 |
self._network_host == other.dnssd_hostname) or |
| 304 |
(self.dnssd_hostname and len (self.dnssd_hostname) > 0 and |
| 305 |
other._network_host and len (other._network_host) > 0 and |
| 306 |
self.dnssd_hostname == other._network_host)): |
| 307 |
return False |
| 308 |
|
| 309 |
devs = other.get_devices() |
| 310 |
if devs: |
| 311 |
uris = [x.uri for x in self.devices] |
| 312 |
for dev in devs: |
| 313 |
if dev.uri in uris: |
| 314 |
# URI match |
| 315 |
return True |
| 316 |
|
| 317 |
if ((other.mfg == '' and other.mdl == '') or |
| 318 |
(self.mfg == '' and self.mdl == '')): |
| 319 |
if other.mfg == '' and self.mfg == '': |
| 320 |
# Both just a backend, not a real physical device. |
| 321 |
return self.devices[0] == other.devices[0] |
| 322 |
|
| 323 |
# One or other is just a backend, not a real physical device. |
| 324 |
return False |
| 325 |
|
| 326 |
def split_make_and_model (dev): |
| 327 |
if dev.mfg == '' or dev.mdl.lower ().startswith (dev.mfg.lower ()): |
| 328 |
make_and_model = dev.mdl |
| 329 |
else: |
| 330 |
make_and_model = "%s %s" % (dev.mfg, dev.mdl) |
| 331 |
(mfg, mdl) = cupshelpers.ppds.ppdMakeModelSplit (make_and_model) |
| 332 |
return (cupshelpers.ppds.normalize (mfg), |
| 333 |
cupshelpers.ppds.normalize (mdl)) |
| 334 |
|
| 335 |
(our_mfg, our_mdl) = split_make_and_model (self) |
| 336 |
(other_mfg, other_mdl) = split_make_and_model (other) |
| 337 |
|
| 338 |
if our_mfg != other_mfg: |
| 339 |
return False |
| 340 |
|
| 341 |
if our_mfg == "hp" and self.sn != '' and self.sn == other.sn: |
| 342 |
return True |
| 343 |
|
| 344 |
if our_mdl != other_mdl: |
| 345 |
return False |
| 346 |
|
| 347 |
if self.sn == '' or other.sn == '': |
| 348 |
return True |
| 349 |
|
| 350 |
return self.sn == other.sn |
| 351 |
|
| 352 |
def __lt__(self, other): |
| 353 |
if type (other) != type (self): |
| 354 |
return False |
| 355 |
|
| 356 |
if self == other: |
| 357 |
return False; |
| 358 |
|
| 359 |
if self._network_host != other._network_host: |
| 360 |
if self._network_host is None: |
| 361 |
return True |
| 362 |
|
| 363 |
if other._network_host is None: |
| 364 |
return False |
| 365 |
|
| 366 |
return self._network_host < other._network_host |
| 367 |
|
| 368 |
if self.dnssd_hostname != other.dnssd_hostname: |
| 369 |
if self.dnssd_hostname is None: |
| 370 |
return True |
| 371 |
|
| 372 |
if other.dnssd_hostname is None: |
| 373 |
return False |
| 374 |
|
| 375 |
return self.dnssd_hostname < other.dnssd_hostname |
| 376 |
|
| 377 |
devs = other.get_devices() |
| 378 |
if devs: |
| 379 |
uris = [x.uri for x in self.devices] |
| 380 |
for dev in devs: |
| 381 |
if dev.uri in uris: |
| 382 |
# URI match, so compare equal. Not less than. |
| 383 |
return False |
| 384 |
|
| 385 |
if ((other.mfg == '' and other.mdl == '') or |
| 386 |
(self.mfg == '' and self.mdl == '')): |
| 387 |
if other.mfg == '' and self.mfg == '': |
| 388 |
# Both just a backend, not a real physical device. |
| 389 |
return self.devices[0] < other.devices[0] |
| 390 |
|
| 391 |
# One or other is just a backend, not a real physical device. |
| 392 |
return other.mfg == '' and other.mdl == '' |
| 393 |
|
| 394 |
def split_make_and_model (dev): |
| 395 |
if dev.mfg == '' or dev.mdl.lower ().startswith (dev.mfg.lower ()): |
| 396 |
make_and_model = dev.mdl |
| 397 |
else: |
| 398 |
make_and_model = "%s %s" % (dev.mfg, dev.mdl) |
| 399 |
(mfg, mdl) = cupshelpers.ppds.ppdMakeModelSplit (make_and_model) |
| 400 |
return (cupshelpers.ppds.normalize (mfg), |
| 401 |
cupshelpers.ppds.normalize (mdl)) |
| 402 |
|
| 403 |
(our_mfg, our_mdl) = split_make_and_model (self) |
| 404 |
(other_mfg, other_mdl) = split_make_and_model (other) |
| 405 |
|
| 406 |
if our_mfg != other_mfg: |
| 407 |
return our_mfg < other_mfg |
| 408 |
|
| 409 |
if our_mdl != other_mdl: |
| 410 |
return our_mdl < other_mdl |
| 411 |
|
| 412 |
if self.sn == '' or other.sn == '': |
| 413 |
return False |
| 414 |
|
| 415 |
return self.sn < other.sn |
| 416 |
|
| 417 |
if __name__ == '__main__': |
| 418 |
import authconn |
| 419 |
c = authconn.Connection () |
| 420 |
devices = cupshelpers.getDevices (c) |
| 421 |
|
| 422 |
physicaldevices = [] |
| 423 |
for device in devices.values (): |
| 424 |
physicaldevice = PhysicalDevice (device) |
| 425 |
try: |
| 426 |
i = physicaldevices.index (physicaldevice) |
| 427 |
physicaldevices[i].add_device (device) |
| 428 |
except ValueError: |
| 429 |
physicaldevices.append (physicaldevice) |
| 430 |
|
| 431 |
physicaldevices.sort () |
| 432 |
for physicaldevice in physicaldevices: |
| 433 |
print(physicaldevice.get_info ()) |
| 434 |
devices = physicaldevice.get_devices () |
| 435 |
for device in devices: |
| 436 |
print(" ", device) |