root / pykota / trunk / bin / repykota @ 1796

Revision 1796, 11.1 kB (checked in by jalet, 20 years ago)

Renders help translatable

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[696]1#! /usr/bin/env python
[1144]2# -*- coding: ISO-8859-15 -*-
[696]3
[731]4# PyKota Print Quota Reports generator
[696]5#
[952]6# PyKota - Print Quotas for CUPS and LPRng
[696]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.
[696]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.
[696]22#
23# $Id$
24#
25# $Log$
[1796]26# Revision 1.51  2004/10/11 12:49:06  jalet
27# Renders help translatable
28#
[1785]29# Revision 1.50  2004/10/06 10:05:47  jalet
30# Minor changes to allow any PyKota administrator to launch enhanced versions
31# of the commands, and not only the root user.
32#
[1546]33# Revision 1.49  2004/06/18 13:34:49  jalet
34# Now all tracebacks include PyKota's version number
35#
[1526]36# Revision 1.48  2004/06/07 18:43:41  jalet
37# Fixed over-verbose exits when displaying help or version number
38#
[1517]39# Revision 1.47  2004/06/03 21:50:34  jalet
40# Improved error logging.
41# crashrecipient directive added.
42# Now exports the job's size in bytes too.
43#
[1257]44# Revision 1.46  2004/01/08 14:10:32  jalet
45# Copyright year changed.
46#
[1156]47# Revision 1.45  2003/10/09 21:25:25  jalet
48# Multiple printer names or wildcards can be passed on the command line
49# separated with commas.
50# Beta phase.
51#
[1144]52# Revision 1.44  2003/10/07 09:07:27  jalet
53# Character encoding added to please latest version of Python
54#
[1117]55# Revision 1.43  2003/08/18 16:35:28  jalet
56# New pychecker pass, on the tools this time.
57#
[1113]58# Revision 1.42  2003/07/29 20:55:17  jalet
59# 1.14 is out !
60#
[1071]61# Revision 1.41  2003/07/07 12:19:52  jalet
62# Now repykota and warnpykota --groups check the groups the user is a member of
63# in the print quota database, not in the system passwd/group files
64#
[1048]65# Revision 1.40  2003/06/30 12:46:15  jalet
66# Extracted reporting code.
67#
[1041]68# Revision 1.39  2003/06/25 14:10:01  jalet
69# Hey, it may work (edpykota --reset excepted) !
70#
[1030]71# Revision 1.38  2003/06/15 22:26:52  jalet
72# More work on LDAP
73#
[1021]74# Revision 1.37  2003/06/10 16:37:54  jalet
75# Deletion of the second user which is not needed anymore.
76# Added a debug configuration field in /etc/pykota.conf
77# All queries can now be sent to the logger in debug mode, this will
78# greatly help improve performance when time for this will come.
79#
[1017]80# Revision 1.36  2003/06/06 14:21:08  jalet
81# New LDAP schema.
82# Small bug fixes.
83#
[1005]84# Revision 1.35  2003/05/28 13:56:24  jalet
85# Unneeded code.
86#
[975]87# Revision 1.34  2003/04/29 22:03:38  jalet
88# Better error handling.
89#
[970]90# Revision 1.33  2003/04/28 08:25:48  jalet
91# Small display bug in repykota was fixed.
92#
[952]93# Revision 1.32  2003/04/23 22:13:56  jalet
94# Preliminary support for LPRng added BUT STILL UNTESTED.
95#
[950]96# Revision 1.31  2003/04/23 09:58:17  jalet
97# Prices per page/job are now displayed
98#
[949]99# Revision 1.30  2003/04/18 08:34:51  jalet
100# Minor fix.
101#
[946]102# Revision 1.29  2003/04/18 08:29:30  jalet
103# Minor bug fix
104#
[931]105# Revision 1.28  2003/04/17 09:36:30  jalet
106# Bad alignment in output
107#
[929]108# Revision 1.27  2003/04/17 09:26:21  jalet
109# repykota now reports account balances too.
110#
[927]111# Revision 1.26  2003/04/16 12:35:49  jalet
112# Groups quota work now !
113#
[915]114# Revision 1.25  2003/04/15 11:30:57  jalet
115# More work done on money print charging.
116# Minor bugs corrected.
117# All tools now access to the storage as priviledged users, repykota excepted.
118#
[912]119# Revision 1.24  2003/04/14 20:05:20  jalet
120# Reversed test.
121#
[905]122# Revision 1.23  2003/04/11 14:42:54  jalet
123# Translations
[904]124#
[900]125# Revision 1.21  2003/04/10 21:47:20  jalet
126# Job history added. Upgrade script neutralized for now !
127#
[895]128# Revision 1.20  2003/04/08 21:31:39  jalet
129# (anything or 0) = anything !!! Go back to school Jerome !
130#
[891]131# Revision 1.19  2003/04/08 21:13:44  jalet
132# Prepare --groups option to work.
133#
[890]134# Revision 1.18  2003/04/08 21:10:18  jalet
135# Checks --groups option presence instead of --users because --users is the default.
136#
[873]137# Revision 1.17  2003/03/29 13:45:27  jalet
138# GPL paragraphs were incorrectly (from memory) copied into the sources.
139# Two README files were added.
140# Upgrade script for PostgreSQL pre 1.01 schema was added.
141#
[843]142# Revision 1.16  2003/03/09 23:56:21  jalet
143# Option noquota added to do accounting only.
144#
[842]145# Revision 1.15  2003/03/09 23:39:14  jalet
146# Simplified translations.
147#
[814]148# Revision 1.14  2003/02/27 09:04:02  jalet
149# Missing translation
150#
[813]151# Revision 1.13  2003/02/27 08:44:01  jalet
152# Check to see if the printer was ever used at all, and displays "unknown"
153# as the pagecounter value in this casCheck to see if the printer was ever used at all, and displays "unknown"
154# as the pagecounter value in this case.
155#
[805]156# Revision 1.12  2003/02/17 23:02:23  jalet
157# getGraceDelay for printer
158#
[793]159# Revision 1.11  2003/02/10 12:12:34  jalet
160# Translations.
161#
[791]162# Revision 1.10  2003/02/10 12:07:30  jalet
163# Now repykota should output the recorded total page number for each printer too.
164#
[775]165# Revision 1.9  2003/02/09 13:40:29  jalet
166# typo
167#
[772]168# Revision 1.8  2003/02/09 12:56:53  jalet
169# Internationalization begins...
170#
[770]171# Revision 1.7  2003/02/08 23:17:20  jalet
172# repykota now outputs life time page counters and the total pages printed by
173# all users/groups on each printer.
174#
[752]175# Revision 1.6  2003/02/07 23:39:16  jalet
176# Typos
177#
[734]178# Revision 1.5  2003/02/07 08:38:36  jalet
179# Missing conversion.
180# empty line between two printers
181#
[733]182# Revision 1.4  2003/02/07 08:34:15  jalet
183# Test wrt date limit was wrong
184#
[732]185# Revision 1.3  2003/02/07 00:08:52  jalet
186# Typos
187#
[731]188# Revision 1.2  2003/02/06 23:58:05  jalet
189# repykota should be ok
[696]190#
191#
192#
193
[731]194import sys
[1041]195import os
196import pwd
[731]197
198from mx import DateTime
199
200from pykota import version
[1796]201from pykota.tool import PyKotaTool, PyKotaToolError, crashed, N_
[975]202from pykota.config import PyKotaConfigError
203from pykota.storage import PyKotaStorageError
[1048]204from pykota.reporter import PyKotaReporterError
205from pykota import reporter
[731]206
[1796]207__doc__ = N_("""repykota v%s (c) 2003-2004 C@LL - Conseil Internet & Logiciels Libres
[731]208
209Generates print quota reports.
210
211command line usage :
212
213  repykota [options]
214
215options :
216
217  -v | --version       Prints repykota's version number then exits.
218  -h | --help          Prints this message then exits.
219 
220  -u | --users         Generates a report on users quota, this is
221                       the default.
222 
223  -g | --groups        Generates a report on group quota instead of users.
224 
225  -P | --printer p     Report quotas on this printer only. Actually p can
226                       use wildcards characters to select only
227                       some printers. The default value is *, meaning
228                       all printers.
[1156]229                       You can specify several names or wildcards,
230                       by separating them with commas.
[731]231 
232examples :                             
233
234  $ repykota --printer lp
235 
[752]236  This will print the quota status for all users who use the lp printer.
[731]237
238  $ repykota
239 
240  This will print the quota status for all users on all printers.
[1041]241 
[1156]242  $ repykota --printer "laser*,*pson" jerome "jo*"
[1041]243 
244  This will print the quota status for user jerome and all users
[1156]245  whose name begins with "jo" on all printers which name begins
246  with "laser" or ends with "pson".
[1041]247 
[1785]248  If launched by an user who is not a PyKota administrator, additionnal
249  arguments representing users or groups names are ignored, and only the
250  current user/group is reported.
[731]251
252This program is free software; you can redistribute it and/or modify
253it under the terms of the GNU General Public License as published by
254the Free Software Foundation; either version 2 of the License, or
255(at your option) any later version.
256
257This program is distributed in the hope that it will be useful,
258but WITHOUT ANY WARRANTY; without even the implied warranty of
259MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
260GNU General Public License for more details.
261
262You should have received a copy of the GNU General Public License
263along with this program; if not, write to the Free Software
264Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
265
[1796]266Please e-mail bugs to: %s""") % (version.__version__, version.__author__)
[731]267       
268class RePyKota(PyKotaTool) :       
269    """A class for repykota."""
[1041]270    def main(self, ugnames, options) :
[731]271        """Print Quota reports generator."""
[1785]272        if self.config.isAdmin :
273            # PyKota administrator
[1041]274            if not ugnames :
275                # no username, means all usernames
276                ugnames = [ "*" ]
277        else :       
278            # not the root user
279            # reports only the current user
[1785]280            username = pwd.getpwuid(os.geteuid())[0]
[1041]281            if options["groups"] :
[1071]282                user = self.storage.getUser(username)
283                if user.Exists :
284                    ugnames = [ g.Name for g in self.storage.getUserGroups(user) ]
285                else :   
286                    ugnames = [ ]
[1041]287            else :
[1071]288                ugnames = [ username ]
[1041]289       
[900]290        printers = self.storage.getMatchingPrinters(options["printer"])
291        if not printers :
[772]292            raise PyKotaToolError, _("There's no printer matching %s") % options["printer"]
[1041]293           
[1048]294        self.reportingtool = reporter.openReporter(self, "text", printers, ugnames, (options["groups"] and 1) or 0)   
295        print self.reportingtool.generateReport()
[731]296                   
297if __name__ == "__main__" : 
[1526]298    retcode = 0
[731]299    try :
300        defaults = { \
301                     "printer" : "*", \
302                   }
303        short_options = "vhugP:"
304        long_options = ["help", "version", "users", "groups", "printer="]
305       
306        # Initializes the command line tool
[1048]307        reportTool = RePyKota(doc=__doc__)
[731]308       
309        # parse and checks the command line
[1048]310        (options, args) = reportTool.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
[731]311       
312        # sets long options
313        options["help"] = options["h"] or options["help"]
314        options["version"] = options["v"] or options["version"]
[895]315        options["users"] = options["u"] or options["users"]
316        options["groups"] = options["g"] or options["groups"]
[731]317        options["printer"] = options["P"] or options["printer"] or defaults["printer"]
318       
319        if options["help"] :
[1048]320            reportTool.display_usage_and_quit()
[731]321        elif options["version"] :
[1048]322            reportTool.display_version_and_quit()
[731]323        elif options["users"] and options["groups"] :   
[842]324            raise PyKotaToolError, _("incompatible options, see help.")
[731]325        else :
[1113]326            retcode = reportTool.main(args, options)
[1526]327    except SystemExit :       
328        pass
[1517]329    except :
330        try :
331            reportTool.crashed("repykota failed")
332        except :   
[1546]333            crashed("repykota failed")
[1517]334        retcode = -1
[731]335
[1113]336    try :
337        reportTool.storage.close()
338    except (TypeError, NameError, AttributeError) :   
339        pass
340       
341    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.