[696] | 1 | #! /usr/bin/env python |
---|
| 2 | |
---|
[731] | 3 | # PyKota Print Quota Reports generator |
---|
[696] | 4 | # |
---|
| 5 | # PyKota - Print Quotas for CUPS |
---|
| 6 | # |
---|
| 7 | # (c) 2003 Jerome Alet <alet@librelogiciel.com> |
---|
| 8 | # You're welcome to redistribute this software under the |
---|
| 9 | # terms of the GNU General Public Licence version 2.0 |
---|
| 10 | # or, at your option, any higher version. |
---|
| 11 | # |
---|
| 12 | # You can read the complete GNU GPL in the file COPYING |
---|
| 13 | # which should come along with this software, or visit |
---|
| 14 | # the Free Software Foundation's WEB site http://www.fsf.org |
---|
| 15 | # |
---|
| 16 | # $Id$ |
---|
| 17 | # |
---|
| 18 | # $Log$ |
---|
[775] | 19 | # Revision 1.9 2003/02/09 13:40:29 jalet |
---|
| 20 | # typo |
---|
| 21 | # |
---|
[772] | 22 | # Revision 1.8 2003/02/09 12:56:53 jalet |
---|
| 23 | # Internationalization begins... |
---|
| 24 | # |
---|
[770] | 25 | # Revision 1.7 2003/02/08 23:17:20 jalet |
---|
| 26 | # repykota now outputs life time page counters and the total pages printed by |
---|
| 27 | # all users/groups on each printer. |
---|
| 28 | # |
---|
[752] | 29 | # Revision 1.6 2003/02/07 23:39:16 jalet |
---|
| 30 | # Typos |
---|
| 31 | # |
---|
[734] | 32 | # Revision 1.5 2003/02/07 08:38:36 jalet |
---|
| 33 | # Missing conversion. |
---|
| 34 | # empty line between two printers |
---|
| 35 | # |
---|
[733] | 36 | # Revision 1.4 2003/02/07 08:34:15 jalet |
---|
| 37 | # Test wrt date limit was wrong |
---|
| 38 | # |
---|
[732] | 39 | # Revision 1.3 2003/02/07 00:08:52 jalet |
---|
| 40 | # Typos |
---|
| 41 | # |
---|
[731] | 42 | # Revision 1.2 2003/02/06 23:58:05 jalet |
---|
| 43 | # repykota should be ok |
---|
[696] | 44 | # |
---|
| 45 | # |
---|
| 46 | # |
---|
| 47 | |
---|
[731] | 48 | import sys |
---|
| 49 | |
---|
| 50 | from mx import DateTime |
---|
| 51 | |
---|
| 52 | from pykota import version |
---|
| 53 | from pykota.tool import PyKotaTool, PyKotaToolError |
---|
| 54 | |
---|
| 55 | __doc__ = """repykota v%s (C) 2003 C@LL - Conseil Internet & Logiciels Libres |
---|
| 56 | |
---|
| 57 | Generates print quota reports. |
---|
| 58 | |
---|
| 59 | command line usage : |
---|
| 60 | |
---|
| 61 | repykota [options] |
---|
| 62 | |
---|
| 63 | options : |
---|
| 64 | |
---|
| 65 | -v | --version Prints repykota's version number then exits. |
---|
| 66 | -h | --help Prints this message then exits. |
---|
| 67 | |
---|
| 68 | -u | --users Generates a report on users quota, this is |
---|
| 69 | the default. |
---|
| 70 | |
---|
| 71 | -g | --groups Generates a report on group quota instead of users. |
---|
| 72 | |
---|
| 73 | -P | --printer p Report quotas on this printer only. Actually p can |
---|
| 74 | use wildcards characters to select only |
---|
| 75 | some printers. The default value is *, meaning |
---|
| 76 | all printers. |
---|
| 77 | |
---|
| 78 | examples : |
---|
| 79 | |
---|
| 80 | $ repykota --printer lp |
---|
| 81 | |
---|
[752] | 82 | This will print the quota status for all users who use the lp printer. |
---|
[731] | 83 | |
---|
| 84 | $ repykota |
---|
| 85 | |
---|
| 86 | This will print the quota status for all users on all printers. |
---|
| 87 | |
---|
| 88 | This program is free software; you can redistribute it and/or modify |
---|
| 89 | it under the terms of the GNU General Public License as published by |
---|
| 90 | the Free Software Foundation; either version 2 of the License, or |
---|
| 91 | (at your option) any later version. |
---|
| 92 | |
---|
| 93 | This program is distributed in the hope that it will be useful, |
---|
| 94 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 95 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 96 | GNU General Public License for more details. |
---|
| 97 | |
---|
| 98 | You should have received a copy of the GNU General Public License |
---|
| 99 | along with this program; if not, write to the Free Software |
---|
| 100 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 101 | |
---|
| 102 | Please e-mail bugs to: %s""" % (version.__version__, version.__author__) |
---|
| 103 | |
---|
| 104 | class RePyKota(PyKotaTool) : |
---|
| 105 | """A class for repykota.""" |
---|
| 106 | def main(self, options) : |
---|
| 107 | """Print Quota reports generator.""" |
---|
| 108 | printernames = self.storage.getMatchingPrinters(options["printer"]) |
---|
| 109 | if not printernames : |
---|
[772] | 110 | raise PyKotaToolError, _("There's no printer matching %s") % options["printer"] |
---|
[731] | 111 | for printer in printernames : |
---|
[772] | 112 | print _("*** Report for %s quota on printer %s") % ((options["users"] and "user") or "group", printer) |
---|
| 113 | print _("Pages grace time: %idays") % self.config.getGraceDelay() |
---|
[770] | 114 | total = 0 |
---|
[731] | 115 | if options["users"] : |
---|
[772] | 116 | print _("User used soft hard grace total") |
---|
[770] | 117 | print "------------------------------------------------------------" |
---|
[731] | 118 | for name in self.storage.getPrinterUsers(printer) : |
---|
| 119 | quota = self.storage.getUserPQuota(name, printer) |
---|
[770] | 120 | total += self.printQuota(name, quota) |
---|
[731] | 121 | else : |
---|
[772] | 122 | print _("Group used soft hard grace total") |
---|
[770] | 123 | print "------------------------------------------------------------" |
---|
[731] | 124 | for name in self.storage.getPrinterGroups(printer) : |
---|
| 125 | quota = self.storage.getGroupPQuota(name, printer) |
---|
[770] | 126 | total += self.printQuota(name, quota) |
---|
| 127 | if total : |
---|
| 128 | print (" " * 43) + ("Total : %9i" % total) |
---|
[734] | 129 | print |
---|
[731] | 130 | |
---|
| 131 | def printQuota(self, name, quota) : |
---|
| 132 | """Prints the quota information.""" |
---|
| 133 | if quota is not None : |
---|
[770] | 134 | lifepagecounter = quota["lifepagecounter"] |
---|
[731] | 135 | pagecounter = quota["pagecounter"] |
---|
| 136 | softlimit = quota["softlimit"] or 0 |
---|
| 137 | hardlimit = quota["hardlimit"] or 0 |
---|
| 138 | datelimit = quota["datelimit"] |
---|
| 139 | if datelimit is not None : |
---|
| 140 | now = DateTime.now() |
---|
| 141 | datelimit = DateTime.ISO.ParseDateTime(datelimit) |
---|
[733] | 142 | if now >= datelimit : |
---|
[731] | 143 | datelimit = "DENY" |
---|
| 144 | else : |
---|
| 145 | datelimit = "" |
---|
| 146 | reached = ((pagecounter >= softlimit) and "+") or "-" |
---|
[770] | 147 | print "%-10.10s %c %8i %8i %8i %10s %9i" % (name, reached, pagecounter, softlimit, hardlimit, str(datelimit)[:10], lifepagecounter) |
---|
| 148 | return lifepagecounter |
---|
[731] | 149 | |
---|
| 150 | |
---|
| 151 | if __name__ == "__main__" : |
---|
| 152 | try : |
---|
| 153 | defaults = { \ |
---|
| 154 | "users" : 1, \ |
---|
| 155 | "groups" : 0, \ |
---|
| 156 | "printer" : "*", \ |
---|
| 157 | } |
---|
| 158 | short_options = "vhugP:" |
---|
| 159 | long_options = ["help", "version", "users", "groups", "printer="] |
---|
| 160 | |
---|
| 161 | # Initializes the command line tool |
---|
| 162 | reporter = RePyKota(doc=__doc__) |
---|
| 163 | |
---|
| 164 | # parse and checks the command line |
---|
| 165 | (options, args) = reporter.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
| 166 | |
---|
| 167 | # sets long options |
---|
| 168 | options["help"] = options["h"] or options["help"] |
---|
| 169 | options["version"] = options["v"] or options["version"] |
---|
| 170 | options["users"] = options["u"] or options["users"] or defaults["users"] |
---|
| 171 | options["groups"] = options["g"] or options["groups"] or defaults["groups"] |
---|
| 172 | options["printer"] = options["P"] or options["printer"] or defaults["printer"] |
---|
| 173 | |
---|
| 174 | if options["help"] : |
---|
| 175 | reporter.display_usage_and_quit() |
---|
| 176 | elif options["version"] : |
---|
| 177 | reporter.display_version_and_quit() |
---|
| 178 | elif options["users"] and options["groups"] : |
---|
[772] | 179 | raise PyKotaToolError, _("repykota: options --users and --groups are incompatible.") |
---|
[731] | 180 | elif options["groups"] : |
---|
[775] | 181 | raise PyKotaToolError, _("repykota: option --groups is currently not implemented.") |
---|
[731] | 182 | elif args : |
---|
[772] | 183 | raise PyKotaToolError, _("repykota: unused arguments [%s]. Aborting.") % ", ".join(args) |
---|
[731] | 184 | else : |
---|
| 185 | sys.exit(reporter.main(options)) |
---|
| 186 | except PyKotaToolError, msg : |
---|
| 187 | sys.stderr.write("%s\n" % msg) |
---|
| 188 | sys.stderr.flush() |
---|
| 189 | sys.exit(-1) |
---|