root / pykota / trunk / bin / repykota @ 1113

Revision 1113, 9.8 kB (checked in by jalet, 21 years ago)

1.14 is out !

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