#! /usr/bin/env python # PyKota Print Quota Reports generator # # PyKota - Print Quotas for CUPS # # (c) 2003 Jerome Alet # You're welcome to redistribute this software under the # terms of the GNU General Public Licence version 2.0 # or, at your option, any higher version. # # You can read the complete GNU GPL in the file COPYING # which should come along with this software, or visit # the Free Software Foundation's WEB site http://www.fsf.org # # $Id$ # # $Log$ # Revision 1.9 2003/02/09 13:40:29 jalet # typo # # Revision 1.8 2003/02/09 12:56:53 jalet # Internationalization begins... # # Revision 1.7 2003/02/08 23:17:20 jalet # repykota now outputs life time page counters and the total pages printed by # all users/groups on each printer. # # Revision 1.6 2003/02/07 23:39:16 jalet # Typos # # Revision 1.5 2003/02/07 08:38:36 jalet # Missing conversion. # empty line between two printers # # Revision 1.4 2003/02/07 08:34:15 jalet # Test wrt date limit was wrong # # Revision 1.3 2003/02/07 00:08:52 jalet # Typos # # Revision 1.2 2003/02/06 23:58:05 jalet # repykota should be ok # # # import sys from mx import DateTime from pykota import version from pykota.tool import PyKotaTool, PyKotaToolError __doc__ = """repykota v%s (C) 2003 C@LL - Conseil Internet & Logiciels Libres Generates print quota reports. command line usage : repykota [options] options : -v | --version Prints repykota's version number then exits. -h | --help Prints this message then exits. -u | --users Generates a report on users quota, this is the default. -g | --groups Generates a report on group quota instead of users. -P | --printer p Report quotas on this printer only. Actually p can use wildcards characters to select only some printers. The default value is *, meaning all printers. examples : $ repykota --printer lp This will print the quota status for all users who use the lp printer. $ repykota This will print the quota status for all users on all printers. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. Please e-mail bugs to: %s""" % (version.__version__, version.__author__) class RePyKota(PyKotaTool) : """A class for repykota.""" def main(self, options) : """Print Quota reports generator.""" printernames = self.storage.getMatchingPrinters(options["printer"]) if not printernames : raise PyKotaToolError, _("There's no printer matching %s") % options["printer"] for printer in printernames : print _("*** Report for %s quota on printer %s") % ((options["users"] and "user") or "group", printer) print _("Pages grace time: %idays") % self.config.getGraceDelay() total = 0 if options["users"] : print _("User used soft hard grace total") print "------------------------------------------------------------" for name in self.storage.getPrinterUsers(printer) : quota = self.storage.getUserPQuota(name, printer) total += self.printQuota(name, quota) else : print _("Group used soft hard grace total") print "------------------------------------------------------------" for name in self.storage.getPrinterGroups(printer) : quota = self.storage.getGroupPQuota(name, printer) total += self.printQuota(name, quota) if total : print (" " * 43) + ("Total : %9i" % total) print def printQuota(self, name, quota) : """Prints the quota information.""" if quota is not None : lifepagecounter = quota["lifepagecounter"] pagecounter = quota["pagecounter"] softlimit = quota["softlimit"] or 0 hardlimit = quota["hardlimit"] or 0 datelimit = quota["datelimit"] if datelimit is not None : now = DateTime.now() datelimit = DateTime.ISO.ParseDateTime(datelimit) if now >= datelimit : datelimit = "DENY" else : datelimit = "" reached = ((pagecounter >= softlimit) and "+") or "-" print "%-10.10s %c %8i %8i %8i %10s %9i" % (name, reached, pagecounter, softlimit, hardlimit, str(datelimit)[:10], lifepagecounter) return lifepagecounter if __name__ == "__main__" : try : defaults = { \ "users" : 1, \ "groups" : 0, \ "printer" : "*", \ } short_options = "vhugP:" long_options = ["help", "version", "users", "groups", "printer="] # Initializes the command line tool reporter = RePyKota(doc=__doc__) # parse and checks the command line (options, args) = reporter.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) # sets long options options["help"] = options["h"] or options["help"] options["version"] = options["v"] or options["version"] options["users"] = options["u"] or options["users"] or defaults["users"] options["groups"] = options["g"] or options["groups"] or defaults["groups"] options["printer"] = options["P"] or options["printer"] or defaults["printer"] if options["help"] : reporter.display_usage_and_quit() elif options["version"] : reporter.display_version_and_quit() elif options["users"] and options["groups"] : raise PyKotaToolError, _("repykota: options --users and --groups are incompatible.") elif options["groups"] : raise PyKotaToolError, _("repykota: option --groups is currently not implemented.") elif args : raise PyKotaToolError, _("repykota: unused arguments [%s]. Aborting.") % ", ".join(args) else : sys.exit(reporter.main(options)) except PyKotaToolError, msg : sys.stderr.write("%s\n" % msg) sys.stderr.flush() sys.exit(-1)