#! /usr/bin/env python # -*- coding: ISO-8859-15 -*- # PyKota Print Quota Reports generator # # PyKota - Print Quotas for CUPS and LPRng # # (c) 2003, 2004, 2005 Jerome Alet # 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. # # $Id$ # # $Log$ # Revision 1.53 2005/01/17 08:44:24 jalet # Modified copyright years # # Revision 1.52 2004/10/11 22:53:05 jalet # Postponed string interpolation to help message's output method # # Revision 1.51 2004/10/11 12:49:06 jalet # Renders help translatable # # Revision 1.50 2004/10/06 10:05:47 jalet # Minor changes to allow any PyKota administrator to launch enhanced versions # of the commands, and not only the root user. # # Revision 1.49 2004/06/18 13:34:49 jalet # Now all tracebacks include PyKota's version number # # Revision 1.48 2004/06/07 18:43:41 jalet # Fixed over-verbose exits when displaying help or version number # # Revision 1.47 2004/06/03 21:50:34 jalet # Improved error logging. # crashrecipient directive added. # Now exports the job's size in bytes too. # # Revision 1.46 2004/01/08 14:10:32 jalet # Copyright year changed. # # Revision 1.45 2003/10/09 21:25:25 jalet # Multiple printer names or wildcards can be passed on the command line # separated with commas. # Beta phase. # # Revision 1.44 2003/10/07 09:07:27 jalet # Character encoding added to please latest version of Python # # Revision 1.43 2003/08/18 16:35:28 jalet # New pychecker pass, on the tools this time. # # Revision 1.42 2003/07/29 20:55:17 jalet # 1.14 is out ! # # Revision 1.41 2003/07/07 12:19:52 jalet # Now repykota and warnpykota --groups check the groups the user is a member of # in the print quota database, not in the system passwd/group files # # Revision 1.40 2003/06/30 12:46:15 jalet # Extracted reporting code. # # Revision 1.39 2003/06/25 14:10:01 jalet # Hey, it may work (edpykota --reset excepted) ! # # Revision 1.38 2003/06/15 22:26:52 jalet # More work on LDAP # # Revision 1.37 2003/06/10 16:37:54 jalet # Deletion of the second user which is not needed anymore. # Added a debug configuration field in /etc/pykota.conf # All queries can now be sent to the logger in debug mode, this will # greatly help improve performance when time for this will come. # # Revision 1.36 2003/06/06 14:21:08 jalet # New LDAP schema. # Small bug fixes. # # Revision 1.35 2003/05/28 13:56:24 jalet # Unneeded code. # # Revision 1.34 2003/04/29 22:03:38 jalet # Better error handling. # # Revision 1.33 2003/04/28 08:25:48 jalet # Small display bug in repykota was fixed. # # Revision 1.32 2003/04/23 22:13:56 jalet # Preliminary support for LPRng added BUT STILL UNTESTED. # # Revision 1.31 2003/04/23 09:58:17 jalet # Prices per page/job are now displayed # # Revision 1.30 2003/04/18 08:34:51 jalet # Minor fix. # # Revision 1.29 2003/04/18 08:29:30 jalet # Minor bug fix # # Revision 1.28 2003/04/17 09:36:30 jalet # Bad alignment in output # # Revision 1.27 2003/04/17 09:26:21 jalet # repykota now reports account balances too. # # Revision 1.26 2003/04/16 12:35:49 jalet # Groups quota work now ! # # Revision 1.25 2003/04/15 11:30:57 jalet # More work done on money print charging. # Minor bugs corrected. # All tools now access to the storage as priviledged users, repykota excepted. # # Revision 1.24 2003/04/14 20:05:20 jalet # Reversed test. # # Revision 1.23 2003/04/11 14:42:54 jalet # Translations # # Revision 1.21 2003/04/10 21:47:20 jalet # Job history added. Upgrade script neutralized for now ! # # Revision 1.20 2003/04/08 21:31:39 jalet # (anything or 0) = anything !!! Go back to school Jerome ! # # Revision 1.19 2003/04/08 21:13:44 jalet # Prepare --groups option to work. # # Revision 1.18 2003/04/08 21:10:18 jalet # Checks --groups option presence instead of --users because --users is the default. # # Revision 1.17 2003/03/29 13:45:27 jalet # GPL paragraphs were incorrectly (from memory) copied into the sources. # Two README files were added. # Upgrade script for PostgreSQL pre 1.01 schema was added. # # Revision 1.16 2003/03/09 23:56:21 jalet # Option noquota added to do accounting only. # # Revision 1.15 2003/03/09 23:39:14 jalet # Simplified translations. # # Revision 1.14 2003/02/27 09:04:02 jalet # Missing translation # # Revision 1.13 2003/02/27 08:44:01 jalet # Check to see if the printer was ever used at all, and displays "unknown" # as the pagecounter value in this casCheck to see if the printer was ever used at all, and displays "unknown" # as the pagecounter value in this case. # # Revision 1.12 2003/02/17 23:02:23 jalet # getGraceDelay for printer # # Revision 1.11 2003/02/10 12:12:34 jalet # Translations. # # Revision 1.10 2003/02/10 12:07:30 jalet # Now repykota should output the recorded total page number for each printer too. # # 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 import os import pwd from mx import DateTime from pykota.tool import PyKotaTool, PyKotaToolError, crashed, N_ from pykota.config import PyKotaConfigError from pykota.storage import PyKotaStorageError from pykota.reporter import PyKotaReporterError from pykota import reporter __doc__ = N_("""repykota v%s (c) 2003, 2004, 2005 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. You can specify several names or wildcards, by separating them with commas. 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. $ repykota --printer "laser*,*pson" jerome "jo*" This will print the quota status for user jerome and all users whose name begins with "jo" on all printers which name begins with "laser" or ends with "pson". If launched by an user who is not a PyKota administrator, additionnal arguments representing users or groups names are ignored, and only the current user/group is reported. 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""") class RePyKota(PyKotaTool) : """A class for repykota.""" def main(self, ugnames, options) : """Print Quota reports generator.""" if self.config.isAdmin : # PyKota administrator if not ugnames : # no username, means all usernames ugnames = [ "*" ] else : # not the root user # reports only the current user username = pwd.getpwuid(os.geteuid())[0] if options["groups"] : user = self.storage.getUser(username) if user.Exists : ugnames = [ g.Name for g in self.storage.getUserGroups(user) ] else : ugnames = [ ] else : ugnames = [ username ] printers = self.storage.getMatchingPrinters(options["printer"]) if not printers : raise PyKotaToolError, _("There's no printer matching %s") % options["printer"] self.reportingtool = reporter.openReporter(self, "text", printers, ugnames, (options["groups"] and 1) or 0) print self.reportingtool.generateReport() if __name__ == "__main__" : retcode = 0 try : defaults = { \ "printer" : "*", \ } short_options = "vhugP:" long_options = ["help", "version", "users", "groups", "printer="] # Initializes the command line tool reportTool = RePyKota(doc=__doc__) # parse and checks the command line (options, args) = reportTool.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"] options["groups"] = options["g"] or options["groups"] options["printer"] = options["P"] or options["printer"] or defaults["printer"] if options["help"] : reportTool.display_usage_and_quit() elif options["version"] : reportTool.display_version_and_quit() elif options["users"] and options["groups"] : raise PyKotaToolError, _("incompatible options, see help.") else : retcode = reportTool.main(args, options) except SystemExit : pass except : try : reportTool.crashed("repykota failed") except : crashed("repykota failed") retcode = -1 try : reportTool.storage.close() except (TypeError, NameError, AttributeError) : pass sys.exit(retcode)