[696] | 1 | #! /usr/bin/env python |
---|
| 2 | |
---|
[731] | 3 | # PyKota Print Quota Reports generator |
---|
[696] | 4 | # |
---|
[952] | 5 | # PyKota - Print Quotas for CUPS and LPRng |
---|
[696] | 6 | # |
---|
| 7 | # (c) 2003 Jerome Alet <alet@librelogiciel.com> |
---|
[873] | 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. |
---|
[696] | 12 | # |
---|
[873] | 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
[696] | 21 | # |
---|
| 22 | # $Id$ |
---|
| 23 | # |
---|
| 24 | # $Log$ |
---|
[1017] | 25 | # Revision 1.36 2003/06/06 14:21:08 jalet |
---|
| 26 | # New LDAP schema. |
---|
| 27 | # Small bug fixes. |
---|
| 28 | # |
---|
[1005] | 29 | # Revision 1.35 2003/05/28 13:56:24 jalet |
---|
| 30 | # Unneeded code. |
---|
| 31 | # |
---|
[975] | 32 | # Revision 1.34 2003/04/29 22:03:38 jalet |
---|
| 33 | # Better error handling. |
---|
| 34 | # |
---|
[970] | 35 | # Revision 1.33 2003/04/28 08:25:48 jalet |
---|
| 36 | # Small display bug in repykota was fixed. |
---|
| 37 | # |
---|
[952] | 38 | # Revision 1.32 2003/04/23 22:13:56 jalet |
---|
| 39 | # Preliminary support for LPRng added BUT STILL UNTESTED. |
---|
| 40 | # |
---|
[950] | 41 | # Revision 1.31 2003/04/23 09:58:17 jalet |
---|
| 42 | # Prices per page/job are now displayed |
---|
| 43 | # |
---|
[949] | 44 | # Revision 1.30 2003/04/18 08:34:51 jalet |
---|
| 45 | # Minor fix. |
---|
| 46 | # |
---|
[946] | 47 | # Revision 1.29 2003/04/18 08:29:30 jalet |
---|
| 48 | # Minor bug fix |
---|
| 49 | # |
---|
[931] | 50 | # Revision 1.28 2003/04/17 09:36:30 jalet |
---|
| 51 | # Bad alignment in output |
---|
| 52 | # |
---|
[929] | 53 | # Revision 1.27 2003/04/17 09:26:21 jalet |
---|
| 54 | # repykota now reports account balances too. |
---|
| 55 | # |
---|
[927] | 56 | # Revision 1.26 2003/04/16 12:35:49 jalet |
---|
| 57 | # Groups quota work now ! |
---|
| 58 | # |
---|
[915] | 59 | # Revision 1.25 2003/04/15 11:30:57 jalet |
---|
| 60 | # More work done on money print charging. |
---|
| 61 | # Minor bugs corrected. |
---|
| 62 | # All tools now access to the storage as priviledged users, repykota excepted. |
---|
| 63 | # |
---|
[912] | 64 | # Revision 1.24 2003/04/14 20:05:20 jalet |
---|
| 65 | # Reversed test. |
---|
| 66 | # |
---|
[905] | 67 | # Revision 1.23 2003/04/11 14:42:54 jalet |
---|
| 68 | # Translations |
---|
[904] | 69 | # |
---|
[900] | 70 | # Revision 1.21 2003/04/10 21:47:20 jalet |
---|
| 71 | # Job history added. Upgrade script neutralized for now ! |
---|
| 72 | # |
---|
[895] | 73 | # Revision 1.20 2003/04/08 21:31:39 jalet |
---|
| 74 | # (anything or 0) = anything !!! Go back to school Jerome ! |
---|
| 75 | # |
---|
[891] | 76 | # Revision 1.19 2003/04/08 21:13:44 jalet |
---|
| 77 | # Prepare --groups option to work. |
---|
| 78 | # |
---|
[890] | 79 | # Revision 1.18 2003/04/08 21:10:18 jalet |
---|
| 80 | # Checks --groups option presence instead of --users because --users is the default. |
---|
| 81 | # |
---|
[873] | 82 | # Revision 1.17 2003/03/29 13:45:27 jalet |
---|
| 83 | # GPL paragraphs were incorrectly (from memory) copied into the sources. |
---|
| 84 | # Two README files were added. |
---|
| 85 | # Upgrade script for PostgreSQL pre 1.01 schema was added. |
---|
| 86 | # |
---|
[843] | 87 | # Revision 1.16 2003/03/09 23:56:21 jalet |
---|
| 88 | # Option noquota added to do accounting only. |
---|
| 89 | # |
---|
[842] | 90 | # Revision 1.15 2003/03/09 23:39:14 jalet |
---|
| 91 | # Simplified translations. |
---|
| 92 | # |
---|
[814] | 93 | # Revision 1.14 2003/02/27 09:04:02 jalet |
---|
| 94 | # Missing translation |
---|
| 95 | # |
---|
[813] | 96 | # Revision 1.13 2003/02/27 08:44:01 jalet |
---|
| 97 | # Check to see if the printer was ever used at all, and displays "unknown" |
---|
| 98 | # as the pagecounter value in this casCheck to see if the printer was ever used at all, and displays "unknown" |
---|
| 99 | # as the pagecounter value in this case. |
---|
| 100 | # |
---|
[805] | 101 | # Revision 1.12 2003/02/17 23:02:23 jalet |
---|
| 102 | # getGraceDelay for printer |
---|
| 103 | # |
---|
[793] | 104 | # Revision 1.11 2003/02/10 12:12:34 jalet |
---|
| 105 | # Translations. |
---|
| 106 | # |
---|
[791] | 107 | # Revision 1.10 2003/02/10 12:07:30 jalet |
---|
| 108 | # Now repykota should output the recorded total page number for each printer too. |
---|
| 109 | # |
---|
[775] | 110 | # Revision 1.9 2003/02/09 13:40:29 jalet |
---|
| 111 | # typo |
---|
| 112 | # |
---|
[772] | 113 | # Revision 1.8 2003/02/09 12:56:53 jalet |
---|
| 114 | # Internationalization begins... |
---|
| 115 | # |
---|
[770] | 116 | # Revision 1.7 2003/02/08 23:17:20 jalet |
---|
| 117 | # repykota now outputs life time page counters and the total pages printed by |
---|
| 118 | # all users/groups on each printer. |
---|
| 119 | # |
---|
[752] | 120 | # Revision 1.6 2003/02/07 23:39:16 jalet |
---|
| 121 | # Typos |
---|
| 122 | # |
---|
[734] | 123 | # Revision 1.5 2003/02/07 08:38:36 jalet |
---|
| 124 | # Missing conversion. |
---|
| 125 | # empty line between two printers |
---|
| 126 | # |
---|
[733] | 127 | # Revision 1.4 2003/02/07 08:34:15 jalet |
---|
| 128 | # Test wrt date limit was wrong |
---|
| 129 | # |
---|
[732] | 130 | # Revision 1.3 2003/02/07 00:08:52 jalet |
---|
| 131 | # Typos |
---|
| 132 | # |
---|
[731] | 133 | # Revision 1.2 2003/02/06 23:58:05 jalet |
---|
| 134 | # repykota should be ok |
---|
[696] | 135 | # |
---|
| 136 | # |
---|
| 137 | # |
---|
| 138 | |
---|
[731] | 139 | import sys |
---|
| 140 | |
---|
| 141 | from mx import DateTime |
---|
| 142 | |
---|
| 143 | from pykota import version |
---|
| 144 | from pykota.tool import PyKotaTool, PyKotaToolError |
---|
[975] | 145 | from pykota.config import PyKotaConfigError |
---|
| 146 | from pykota.storage import PyKotaStorageError |
---|
[731] | 147 | |
---|
| 148 | __doc__ = """repykota v%s (C) 2003 C@LL - Conseil Internet & Logiciels Libres |
---|
| 149 | |
---|
| 150 | Generates print quota reports. |
---|
| 151 | |
---|
| 152 | command line usage : |
---|
| 153 | |
---|
| 154 | repykota [options] |
---|
| 155 | |
---|
| 156 | options : |
---|
| 157 | |
---|
| 158 | -v | --version Prints repykota's version number then exits. |
---|
| 159 | -h | --help Prints this message then exits. |
---|
| 160 | |
---|
| 161 | -u | --users Generates a report on users quota, this is |
---|
| 162 | the default. |
---|
| 163 | |
---|
| 164 | -g | --groups Generates a report on group quota instead of users. |
---|
| 165 | |
---|
| 166 | -P | --printer p Report quotas on this printer only. Actually p can |
---|
| 167 | use wildcards characters to select only |
---|
| 168 | some printers. The default value is *, meaning |
---|
| 169 | all printers. |
---|
| 170 | |
---|
| 171 | examples : |
---|
| 172 | |
---|
| 173 | $ repykota --printer lp |
---|
| 174 | |
---|
[752] | 175 | This will print the quota status for all users who use the lp printer. |
---|
[731] | 176 | |
---|
| 177 | $ repykota |
---|
| 178 | |
---|
| 179 | This will print the quota status for all users on all printers. |
---|
| 180 | |
---|
| 181 | This program is free software; you can redistribute it and/or modify |
---|
| 182 | it under the terms of the GNU General Public License as published by |
---|
| 183 | the Free Software Foundation; either version 2 of the License, or |
---|
| 184 | (at your option) any later version. |
---|
| 185 | |
---|
| 186 | This program is distributed in the hope that it will be useful, |
---|
| 187 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 188 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 189 | GNU General Public License for more details. |
---|
| 190 | |
---|
| 191 | You should have received a copy of the GNU General Public License |
---|
| 192 | along with this program; if not, write to the Free Software |
---|
| 193 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 194 | |
---|
| 195 | Please e-mail bugs to: %s""" % (version.__version__, version.__author__) |
---|
| 196 | |
---|
| 197 | class RePyKota(PyKotaTool) : |
---|
| 198 | """A class for repykota.""" |
---|
| 199 | def main(self, options) : |
---|
| 200 | """Print Quota reports generator.""" |
---|
[900] | 201 | printers = self.storage.getMatchingPrinters(options["printer"]) |
---|
| 202 | if not printers : |
---|
[772] | 203 | raise PyKotaToolError, _("There's no printer matching %s") % options["printer"] |
---|
[900] | 204 | for (printerid, printer) in printers : |
---|
[912] | 205 | print _("*** Report for %s quota on printer %s") % ((options["groups"] and "group") or "user", printer) |
---|
[950] | 206 | print _("Pages grace time: %i days") % self.config.getGraceDelay(printer) |
---|
| 207 | prices = self.storage.getPrinterPrices(printerid) |
---|
| 208 | if prices is None : |
---|
[970] | 209 | (perpage, perjob) = (None, None) |
---|
[950] | 210 | else : |
---|
| 211 | (perpage, perjob) = prices |
---|
[970] | 212 | if perjob is not None : |
---|
| 213 | print _("Price per job: %.3f") % perjob |
---|
| 214 | if perpage is not None : |
---|
| 215 | print _("Price per page: %.3f") % perpage |
---|
[770] | 216 | total = 0 |
---|
[929] | 217 | totalmoney = 0.0 |
---|
[890] | 218 | if options["groups"] : |
---|
[929] | 219 | print _("Group used soft hard balance grace total paid") |
---|
| 220 | print "------------------------------------------------------------------------------" |
---|
[927] | 221 | for (ident, name) in self.storage.getPrinterGroups(printerid) : |
---|
[900] | 222 | quota = self.storage.getGroupPQuota(ident, printerid) |
---|
[929] | 223 | balance = self.storage.getGroupBalance(ident) |
---|
| 224 | limitby = self.storage.getGroupLimitBy(ident) |
---|
| 225 | (pages, money) = self.printQuota(name, quota, balance, limitby) |
---|
| 226 | total += pages |
---|
| 227 | totalmoney += money |
---|
[890] | 228 | else : |
---|
| 229 | # default is user quota report |
---|
[929] | 230 | print _("User used soft hard balance grace total paid") |
---|
| 231 | print "------------------------------------------------------------------------------" |
---|
[900] | 232 | for (ident, name) in self.storage.getPrinterUsers(printerid) : |
---|
| 233 | quota = self.storage.getUserPQuota(ident, printerid) |
---|
[929] | 234 | balance = self.storage.getUserBalance(ident) |
---|
| 235 | limitby = self.storage.getUserLimitBy(ident) |
---|
| 236 | (pages, money) = self.printQuota(name, quota, balance, limitby) |
---|
| 237 | total += pages |
---|
| 238 | totalmoney += money |
---|
| 239 | if total or totalmoney : |
---|
[946] | 240 | print (" " * 50) + (_("Total : %9i") % total) + ("%11s" % ("%7.2f" % totalmoney)[:11]) |
---|
[900] | 241 | printerpagecounter = self.storage.getPrinterPageCounter(printerid) |
---|
[915] | 242 | try : |
---|
| 243 | msg = "%9i" % printerpagecounter["pagecounter"] |
---|
| 244 | except TypeError : |
---|
[814] | 245 | msg = _("unknown") |
---|
[929] | 246 | print (" " * 51) + (_("Real : %s") % msg) |
---|
[734] | 247 | print |
---|
[929] | 248 | if options["groups"] : |
---|
| 249 | print _("Totals may be inaccurate if some users are members of several groups.") |
---|
[731] | 250 | |
---|
[929] | 251 | def printQuota(self, name, quota, balance, limitby) : |
---|
[731] | 252 | """Prints the quota information.""" |
---|
| 253 | if quota is not None : |
---|
[927] | 254 | lifepagecounter = quota["lifepagecounter"] or 0 |
---|
| 255 | pagecounter = quota["pagecounter"] or 0 |
---|
[843] | 256 | softlimit = quota["softlimit"] |
---|
| 257 | hardlimit = quota["hardlimit"] |
---|
[731] | 258 | datelimit = quota["datelimit"] |
---|
[929] | 259 | if balance is not None : |
---|
| 260 | (balance, lifetimepaid) = balance |
---|
| 261 | else : |
---|
[1017] | 262 | (balance, lifetimepaid) = (0.0, 0.0) |
---|
[731] | 263 | if datelimit is not None : |
---|
| 264 | now = DateTime.now() |
---|
| 265 | datelimit = DateTime.ISO.ParseDateTime(datelimit) |
---|
[733] | 266 | if now >= datelimit : |
---|
[731] | 267 | datelimit = "DENY" |
---|
| 268 | else : |
---|
| 269 | datelimit = "" |
---|
[929] | 270 | if limitby == "balance" : |
---|
| 271 | reached = (((balance <= 0) and "+") or "-") + "B" |
---|
| 272 | else : |
---|
| 273 | reached = (((softlimit is not None) and (pagecounter >= softlimit) and "+") or "-") + "Q" |
---|
[949] | 274 | balance = balance or 0.0 |
---|
| 275 | lifetimepaid = lifetimepaid or 0.0 |
---|
| 276 | strbalance = ("%5.2f" % balance)[:10] |
---|
| 277 | strlifetimepaid = ("%6.2f" % lifetimepaid)[:10] |
---|
[931] | 278 | print "%-9.9s %s %7i %7s %7s %10s %-10.10s %8i %10s" % (name, reached, pagecounter, str(softlimit), str(hardlimit), strbalance, str(datelimit)[:10], lifepagecounter, strlifetimepaid) |
---|
[929] | 279 | return (lifepagecounter, lifetimepaid) |
---|
[1017] | 280 | else : |
---|
| 281 | return (0, 0) |
---|
[905] | 282 | |
---|
[731] | 283 | |
---|
| 284 | if __name__ == "__main__" : |
---|
| 285 | try : |
---|
| 286 | defaults = { \ |
---|
| 287 | "printer" : "*", \ |
---|
| 288 | } |
---|
| 289 | short_options = "vhugP:" |
---|
| 290 | long_options = ["help", "version", "users", "groups", "printer="] |
---|
| 291 | |
---|
| 292 | # Initializes the command line tool |
---|
[1005] | 293 | reporter = RePyKota(asadmin=0, doc=__doc__) |
---|
[731] | 294 | |
---|
| 295 | # parse and checks the command line |
---|
| 296 | (options, args) = reporter.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
| 297 | |
---|
| 298 | # sets long options |
---|
| 299 | options["help"] = options["h"] or options["help"] |
---|
| 300 | options["version"] = options["v"] or options["version"] |
---|
[895] | 301 | options["users"] = options["u"] or options["users"] |
---|
| 302 | options["groups"] = options["g"] or options["groups"] |
---|
[731] | 303 | options["printer"] = options["P"] or options["printer"] or defaults["printer"] |
---|
| 304 | |
---|
| 305 | if options["help"] : |
---|
| 306 | reporter.display_usage_and_quit() |
---|
| 307 | elif options["version"] : |
---|
| 308 | reporter.display_version_and_quit() |
---|
| 309 | elif options["users"] and options["groups"] : |
---|
[842] | 310 | raise PyKotaToolError, _("incompatible options, see help.") |
---|
[731] | 311 | elif args : |
---|
[842] | 312 | raise PyKotaToolError, _("unused arguments [%s]. Aborting.") % ", ".join(args) |
---|
[731] | 313 | else : |
---|
| 314 | sys.exit(reporter.main(options)) |
---|
[975] | 315 | except (PyKotaToolError, PyKotaConfigError, PyKotaStorageError), msg : |
---|
[731] | 316 | sys.stderr.write("%s\n" % msg) |
---|
| 317 | sys.stderr.flush() |
---|
| 318 | sys.exit(-1) |
---|