root / pykota / trunk / bin / repykota @ 1517

Revision 1517, 10.5 kB (checked in by jalet, 20 years ago)

Improved error logging.
crashrecipient directive added.
Now exports the job's size in bytes too.

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