root / pykota / trunk / bin / repykota @ 1144

Revision 1144, 10.0 kB (checked in by jalet, 21 years ago)

Character encoding added to please latest version of Python

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