[695] | 1 | #! /usr/bin/env python |
---|
[1144] | 2 | # -*- coding: ISO-8859-15 -*- |
---|
[695] | 3 | |
---|
| 4 | # PyKota accounting filter |
---|
| 5 | # |
---|
[952] | 6 | # PyKota - Print Quotas for CUPS and LPRng |
---|
[695] | 7 | # |
---|
[1257] | 8 | # (c) 2003-2004 Jerome Alet <alet@librelogiciel.com> |
---|
[873] | 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. |
---|
[695] | 13 | # |
---|
[873] | 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
[695] | 22 | # |
---|
| 23 | # $Id$ |
---|
| 24 | # |
---|
| 25 | # $Log$ |
---|
[1400] | 26 | # Revision 1.57 2004/03/15 10:47:56 jalet |
---|
| 27 | # This time the traceback formatting should be correct ! |
---|
| 28 | # |
---|
[1391] | 29 | # Revision 1.56 2004/03/05 12:46:08 jalet |
---|
| 30 | # Improve tracebacks |
---|
| 31 | # |
---|
[1390] | 32 | # Revision 1.55 2004/03/05 12:31:35 jalet |
---|
| 33 | # Now should output full traceback when crashing |
---|
| 34 | # |
---|
[1376] | 35 | # Revision 1.54 2004/03/01 15:06:51 jalet |
---|
| 36 | # Pre and Post hooks should now work in the pykota filter too. |
---|
| 37 | # The pykota filter doesn't check the last user's quota anymore |
---|
| 38 | # when delayed hardware accounting is used : this will be checked |
---|
| 39 | # anyway the next time the last user will print |
---|
| 40 | # |
---|
[1359] | 41 | # Revision 1.53 2004/02/25 12:36:34 jalet |
---|
| 42 | # Avoids a database query even if caching was disabled. |
---|
| 43 | # |
---|
[1292] | 44 | # Revision 1.52 2004/01/14 15:57:54 jalet |
---|
| 45 | # Missing return caused problems with LPRng |
---|
| 46 | # |
---|
[1281] | 47 | # Revision 1.51 2004/01/12 18:20:45 jalet |
---|
| 48 | # Denied jobs weren't added to the history anymore, this is now fixed. |
---|
| 49 | # |
---|
[1271] | 50 | # Revision 1.50 2004/01/11 23:22:42 jalet |
---|
| 51 | # Major code refactoring, it's way cleaner, and now allows automated addition |
---|
| 52 | # of printers on first print. |
---|
| 53 | # |
---|
[1257] | 54 | # Revision 1.49 2004/01/08 14:10:32 jalet |
---|
| 55 | # Copyright year changed. |
---|
| 56 | # |
---|
[1240] | 57 | # Revision 1.48 2003/12/27 16:49:25 uid67467 |
---|
| 58 | # Should be ok now. |
---|
| 59 | # |
---|
[1221] | 60 | # Revision 1.47 2003/11/26 19:17:35 jalet |
---|
| 61 | # Printing on a printer not present in the Quota Storage now results |
---|
| 62 | # in the job being stopped or cancelled depending on the system. |
---|
| 63 | # |
---|
[1205] | 64 | # Revision 1.46 2003/11/24 14:25:02 jalet |
---|
| 65 | # Missing import in pykota filter |
---|
| 66 | # |
---|
[1196] | 67 | # Revision 1.45 2003/11/19 23:19:37 jalet |
---|
| 68 | # Code refactoring work. |
---|
| 69 | # Explicit redirection to /dev/null has to be set in external policy now, just |
---|
| 70 | # like in external mailto. |
---|
| 71 | # |
---|
[1177] | 72 | # Revision 1.44 2003/11/08 16:05:31 jalet |
---|
| 73 | # CUPS backend added for people to experiment. |
---|
| 74 | # |
---|
[1176] | 75 | # Revision 1.43 2003/11/06 22:33:25 jalet |
---|
| 76 | # French variable name |
---|
| 77 | # |
---|
[1152] | 78 | # Revision 1.42 2003/10/08 21:41:38 jalet |
---|
| 79 | # External policies for printers works ! |
---|
| 80 | # We can now auto-add users on first print, and do other useful things if needed. |
---|
| 81 | # |
---|
[1144] | 82 | # Revision 1.41 2003/10/07 09:07:27 jalet |
---|
| 83 | # Character encoding added to please latest version of Python |
---|
| 84 | # |
---|
[1124] | 85 | # Revision 1.40 2003/09/04 08:38:56 jalet |
---|
| 86 | # Added an exception catch to ensure clean close of database even in |
---|
| 87 | # case of TypeError too. |
---|
| 88 | # |
---|
[1117] | 89 | # Revision 1.39 2003/08/18 16:35:28 jalet |
---|
| 90 | # New pychecker pass, on the tools this time. |
---|
| 91 | # |
---|
[1116] | 92 | # Revision 1.38 2003/08/18 16:20:59 jalet |
---|
| 93 | # Improvement of the printing system detection code. |
---|
| 94 | # |
---|
[1113] | 95 | # Revision 1.37 2003/07/29 20:55:17 jalet |
---|
| 96 | # 1.14 is out ! |
---|
| 97 | # |
---|
[1080] | 98 | # Revision 1.36 2003/07/10 06:09:52 jalet |
---|
| 99 | # Incorrect documentation string |
---|
| 100 | # |
---|
[1041] | 101 | # Revision 1.35 2003/06/25 14:10:01 jalet |
---|
| 102 | # Hey, it may work (edpykota --reset excepted) ! |
---|
| 103 | # |
---|
[1026] | 104 | # Revision 1.34 2003/06/13 18:54:17 jalet |
---|
| 105 | # Bug with remote jobs and LPRng fixed. |
---|
| 106 | # |
---|
[1004] | 107 | # Revision 1.33 2003/05/28 13:51:38 jalet |
---|
| 108 | # Better handling of errors |
---|
| 109 | # |
---|
[1000] | 110 | # Revision 1.32 2003/05/27 23:00:20 jalet |
---|
| 111 | # Big rewrite of external accounting methods. |
---|
| 112 | # Should work well now. |
---|
| 113 | # |
---|
[976] | 114 | # Revision 1.31 2003/04/30 13:36:39 jalet |
---|
| 115 | # Stupid accounting method was added. |
---|
| 116 | # |
---|
[975] | 117 | # Revision 1.30 2003/04/29 22:03:38 jalet |
---|
| 118 | # Better error handling. |
---|
| 119 | # |
---|
[973] | 120 | # Revision 1.29 2003/04/29 18:37:54 jalet |
---|
| 121 | # Pluggable accounting methods (actually doesn't support external scripts) |
---|
| 122 | # |
---|
[966] | 123 | # Revision 1.28 2003/04/26 08:41:24 jalet |
---|
| 124 | # Small code reorganisation (UNTESTED) to allow pluggable accounting |
---|
| 125 | # methods in the future. |
---|
| 126 | # |
---|
[963] | 127 | # Revision 1.27 2003/04/25 09:23:47 jalet |
---|
| 128 | # Debug message passed through ! |
---|
| 129 | # |
---|
[962] | 130 | # Revision 1.26 2003/04/25 08:23:23 jalet |
---|
| 131 | # Multiple tries to get the printer's internal page counter, waits for |
---|
| 132 | # one minute maximum for the printer to warm up, actually. |
---|
| 133 | # |
---|
[956] | 134 | # Revision 1.25 2003/04/24 11:53:48 jalet |
---|
| 135 | # Default policy for unknown users/groups is to DENY printing instead |
---|
| 136 | # of the previous default to ALLOW printing. This is to solve an accuracy |
---|
| 137 | # problem. If you set the policy to ALLOW, jobs printed by in nexistant user |
---|
| 138 | # (from PyKota's POV) will be charged to the next user who prints on the |
---|
| 139 | # same printer. |
---|
| 140 | # |
---|
[952] | 141 | # Revision 1.24 2003/04/23 22:13:56 jalet |
---|
| 142 | # Preliminary support for LPRng added BUT STILL UNTESTED. |
---|
| 143 | # |
---|
[915] | 144 | # Revision 1.23 2003/04/15 11:30:57 jalet |
---|
| 145 | # More work done on money print charging. |
---|
| 146 | # Minor bugs corrected. |
---|
| 147 | # All tools now access to the storage as priviledged users, repykota excepted. |
---|
| 148 | # |
---|
[914] | 149 | # Revision 1.22 2003/04/15 11:09:04 jalet |
---|
| 150 | # Small bug was fixed when a printer was never used and its internal |
---|
| 151 | # page counter is not accessible. |
---|
| 152 | # |
---|
[909] | 153 | # Revision 1.21 2003/04/12 17:20:14 jalet |
---|
| 154 | # Better formula for HP workaround |
---|
| 155 | # |
---|
[908] | 156 | # Revision 1.20 2003/04/12 16:58:28 jalet |
---|
| 157 | # The workaround for HP printers was not correct, and there's probably no |
---|
| 158 | # correct way to workaround the problem because we can't save the internal |
---|
| 159 | # page counter in real time. The last job's size is unconditionnally set to |
---|
| 160 | # 5 pages in this case. |
---|
| 161 | # |
---|
[902] | 162 | # Revision 1.19 2003/04/11 08:56:49 jalet |
---|
| 163 | # Comment |
---|
| 164 | # |
---|
[901] | 165 | # Revision 1.18 2003/04/11 08:50:39 jalet |
---|
| 166 | # Workaround for the HP "feature" of saving the page counter to NVRAM |
---|
| 167 | # only every time 10 new pages are printed... |
---|
| 168 | # Workaround for printers with volatile page counters. |
---|
| 169 | # |
---|
[900] | 170 | # Revision 1.17 2003/04/10 21:47:20 jalet |
---|
| 171 | # Job history added. Upgrade script neutralized for now ! |
---|
| 172 | # |
---|
[887] | 173 | # Revision 1.16 2003/04/08 20:38:08 jalet |
---|
| 174 | # The last job Id is saved now for each printer, this will probably |
---|
| 175 | # allow other accounting methods in the future. |
---|
| 176 | # |
---|
[873] | 177 | # Revision 1.15 2003/03/29 13:45:27 jalet |
---|
| 178 | # GPL paragraphs were incorrectly (from memory) copied into the sources. |
---|
| 179 | # Two README files were added. |
---|
| 180 | # Upgrade script for PostgreSQL pre 1.01 schema was added. |
---|
| 181 | # |
---|
[832] | 182 | # Revision 1.14 2003/03/07 22:16:57 jalet |
---|
| 183 | # Algorithmically incorrect : last user quota wasn't updated if current |
---|
| 184 | # user wasn't allowed to print. |
---|
| 185 | # |
---|
[826] | 186 | # Revision 1.13 2003/02/27 23:59:28 jalet |
---|
| 187 | # Stupid bug wrt exception handlingand value conversion |
---|
| 188 | # |
---|
[825] | 189 | # Revision 1.12 2003/02/27 23:48:41 jalet |
---|
| 190 | # Correctly maps PyKota's log levels to syslog log levels |
---|
| 191 | # |
---|
[824] | 192 | # Revision 1.11 2003/02/27 22:55:20 jalet |
---|
| 193 | # WARN log priority doesn't exist. |
---|
| 194 | # |
---|
[823] | 195 | # Revision 1.10 2003/02/27 22:43:21 jalet |
---|
| 196 | # Missing import |
---|
| 197 | # |
---|
[822] | 198 | # Revision 1.9 2003/02/27 22:40:26 jalet |
---|
| 199 | # Correctly handles cases where the printer is off. |
---|
| 200 | # |
---|
[772] | 201 | # Revision 1.8 2003/02/09 12:56:53 jalet |
---|
| 202 | # Internationalization begins... |
---|
| 203 | # |
---|
[740] | 204 | # Revision 1.7 2003/02/07 10:23:48 jalet |
---|
| 205 | # Avoid a possible future name clash |
---|
| 206 | # |
---|
[728] | 207 | # Revision 1.6 2003/02/06 22:54:33 jalet |
---|
| 208 | # warnpykota should be ok |
---|
| 209 | # |
---|
[704] | 210 | # Revision 1.5 2003/02/05 22:45:25 jalet |
---|
| 211 | # Forgotten import |
---|
| 212 | # |
---|
[703] | 213 | # Revision 1.4 2003/02/05 22:42:51 jalet |
---|
| 214 | # Typo |
---|
| 215 | # |
---|
[701] | 216 | # Revision 1.3 2003/02/05 22:38:39 jalet |
---|
| 217 | # Typo |
---|
| 218 | # |
---|
[699] | 219 | # Revision 1.2 2003/02/05 22:16:20 jalet |
---|
| 220 | # DEVICE_URI is undefined outside of CUPS, i.e. for normal command line tools |
---|
| 221 | # |
---|
[695] | 222 | # Revision 1.1 2003/02/05 21:28:17 jalet |
---|
| 223 | # Initial import into CVS |
---|
| 224 | # |
---|
| 225 | # |
---|
| 226 | # |
---|
| 227 | |
---|
| 228 | import sys |
---|
[704] | 229 | import os |
---|
[695] | 230 | |
---|
[1205] | 231 | from pykota.tool import PyKotaFilterOrBackend, PyKotaToolError |
---|
[975] | 232 | from pykota.config import PyKotaConfigError |
---|
[973] | 233 | from pykota.storage import PyKotaStorageError |
---|
[1196] | 234 | from pykota.accounter import PyKotaAccounterError |
---|
| 235 | from pykota.requester import PyKotaRequesterError |
---|
[695] | 236 | |
---|
[1196] | 237 | class PyKotaFilter(PyKotaFilterOrBackend) : |
---|
| 238 | """A class for the pykota filter.""" |
---|
[952] | 239 | def acceptJob(self) : |
---|
| 240 | """Returns the exit code needed by the printing backend to accept the job and print it.""" |
---|
| 241 | if self.printingsystem == "CUPS" : |
---|
| 242 | return 0 |
---|
| 243 | elif self.printingsystem == "LPRNG" : |
---|
| 244 | return 0 |
---|
| 245 | else : |
---|
| 246 | # UNKNOWN |
---|
| 247 | return -1 |
---|
| 248 | |
---|
| 249 | def removeJob(self) : |
---|
| 250 | """Returns the exit code needed by the printing backend to refuse the job and remove it.""" |
---|
| 251 | if self.printingsystem == "CUPS" : |
---|
| 252 | return 1 |
---|
| 253 | elif self.printingsystem == "LPRNG" : |
---|
| 254 | return 3 |
---|
| 255 | else : |
---|
| 256 | # UNKNOWN |
---|
| 257 | return -1 |
---|
[1271] | 258 | |
---|
| 259 | def doWork(self, policy, printer, user, userpquota) : |
---|
| 260 | """Most of the work is done here.""" |
---|
| 261 | # Two different values possible for policy here : |
---|
| 262 | # ALLOW means : Either printer, user or user print quota doesn't exist, |
---|
| 263 | # but the job should be allowed anyway. |
---|
| 264 | # OK means : Both printer, user and user print quota exist, job should |
---|
| 265 | # be allowed if current user is allowed to print on this printer |
---|
| 266 | if policy == "OK" : |
---|
[1376] | 267 | # exports user information with initial values |
---|
| 268 | self.exportUserInfo(userpquota) |
---|
| 269 | |
---|
| 270 | # enters first phase |
---|
| 271 | os.putenv("PYKOTAPHASE", "BEFORE") |
---|
| 272 | |
---|
[1271] | 273 | self.logdebug("Does accounting for user %s on printer %s." % (user.Name, printer.Name)) |
---|
| 274 | action = self.accounter.doAccounting(userpquota) |
---|
[1376] | 275 | |
---|
| 276 | # exports some new environment variables |
---|
| 277 | os.putenv("PYKOTAACTION", action) |
---|
| 278 | |
---|
| 279 | # launches the pre hook |
---|
| 280 | self.prehook(userpquota) |
---|
[1281] | 281 | else : |
---|
| 282 | action = "ALLOW" |
---|
[952] | 283 | |
---|
[1271] | 284 | # pass the job's data to the next filter |
---|
[1281] | 285 | if action in ["ALLOW", "WARN"] : |
---|
| 286 | self.logdebug("Passing input data to next filter.") |
---|
| 287 | mustclose = 0 |
---|
| 288 | if self.inputfile is not None : |
---|
| 289 | if hasattr(self.inputfile, "read") : |
---|
| 290 | infile = self.inputfile |
---|
| 291 | else : |
---|
| 292 | infile = open(self.inputfile, "rb") |
---|
| 293 | mustclose = 1 |
---|
[952] | 294 | else : |
---|
[1281] | 295 | infile = sys.stdin |
---|
| 296 | data = infile.read(256*1024) |
---|
| 297 | while data : |
---|
| 298 | sys.stdout.write(data) |
---|
| 299 | data = infile.read(256*1024) |
---|
| 300 | if mustclose : |
---|
| 301 | infile.close() |
---|
| 302 | return self.acceptJob() |
---|
| 303 | else : |
---|
| 304 | self.logdebug("Printing is denied.") |
---|
| 305 | return self.removeJob() |
---|
[1152] | 306 | |
---|
[1271] | 307 | def mainWork(self) : |
---|
| 308 | # If this is a CUPS filter, we should act and die like a CUPS filter when needed |
---|
| 309 | if self.printingsystem == "CUPS" : |
---|
| 310 | if len(sys.argv) not in (6, 7) : |
---|
| 311 | sys.stderr.write("ERROR: %s job-id user title copies options [file]\n" % sys.argv[0]) |
---|
| 312 | return self.removeJob() |
---|
| 313 | |
---|
| 314 | if self.accounter.isDelayed : |
---|
| 315 | # Here we need to update the last user's print quota |
---|
| 316 | # because hardware accounting is delayed by one print job |
---|
| 317 | # in the pykota filter. |
---|
| 318 | printer = self.storage.getPrinter(self.printername) |
---|
| 319 | if not printer.Exists : |
---|
| 320 | self.logger.log_message(_("Printer %s not registered in the PyKota system") % self.printername, "info") |
---|
| 321 | else : |
---|
| 322 | if not printer.LastJob.Exists : |
---|
| 323 | self.logger.log_message(_("Printer %s was never used") % self.printername, "info") |
---|
| 324 | else : |
---|
| 325 | self.logdebug("Updating print quota for last user %s on printer %s" % (printer.LastJob.User.Name, printer.Name)) |
---|
| 326 | lastuserpquota = self.storage.getUserPQuota(printer.LastJob.User, printer) |
---|
| 327 | self.accounter.counterbefore = printer.LastJob.PrinterPageCounter |
---|
| 328 | self.accounter.counterafter = self.accounter.getPrinterInternalPageCounter() or 0 |
---|
| 329 | lastjobsize = self.accounter.getJobSize() |
---|
| 330 | self.logdebug("Last Job size : %i" % lastjobsize) |
---|
[1376] | 331 | lastjobprice = printer.LastJob.setSize(lastuserpquota, lastjobsize) |
---|
[1271] | 332 | self.logdebug("Updating user %s's quota on printer %s" % (lastuserpquota.User.Name, printer.Name)) |
---|
| 333 | lastuserpquota.increasePagesUsage(lastjobsize) |
---|
| 334 | |
---|
[1376] | 335 | # exports user information with final values |
---|
| 336 | self.exportUserInfo(lastuserpquota) |
---|
| 337 | |
---|
| 338 | # enters final phase |
---|
| 339 | os.putenv("PYKOTAPHASE", "AFTER") |
---|
| 340 | os.putenv("PYKOTAACTION", printer.LastJob.JobAction) |
---|
| 341 | os.putenv("PYKOTAJOBSIZE", str(lastjobsize)) |
---|
| 342 | os.putenv("PYKOTAJOBPRICE", str(lastjobprice)) |
---|
| 343 | |
---|
| 344 | # launches the post hook |
---|
| 345 | self.posthook(lastuserpquota) |
---|
| 346 | |
---|
| 347 | # Code below deactivated since this will be checked |
---|
| 348 | # anyway the next time this user prints |
---|
| 349 | # finally check last user's quota |
---|
| 350 | # self.warnUserPQuota(lastuserpquota) |
---|
| 351 | |
---|
[1271] | 352 | # then deal with current print job as usual |
---|
[1292] | 353 | return PyKotaFilterOrBackend.mainWork(self) |
---|
[1271] | 354 | |
---|
[695] | 355 | if __name__ == "__main__" : |
---|
[1004] | 356 | retcode = -1 |
---|
[973] | 357 | try : |
---|
[1113] | 358 | # Initializes the current tool |
---|
| 359 | kotafilter = PyKotaFilter() |
---|
[1271] | 360 | retcode = kotafilter.mainWork() |
---|
[1196] | 361 | except (PyKotaToolError, PyKotaConfigError, PyKotaStorageError, PyKotaAccounterError, PyKotaRequesterError, AttributeError, KeyError, IndexError, ValueError, TypeError, IOError), msg : |
---|
[1390] | 362 | import traceback |
---|
[1400] | 363 | mm = [((f.endswith('\n') and f) or (f + '\n')) for f in traceback.format_exception(*sys.exc_info())] |
---|
| 364 | sys.stderr.write("ERROR : pykota filter failed (%s)\n%s" % (msg, "ERROR : ".join(mm))) |
---|
[973] | 365 | sys.stderr.flush() |
---|
[1177] | 366 | try : |
---|
| 367 | retcode = kotafilter.removeJob() |
---|
| 368 | except : |
---|
| 369 | retcode = -1 |
---|
[1113] | 370 | |
---|
| 371 | try : |
---|
| 372 | kotafilter.storage.close() |
---|
| 373 | except (TypeError, NameError, AttributeError) : |
---|
| 374 | pass |
---|
| 375 | |
---|
[973] | 376 | sys.exit(retcode) |
---|