root / pykota / trunk / bin / repykota @ 1785

Revision 1785, 11.0 kB (checked in by jalet, 20 years ago)

Minor changes to allow any PyKota administrator to launch enhanced versions
of the commands, and not only the root user.

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