root / pykota / trunk / bin / repykota @ 1048

Revision 1048, 9.2 kB (checked in by jalet, 21 years ago)

Extracted reporting code.

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