root / pykota / trunk / bin / repykota @ 1117

Revision 1117, 9.9 kB (checked in by jalet, 21 years ago)

New pychecker pass, on the tools this time.

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