root / pykota / trunk / bin / repykota @ 1156

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
RevLine 
[696]1#! /usr/bin/env python
[1144]2# -*- coding: ISO-8859-15 -*-
[696]3
[731]4# PyKota Print Quota Reports generator
[696]5#
[952]6# PyKota - Print Quotas for CUPS and LPRng
[696]7#
8# (c) 2003 Jerome Alet <alet@librelogiciel.com>
[873]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.
[696]13#
[873]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.
[696]22#
23# $Id$
24#
25# $Log$
[1156]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#
[1144]31# Revision 1.44  2003/10/07 09:07:27  jalet
32# Character encoding added to please latest version of Python
33#
[1117]34# Revision 1.43  2003/08/18 16:35:28  jalet
35# New pychecker pass, on the tools this time.
36#
[1113]37# Revision 1.42  2003/07/29 20:55:17  jalet
38# 1.14 is out !
39#
[1071]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#
[1048]44# Revision 1.40  2003/06/30 12:46:15  jalet
45# Extracted reporting code.
46#
[1041]47# Revision 1.39  2003/06/25 14:10:01  jalet
48# Hey, it may work (edpykota --reset excepted) !
49#
[1030]50# Revision 1.38  2003/06/15 22:26:52  jalet
51# More work on LDAP
52#
[1021]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#
[1017]59# Revision 1.36  2003/06/06 14:21:08  jalet
60# New LDAP schema.
61# Small bug fixes.
62#
[1005]63# Revision 1.35  2003/05/28 13:56:24  jalet
64# Unneeded code.
65#
[975]66# Revision 1.34  2003/04/29 22:03:38  jalet
67# Better error handling.
68#
[970]69# Revision 1.33  2003/04/28 08:25:48  jalet
70# Small display bug in repykota was fixed.
71#
[952]72# Revision 1.32  2003/04/23 22:13:56  jalet
73# Preliminary support for LPRng added BUT STILL UNTESTED.
74#
[950]75# Revision 1.31  2003/04/23 09:58:17  jalet
76# Prices per page/job are now displayed
77#
[949]78# Revision 1.30  2003/04/18 08:34:51  jalet
79# Minor fix.
80#
[946]81# Revision 1.29  2003/04/18 08:29:30  jalet
82# Minor bug fix
83#
[931]84# Revision 1.28  2003/04/17 09:36:30  jalet
85# Bad alignment in output
86#
[929]87# Revision 1.27  2003/04/17 09:26:21  jalet
88# repykota now reports account balances too.
89#
[927]90# Revision 1.26  2003/04/16 12:35:49  jalet
91# Groups quota work now !
92#
[915]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#
[912]98# Revision 1.24  2003/04/14 20:05:20  jalet
99# Reversed test.
100#
[905]101# Revision 1.23  2003/04/11 14:42:54  jalet
102# Translations
[904]103#
[900]104# Revision 1.21  2003/04/10 21:47:20  jalet
105# Job history added. Upgrade script neutralized for now !
106#
[895]107# Revision 1.20  2003/04/08 21:31:39  jalet
108# (anything or 0) = anything !!! Go back to school Jerome !
109#
[891]110# Revision 1.19  2003/04/08 21:13:44  jalet
111# Prepare --groups option to work.
112#
[890]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#
[873]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#
[843]121# Revision 1.16  2003/03/09 23:56:21  jalet
122# Option noquota added to do accounting only.
123#
[842]124# Revision 1.15  2003/03/09 23:39:14  jalet
125# Simplified translations.
126#
[814]127# Revision 1.14  2003/02/27 09:04:02  jalet
128# Missing translation
129#
[813]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#
[805]135# Revision 1.12  2003/02/17 23:02:23  jalet
136# getGraceDelay for printer
137#
[793]138# Revision 1.11  2003/02/10 12:12:34  jalet
139# Translations.
140#
[791]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#
[775]144# Revision 1.9  2003/02/09 13:40:29  jalet
145# typo
146#
[772]147# Revision 1.8  2003/02/09 12:56:53  jalet
148# Internationalization begins...
149#
[770]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#
[752]154# Revision 1.6  2003/02/07 23:39:16  jalet
155# Typos
156#
[734]157# Revision 1.5  2003/02/07 08:38:36  jalet
158# Missing conversion.
159# empty line between two printers
160#
[733]161# Revision 1.4  2003/02/07 08:34:15  jalet
162# Test wrt date limit was wrong
163#
[732]164# Revision 1.3  2003/02/07 00:08:52  jalet
165# Typos
166#
[731]167# Revision 1.2  2003/02/06 23:58:05  jalet
168# repykota should be ok
[696]169#
170#
171#
172
[731]173import sys
[1041]174import os
175import pwd
[731]176
177from mx import DateTime
178
179from pykota import version
180from pykota.tool import PyKotaTool, PyKotaToolError
[975]181from pykota.config import PyKotaConfigError
182from pykota.storage import PyKotaStorageError
[1048]183from pykota.reporter import PyKotaReporterError
184from pykota import reporter
[731]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.
[1156]208                       You can specify several names or wildcards,
209                       by separating them with commas.
[731]210 
211examples :                             
212
213  $ repykota --printer lp
214 
[752]215  This will print the quota status for all users who use the lp printer.
[731]216
217  $ repykota
218 
219  This will print the quota status for all users on all printers.
[1041]220 
[1156]221  $ repykota --printer "laser*,*pson" jerome "jo*"
[1041]222 
223  This will print the quota status for user jerome and all users
[1156]224  whose name begins with "jo" on all printers which name begins
225  with "laser" or ends with "pson".
[1041]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.
[731]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."""
[1041]249    def main(self, ugnames, options) :
[731]250        """Print Quota reports generator."""
[1041]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
[1071]260            username = pwd.getpwuid(uid)[0]
[1041]261            if options["groups"] :
[1071]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 = [ ]
[1041]267            else :
[1071]268                ugnames = [ username ]
[1041]269       
[900]270        printers = self.storage.getMatchingPrinters(options["printer"])
271        if not printers :
[772]272            raise PyKotaToolError, _("There's no printer matching %s") % options["printer"]
[1041]273           
[1048]274        self.reportingtool = reporter.openReporter(self, "text", printers, ugnames, (options["groups"] and 1) or 0)   
275        print self.reportingtool.generateReport()
[731]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
[1048]286        reportTool = RePyKota(doc=__doc__)
[731]287       
288        # parse and checks the command line
[1048]289        (options, args) = reportTool.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
[731]290       
291        # sets long options
292        options["help"] = options["h"] or options["help"]
293        options["version"] = options["v"] or options["version"]
[895]294        options["users"] = options["u"] or options["users"]
295        options["groups"] = options["g"] or options["groups"]
[731]296        options["printer"] = options["P"] or options["printer"] or defaults["printer"]
297       
298        if options["help"] :
[1048]299            reportTool.display_usage_and_quit()
[731]300        elif options["version"] :
[1048]301            reportTool.display_version_and_quit()
[731]302        elif options["users"] and options["groups"] :   
[842]303            raise PyKotaToolError, _("incompatible options, see help.")
[731]304        else :
[1113]305            retcode = reportTool.main(args, options)
[1048]306    except (PyKotaToolError, PyKotaConfigError, PyKotaStorageError, PyKotaReporterError), msg :           
[731]307        sys.stderr.write("%s\n" % msg)
308        sys.stderr.flush()
[1113]309        retcode = -1 
[731]310
[1113]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.