root / pykota / trunk / bin / repykota @ 2028

Revision 2028, 11.2 kB (checked in by jalet, 19 years ago)

Modified copyright years

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