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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
|
Description: Port to Python3
Author: dadosch <daniel-gitlab@dadosch.de>
--- a/tea4cups
+++ b/tea4cups
@@ -1,4 +1,4 @@
-#! /usr/bin/env python
+#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Tea4CUPS : Tee for CUPS
@@ -70,24 +70,39 @@
Send bug reports to : alet@librelogiciel.com
"""
+import getpass
+
import sys
import os
import time
import pwd
import errno
import random
-import md5
-import cStringIO
+# following doesn't work, see https://github.com/VitaliyRodnenko/geeknote/issues/299
+# from hashlib import md5
+import hashlib
+import io
import shlex
import tempfile
-import ConfigParser
+import configparser
import signal
import socket
import fcntl
-import urllib2
+import logging
+import requests
+import urllib.parse
+#urllib.request, urllib.error,
+
from struct import pack, unpack
-__version__ = "3.14alpha_unofficial"
+__version__ = "3.15alpha_unofficial"
+
+# http debug
+logging.basicConfig(level=logging.DEBUG)
+logging.getLogger().setLevel(logging.DEBUG)
+requests_log = logging.getLogger("requests.packages.urllib3")
+requests_log.setLevel(logging.DEBUG)
+requests_log.propagate = True
class TeeError(Exception):
"""Base exception for Tea4CUPS related stuff."""
@@ -380,7 +395,7 @@
answer.extend(attrvalue)
if answer :
return answer
- raise KeyError, key
+ raise KeyError(key)
class IPPRequest :
"""A class for IPP requests."""
@@ -471,7 +486,7 @@
if name in self.attributes_types :
return FakeAttribute(self, name)
else :
- raise AttributeError, name
+ raise AttributeError(name)
def __str__(self) :
"""Returns the parsed IPP message in a readable form."""
@@ -489,13 +504,12 @@
mybuffer.append(" %s : %s" % (name, value))
if self.data :
mybuffer.append("IPP datas : %s" % repr(self.data))
- return "\n".join(mybuffer)
+ return "\n".join(str(mybuffer))
def logDebug(self, msg) :
"""Prints a debug message."""
if self.debug :
- sys.stderr.write("%s\n" % msg)
- sys.stderr.flush()
+ logging.debug(msg)
def setVersion(self, version) :
"""Sets the request's operation id."""
@@ -524,6 +538,7 @@
Returns the message as a string of text.
"""
+
mybuffer = []
if None not in (self.version, self.operation_id) :
mybuffer.append(chr(self.version[0]) + chr(self.version[1]))
@@ -554,7 +569,24 @@
mybuffer.append(val)
mybuffer.append(chr(self.tagvalues["end-of-attributes-tag"]))
mybuffer.append(self.data)
- return "".join(mybuffer)
+
+
+ self.logDebug("mybuffer to str is %s" % str(mybuffer))
+
+ cleanBuffer=[]
+ for x in mybuffer:
+ if isinstance(x, bytes):
+ self.logDebug("x is bytes: %s" % str(x))
+ cleanBuffer.append(str(x))
+ elif isinstance(x, str):
+ cleanBuffer.append(x)
+ else:
+ self.logDebug("uncatched type of %s " % str(x))
+ #elif isinstance(x, char)
+ self.logDebug("cleanBuffer to str is %s" % str(cleanBuffer))
+ #return "".join([chr(x) for x in mybuffer])
+#note: cleanbuffer should be string but must not be parsed to string otherwise \'HTTPResponse\' object is not subscriptable
+ return "\n".join(cleanBuffer)
def parse(self) :
"""Parses an IPP Request.
@@ -563,7 +595,7 @@
"""
self._curname = None
self._curattributes = None
-
+ self.logDebug("data is %s" % str(self._data))
self.setVersion((ord(self._data[0]), ord(self._data[1])))
self.setOperationId(unpack(">H", self._data[2:4])[0])
self.setRequestId(unpack(">I", self._data[4:8])[0])
@@ -587,7 +619,7 @@
if tag == oldtag :
self._curattributes.append([])
except IndexError :
- raise IPPError, "Unexpected end of IPP message."
+ raise IPPError("Unexpected end of IPP message.")
self.data = self._data[self.position+1:]
self.parsed = True
@@ -673,25 +705,26 @@
self.lastErrorMessage = None
self.requestId = None
- def getDefaultURL(self) :
+ def getDefaultURL(self):
"""Builds a default URL."""
# TODO : encryption methods.
server = os.environ.get("CUPS_SERVER") or "localhost"
port = os.environ.get("IPP_PORT") or 631
- if server.startswith("/") :
+ if server.startswith("/"):
# it seems it's a unix domain socket.
+ # TODO
# we can't handle this right now, so we use the default instead.
return "http://localhost:%s" % port
- else :
+ else:
return "http://%s:%s" % (server, port)
- def identifierToURI(self, service, ident) :
+ def identifierToURI(self, service, ident):
"""Transforms an identifier into a particular URI depending on requested service."""
return "%s/%s/%s" % (self.url.replace("http://", "ipp://"),
service,
ident)
- def nextRequestId(self) :
+ def nextRequestId(self):
"""Increments the current request id and returns the new value."""
try :
self.requestId += 1
@@ -707,37 +740,35 @@
debug=self.debug)
req.operation["attributes-charset"] = ("charset", self.charset)
req.operation["attributes-natural-language"] = ("naturalLanguage", self.language)
+ wrapper.logDebug("charset is set to %s" % req.operation["attributes-charset"])
+ wrapper.logDebug("language is set to %s" % req.operation["attributes-natural-language"])
return req
def doRequest(self, req, url=None) :
"""Sends a request to the CUPS server.
returns a new IPPRequest object, containing the parsed answer.
"""
- connexion = urllib2.Request(url=url or self.url, \
- data=req.dump())
- connexion.add_header("Content-Type", "application/ipp")
+ data = req.dump()
+ headers = {'Connection':'close','Content-Type': 'application/ipp','Accept-Encoding':'identity'}
+ auth=None
if self.username :
- pwmanager = urllib2.HTTPPasswordMgrWithDefaultRealm()
- pwmanager.add_password(None, \
- "%s%s" % (connexion.get_host(), connexion.get_selector()), \
- self.username, \
- self.password or "")
- authhandler = urllib2.HTTPBasicAuthHandler(pwmanager)
- opener = urllib2.build_opener(authhandler)
- urllib2.install_opener(opener)
- self.lastError = None
- self.lastErrorMessage = None
- try :
- response = urllib2.urlopen(connexion)
- except (urllib2.URLError, urllib2.HTTPError, socket.error), error :
- self.lastError = error
- self.lastErrorMessage = str(error)
- return None
- else :
- datas = response.read()
- ippresponse = IPPRequest(datas)
- ippresponse.parse()
- return ippresponse
+ auth=(self.username, self.password or "")
+
+ # TODO proper error handling
+ #self.lastError = None
+ #self.lastErrorMessage = None
+
+ r = requests.post(url=url or self.url, auth=auth, data=data, headers=headers, stream=True)
+ r.raw.decode_content=True
+
+ datas = r.raw
+
+ wrapper.logDebug("data to parse: %s" % str(datas))
+ wrapper.logDebug("request content: %s"%str(r.raw.content))
+ ippresponse = IPPRequest(datas)
+
+ ippresponse.parse()
+ return ippresponse
def getPPD(self, queuename) :
"""Retrieves the PPD for a particular queuename."""
@@ -768,18 +799,18 @@
def getDevices(self) :
"""Returns a list of devices as (deviceclass, deviceinfo, devicemakeandmodel, deviceuri) tuples."""
answer = self.doRequest(self.newRequest(CUPS_GET_DEVICES))
- return zip([d[1] for d in answer.printer["device-class"]], \
+ return list(zip([d[1] for d in answer.printer["device-class"]], \
[d[1] for d in answer.printer["device-info"]], \
[d[1] for d in answer.printer["device-make-and-model"]], \
- [d[1] for d in answer.printer["device-uri"]])
+ [d[1] for d in answer.printer["device-uri"]]))
def getPPDs(self) :
"""Returns a list of PPDs as (ppdnaturallanguage, ppdmake, ppdmakeandmodel, ppdname) tuples."""
answer = self.doRequest(self.newRequest(CUPS_GET_PPDS))
- return zip([d[1] for d in answer.printer["ppd-natural-language"]], \
+ return list(zip([d[1] for d in answer.printer["ppd-natural-language"]], \
[d[1] for d in answer.printer["ppd-make"]], \
[d[1] for d in answer.printer["ppd-make-and-model"]], \
- [d[1] for d in answer.printer["ppd-name"]])
+ [d[1] for d in answer.printer["ppd-name"]]))
def createSubscription(self, uri, events=["all"],
userdata=None,
@@ -851,7 +882,7 @@
"""Fakes a configuration file parser."""
def get(self, section, option, raw=0) :
"""Fakes the retrieval of an option."""
- raise ConfigError, "Invalid configuration file : no option %s in section [%s]" % (option, section)
+ raise ConfigError("Invalid configuration file : no option %s in section [%s]" % (option, section))
def isTrue(option) :
"""Returns 1 if option is set to true, else 0."""
@@ -872,7 +903,7 @@
try :
conffile = open(cupsdconf, "r")
except IOError :
- raise TeeError, "Unable to open %s" % cupsdconf
+ raise TeeError("Unable to open %s" % cupsdconf)
else :
for line in conffile.readlines() :
linecopy = line.strip().lower()
@@ -934,6 +965,8 @@
try :
# open the lock file, optionally creating it if needed.
self.LockFile = None
+ if not os.path.isfile(lockfilename):
+ open(lockfilename, 'w+').close()
self.LockFile = open(lockfilename, "a+")
# we wait indefinitely for the lock to become available.
@@ -962,21 +995,25 @@
confdir = os.environ.get("CUPS_SERVERROOT", ".")
self.conffile = os.path.join(confdir, "%s.conf" % self.myname)
if os.path.isfile(self.conffile) :
- self.config = ConfigParser.ConfigParser()
+ self.config = configparser.ConfigParser()
self.config.read([self.conffile])
- self.debug = isTrue(self.getGlobalOption("debug", ignore=1))
+ self.debug = True
+ #isTrue(self.getGlobalOption("debug", ignore=1))
else :
self.config = FakeConfig()
- self.debug = 1 # no config, so force debug mode !
+ self.debug = True # no config, so force debug mode !
+ logging.warning("no config found")
def logInfo(self, message, level="info") :
"""Logs a message to CUPS' error_log file."""
try :
- sys.stderr.write("%s: %s v%s (PID %i) : %s\n" % (level.upper(), self.MyName, __version__, os.getpid(), message))
+ logging.debug(message)
+ sys.stderr.write("[TEA4CUPS]_%s: %s v%s (PID %i) : %s\n" % (level.upper(), self.MyName, __version__, os.getpid(), message))
sys.stderr.flush()
except IOError :
pass
+# TODO are there two logDebug?
def logDebug(self, message) :
"""Logs something to debug output if debug is enabled."""
if self.debug :
@@ -986,31 +1023,31 @@
"""Returns an option from the global section, or raises a ConfigError if ignore is not set, else returns None."""
try :
return self.config.get("global", option, raw=1)
- except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) :
+ except (configparser.NoSectionError, configparser.NoOptionError) :
if not ignore :
- raise ConfigError, "Option %s not found in section global of %s" % (option, self.conffile)
+ raise ConfigError("Option %s not found in section global of %s" % (option, self.conffile))
def getPrintQueueOption(self, printqueuename, option, ignore=0) :
"""Returns an option from the printer section, or the global section, or raises a ConfigError."""
globaloption = self.getGlobalOption(option, ignore=1)
try :
return self.config.get(printqueuename, option, raw=1)
- except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) :
+ except (configparser.NoSectionError, configparser.NoOptionError) :
if globaloption is not None :
return globaloption
elif not ignore :
- raise ConfigError, "Option %s not found in section [%s] of %s" % (option, printqueuename, self.conffile)
+ raise ConfigError("Option %s not found in section [%s] of %s" % (option, printqueuename, self.conffile))
def enumBranches(self, printqueuename, branchtype="tee") :
"""Returns the list of branchtypes branches for a particular section's."""
branchbasename = "%s_" % branchtype.lower()
try :
globalbranches = [ (k, self.config.get("global", k)) for k in self.config.options("global") if k.startswith(branchbasename) ]
- except ConfigParser.NoSectionError, msg :
- raise ConfigError, "Invalid configuration file : %s" % msg
+ except configparser.NoSectionError as msg :
+ raise ConfigError("Invalid configuration file : %s" % msg)
try :
sectionbranches = [ (k, self.config.get(printqueuename, k)) for k in self.config.options(printqueuename) if k.startswith(branchbasename) ]
- except ConfigParser.NoSectionError, msg :
+ except configparser.NoSectionError as msg :
self.logInfo("No section for print queue %s : %s" % (printqueuename, msg))
sectionbranches = []
branches = {}
@@ -1048,7 +1085,7 @@
try :
# see if the pid contained in the lock file is still running
os.kill(pid, 0)
- except OSError, error :
+ except OSError as error :
if error.errno != errno.EPERM :
# process doesn't exist anymore
os.remove(lockfilename)
@@ -1073,7 +1110,7 @@
# each line is of the form :
# 'xxxx xxxx "xxxx xxx" "xxxx xxx"'
# so we have to decompose it carefully
- fdevice = cStringIO.StringIO(d)
+ fdevice = io.StringIO(d)
tokenizer = shlex.shlex(fdevice)
tokenizer.wordchars = tokenizer.wordchars + \
r".:,?!~/\_$*-+={}[]()#"
@@ -1135,7 +1172,7 @@
self.logDebug("Not attached to an existing print queue.")
backend = ""
else :
- raise TeeError, "Invalid DEVICE_URI : %s\n" % device_uri
+ raise TeeError("Invalid DEVICE_URI : %s\n" % device_uri)
self.RealBackend = backend
self.DeviceURI = device_uri
@@ -1143,6 +1180,7 @@
try :
cupsserver = CUPS() # TODO : username and password and/or encryption
answer = cupsserver.getJobAttributes(self.JobId)
+ self.logInfo("answer is %s" % str(answer), "warn")
if answer is None : # probably connection refused because we
raise ValueError # don't hande unix domain sockets yet.
self.ControlFile = "NotUsedAnymore"
@@ -1178,6 +1216,7 @@
if requestroot is None :
cupsdconf = getCupsConfigDirectives(["RequestRoot"])
requestroot = cupsdconf.get("requestroot", "/var/spool/cups")
+
if (len(self.JobId) < 5) and self.JobId.isdigit() :
ippmessagefile = "c%05i" % int(self.JobId)
else :
@@ -1185,16 +1224,21 @@
ippmessagefile = os.path.join(requestroot, ippmessagefile)
ippmessage = {}
try :
- ippdatafile = open(ippmessagefile)
- except :
- self.logInfo("Unable to open IPP message file %s" % ippmessagefile, "warn")
- else :
+ ippdatafile = open(ippmessagefile,"r")
+ except TeeError:
+ self.logDebug("IOError: Unable to open IPP message file %s" % ippmessagefile)
+ self.logDebug("Debug: user: %s" % (os.getgroups()))
+ except FileNotFoundError:
+ # TODO I always get "File Not Found" for /var/spool/cups/cxxxx
+ self.logDebug("File Not found: %s" % ippmessagefile)
+ self.logDebug("Debug: user: %s" % (os.getgroups()))
+ else:
self.logDebug("Parsing of IPP message file %s begins." % ippmessagefile)
try :
ippmessage = IPPRequest(ippdatafile.read())
ippmessage.parse()
- except IPPError, msg :
- self.logInfo("Error while parsing %s : %s" % (ippmessagefile, msg), "warn")
+ except IPPError as msg :
+ self.logDebug("Error while parsing %s : %s" % (ippmessagefile, msg))
else :
self.logDebug("Parsing of IPP message file %s ends." % ippmessagefile)
ippdatafile.close()
@@ -1226,7 +1270,8 @@
infile = open(self.InputFile, "rb")
mustclose = 1
else :
- infile = sys.stdin
+ # https://stackoverflow.com/a/32282458
+ infile = sys.stdin.buffer
filtercommand = self.getPrintQueueOption(self.PrinterName, "filter", \
ignore=1)
@@ -1247,15 +1292,15 @@
CHUNK = 64*1024 # read 64 Kb at a time
dummy = 0
sizeread = 0
- checksum = md5.new()
- outfile = open(self.DataFile, "wb")
+ checksum = ""
+ outfile = open(self.DataFile, "wb") # was wb
while 1 :
data = infile.read(CHUNK)
if not data :
break
sizeread += len(data)
outfile.write(data)
- checksum.update(data)
+ checksum = hashlib.md5(data)
if not (dummy % 32) : # Only display every 2 Mb
self.logDebug("%s bytes saved..." % sizeread)
dummy += 1
@@ -1284,7 +1329,7 @@
and os.path.exists(self.DataFile) :
try :
os.remove(self.DataFile)
- except OSError, msg :
+ except OSError as msg :
self.logInfo("Problem when removing %s : %s" % (self.DataFile, msg), "error")
if self.LockFile is not None :
@@ -1304,10 +1349,10 @@
serialize = isTrue(self.getPrintQueueOption(self.PrinterName, "serialize", ignore=1))
self.pipes = { 0: (0, 1) }
branches = self.enumBranches(self.PrinterName, "prehook")
- for b in branches.keys() :
+ for b in list(branches.keys()) :
self.pipes[b.split("_", 1)[1]] = os.pipe()
retcode = self.runCommands("prehook", branches, serialize)
- for p in [ (k, v) for (k, v) in self.pipes.items() if k != 0 ] :
+ for p in [ (k, v) for (k, v) in list(self.pipes.items()) if k != 0 ] :
os.close(p[1][1])
if self.isCancelled :
retcode = CUPS_BACKEND_CANCEL # Job cancelled, for CUPS.
@@ -1326,7 +1371,7 @@
if self.runCommands("posthook", branches, serialize) :
self.logInfo("An error occured during the execution of posthooks.", "warn")
- for p in [ (k, v) for (k, v) in self.pipes.items() if k != 0 ] :
+ for p in [ (k, v) for (k, v) in list(self.pipes.items()) if k != 0 ] :
os.close(p[1][0])
if retcode == CUPS_BACKEND_OK :
self.logInfo("OK")
@@ -1347,7 +1392,7 @@
os.close(stdout)
try :
os.execl("/bin/sh", "sh", "-c", cmd)
- except OSError, msg :
+ except OSError as msg :
self.logDebug("execl() failed: %s" % msg)
os._exit(-1)
status = os.waitpid(pid, 0)[1]
@@ -1360,7 +1405,7 @@
# Code contributed by Peter Stuge on June 7th 2005
self.logDebug("Launching %s : %s" % (branch, command))
btype, bname = branch.split("_", 1)
- if bname not in self.pipes.keys() :
+ if bname not in list(self.pipes.keys()) :
bname = 0
if btype == "prehook" :
return self.stdioRedirSystem(command, 0, self.pipes[bname][1])
@@ -1372,7 +1417,7 @@
exitcode = CUPS_BACKEND_OK
btype = btype.lower()
btypetitle = btype.title()
- branchlist = branches.keys()
+ branchlist = list(branches.keys())
branchlist.sort()
if serialize :
self.logDebug("Begin serialized %ss" % btypetitle)
@@ -1400,7 +1445,7 @@
pids[branch] = pid
else :
os._exit(self.runCommand(branch, branches[branch]))
- for (branch, pid) in pids.items() :
+ for (branch, pid) in list(pids.items()) :
retcode = os.waitpid(pid, 0)[1]
if os.WIFEXITED(retcode) :
retcode = os.WEXITSTATUS(retcode)
@@ -1460,15 +1505,19 @@
arguments[6] = self.DataFile # in case a tea4cups filter was applied
try :
os.execve(originalbackend, arguments, os.environ)
- except OSError, msg :
+ except OSError as msg :
+ self.logDebug("originalbackend: %s, arguments: %s, os.environ: %s" %originalbackend %arguments %os.environ)
self.logDebug("execve() failed: %s" % msg)
+ logging.error("could run original backend because execve failed: %s" %msg)
os._exit(-1)
killed = 0
status = -1
while status == -1 :
try :
status = os.waitpid(pid, 0)[1]
- except OSError, (err, msg) :
+ # TODO there seems to be some work to be done
+ except OSError as xxx_todo_changeme :
+ (err, msg) = xxx_todo_changeme.args
if err == 4 :
killed = 1
if os.WIFEXITED(status) :
@@ -1489,7 +1538,7 @@
# This is a CUPS backend, we should act and die like a CUPS backend
wrapper = CupsBackend()
if len(sys.argv) == 1 :
- print "\n".join(wrapper.discoverOtherBackends())
+ print("\n".join(wrapper.discoverOtherBackends()))
sys.exit(0)
elif len(sys.argv) not in (6, 7) :
sys.stderr.write("ERROR: %s job-id user title copies options [file]\n"\
@@ -1505,7 +1554,8 @@
wrapper.saveDatasAndCheckSum()
wrapper.exportAttributes()
returncode = wrapper.runBranches()
- except SystemExit, e :
+ wrapper.logDebug("returncode is %i" %returncode )
+ except SystemExit as e :
returncode = e.code
except KeyboardInterrupt :
wrapper.logInfo("Job %s interrupted by the administrator !" % wrapper.JobId, "warn")
@@ -1518,6 +1568,7 @@
wrapper.pid, l) \
for l in (["ERROR: Tea4CUPS v%s" % __version__] + lines)])
sys.stderr.write(errormessage)
+ wrapper.logDebug(errormessage)
sys.stderr.flush()
returncode = 1
finally :
|