root / pykota / trunk / bin / repykota @ 1188

Revision 1156, 10.3 kB (checked in by jalet, 21 years ago)

Multiple printer names or wildcards can be passed on the command line
separated with commas.
Beta phase.

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