root / pykota / trunk / bin / edpykota @ 873

Revision 873, 15.3 kB (checked in by jalet, 21 years ago)

GPL paragraphs were incorrectly (from memory) copied into the sources.
Two README files were added.
Upgrade script for PostgreSQL pre 1.01 schema was added.

  • 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 Editor
4#
5# PyKota - Print Quotas for CUPS
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.28  2003/03/29 13:45:26  jalet
26# GPL paragraphs were incorrectly (from memory) copied into the sources.
27# Two README files were added.
28# Upgrade script for PostgreSQL pre 1.01 schema was added.
29#
30# Revision 1.27  2003/03/10 00:23:04  jalet
31# Bad english
32#
33# Revision 1.26  2003/03/10 00:11:27  jalet
34# Cleaner example.
35#
36# Revision 1.25  2003/03/09 23:56:21  jalet
37# Option noquota added to do accounting only.
38#
39# Revision 1.24  2003/02/27 23:48:41  jalet
40# Correctly maps PyKota's log levels to syslog log levels
41#
42# Revision 1.23  2003/02/27 22:55:20  jalet
43# WARN log priority doesn't exist.
44#
45# Revision 1.22  2003/02/27 09:37:02  jalet
46# Wildcards seem to work now
47#
48# Revision 1.21  2003/02/27 09:04:46  jalet
49# user and group names can be passed as wildcards if the --add option
50# is not set. The default is to act on all users or groups.
51#
52# Revision 1.20  2003/02/10 12:07:30  jalet
53# Now repykota should output the recorded total page number for each printer too.
54#
55# Revision 1.19  2003/02/09 13:40:29  jalet
56# typo
57#
58# Revision 1.18  2003/02/09 12:56:53  jalet
59# Internationalization begins...
60#
61# Revision 1.17  2003/02/08 22:47:23  jalet
62# Option --reset can now be used without having to use soft and hard limits
63# on the command line.
64#
65# Revision 1.16  2003/02/08 22:39:46  jalet
66# --reset command line option added
67#
68# Revision 1.15  2003/02/08 22:20:01  jalet
69# Clarification on why we don't check with /etc/passwd to see if the user
70# name is valid or not.
71#
72# Revision 1.14  2003/02/08 22:18:15  jalet
73# Now checks user and group names for validity before adding them
74#
75# Revision 1.13  2003/02/08 22:09:02  jalet
76# Only printer was added the first time.
77#
78# Revision 1.12  2003/02/08 21:44:49  jalet
79# Python 2.1 string module doesn't define ascii_letters
80#
81# Revision 1.11  2003/02/08 09:42:44  jalet
82# Better handle wrong or bad command line arguments
83#
84# Revision 1.10  2003/02/08 09:39:20  jalet
85# typos
86#
87# Revision 1.9  2003/02/08 09:38:06  jalet
88# Badly placed test
89#
90# Revision 1.8  2003/02/07 22:53:57  jalet
91# Checks if printer name is valid before adding it
92#
93# Revision 1.7  2003/02/07 22:17:58  jalet
94# Incomplete test
95#
96# Revision 1.6  2003/02/07 22:13:13  jalet
97# Perhaps edpykota is now able to add printers !!! Oh, stupid me !
98#
99# Revision 1.5  2003/02/06 14:49:04  jalet
100# edpykota should be ok now
101#
102# Revision 1.4  2003/02/06 14:28:59  jalet
103# edpykota should be ok, minus some typos
104#
105# Revision 1.3  2003/02/06 10:47:21  jalet
106# Documentation string and command line options didn't match.
107#
108# Revision 1.2  2003/02/06 10:39:23  jalet
109# Preliminary edpykota work.
110#
111# Revision 1.1  2003/02/05 21:41:09  jalet
112# Skeletons added for all command line tools
113#
114#
115#
116
117import sys
118
119from pykota import version
120from pykota.tool import PyKotaTool, PyKotaToolError
121
122__doc__ = """edpykota v%s (C) 2003 C@LL - Conseil Internet & Logiciels Libres
123A Print Quota editor for PyKota.
124
125command line usage :
126
127  edpykota [options] user1 user2 ... userN
128  edpykota [options] group1 group2 ... groupN
129
130options :
131
132  -v | --version       Prints edpykota's version number then exits.
133  -h | --help          Prints this message then exits.
134 
135  -a | --add           Adds users and/or printers if they don't
136                       exist on the Quota Storage Server.
137                       
138  -u | --users         Edit users print quotas, this is the default.
139 
140  -P | --printer p     Edit quotas on printer p only. Actually p can
141                       use wildcards characters to select only
142                       some printers. The default value is *, meaning
143                       all printers.
144 
145  -g | --groups        Edit groups print quotas instead of users.
146                         
147  -p | --prototype u|g Uses user u or group g as a prototype to set
148                       print quotas
149                       
150  -n | --noquota       Doesn't set a quota but only does accounting.
151 
152  -r | --reset         Resets the printed page counter for the user
153                       or group to zero. The life time page counter
154                       is kept unchanged.
155                       
156  -S | --softlimit sl  Sets the quota soft limit to sl pages.                       
157 
158  -H | --hardlimit hl  Sets the quota hard limit to hl pages.
159 
160  user1 through userN and group1 through groupN can use wildcards
161  if the --add option is not set.
162 
163examples :                             
164
165  $ edpykota -p jerome john paul george ringo
166 
167  This will set print quotas for the users john, paul, george and ringo
168  to the same values than user jerome. User jerome must exist.
169 
170  $ edpykota --printer lp -S 50 -H 60 jerome
171 
172  This will set jerome's print quota on the lp printer to a soft limit
173  of 50 pages, and a hard limit of 60 pages. If either user jerome or
174  printer lp doesn't exist on the Quota Storage Server then nothing is done.
175
176  $ edpykota --add --printer lp -S 50 -H 60 jerome
177 
178  Same as above, but if either user jerome or printer lp doesn't exist
179  on the Quota Storage Server they are automatically added.
180  WARNING : the CUPS PPD file for this printer must still be modified
181            manually, as well as pykota's configuration file for a
182            new printer to be managed successfully.
183           
184  $ edpykota -g -S 500 -H 550 financial support           
185 
186  This will set print quota soft limit to 500 pages and hard limit
187  to 550 pages for groups financial and support on all printers.
188 
189  $ edpykota --reset jerome "jo*"
190 
191  This will reset jerome's page counter to zero on all printers, as
192  well as every user whose name begins with 'jo'.
193  Their life time page counter on each printer will be kept unchanged.
194 
195  $ edpykota --printer hpcolor --noquota jerome
196 
197  This will tell PyKota to not limit jerome when printing on the
198  hpcolor printer. All his jobs will be allowed on this printer, but
199  accounting of the pages he prints will still be kept.
200  Print Quotas for jerome on other printers are unchanged.
201
202This program is free software; you can redistribute it and/or modify
203it under the terms of the GNU General Public License as published by
204the Free Software Foundation; either version 2 of the License, or
205(at your option) any later version.
206
207This program is distributed in the hope that it will be useful,
208but WITHOUT ANY WARRANTY; without even the implied warranty of
209MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
210GNU General Public License for more details.
211
212You should have received a copy of the GNU General Public License
213along with this program; if not, write to the Free Software
214Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
215
216Please e-mail bugs to: %s""" % (version.__version__, version.__author__)
217       
218class EdPyKota(PyKotaTool) :       
219    """A class for edpykota."""
220    def main(self, names, options) :
221        """Edit user or group quotas."""
222        printernames = self.storage.getMatchingPrinters(options["printer"])
223        if not printernames :
224            pname = options["printer"]
225            if options["add"] and pname :
226                if self.isValidName(pname) :
227                    self.storage.addPrinter(pname)
228                    printernames = [ (pname, 0) ]
229                else :   
230                    raise PyKotaToolError, _("Invalid printer name %s") % pname
231            else :
232                raise PyKotaToolError, _("There's no printer matching %s") % pname
233        softlimit = hardlimit = None   
234        if options["softlimit"] :
235            try :
236                softlimit = int(options["softlimit"].strip())
237            except ValueError :   
238                raise PyKotaToolError, _("Invalid softlimit value %s.") % options["softlimit"]
239        if options["hardlimit"] :
240            try :
241                hardlimit = int(options["hardlimit"].strip())
242            except ValueError :   
243                raise PyKotaToolError, _("Invalid hardlimit value %s.") % options["hardlimit"]
244        if (softlimit is not None) and (hardlimit is not None) and (hardlimit < softlimit) :       
245            # error, exchange them
246            self.logger.log_message(_("Hard limit %i is less than soft limit %i, values will be exchanged.") % (hardlimit, softlimit))
247            (softlimit, hardlimit) = (hardlimit, softlimit)
248        if not names :   
249            if options["add"] :
250                raise PyKotaToolError, _("You have to pass user names on the command line")
251            else :   
252                names = [ "*" ] # all users
253        for (printer, printerpagecounter) in printernames :
254            if options["prototype"] :
255                if options["users"] :
256                    prototype = self.storage.getUserPQuota(options["prototype"], printer)
257                else :     
258                    prototype = self.storage.getGroupPQuota(options["prototype"], printer)
259                if prototype is None :
260                    self.logger.log_message(_("Prototype %s not found in Quota Storage for printer %s.") % (options["prototype"], printer))
261                    continue    # skip this printer
262                else :   
263                    (softlimit, hardlimit) = (prototype["softlimit"], prototype["hardlimit"])
264            if hardlimit is None :   
265                hardlimit = softlimit
266                self.logger.log_message(_("Undefined hard limit set to soft limit (%s) on printer %s.") % (str(hardlimit), printer))
267            if softlimit is None :   
268                softlimit = hardlimit
269                self.logger.log_message(_("Undefined soft limit set to hard limit (%s) on printer %s.") % (str(softlimit), printer))
270            if (not options["reset"] and not options["noquota"]) and ((hardlimit is None) or (softlimit is None)) :
271                raise PyKotaToolError, _("Both hard and soft limits must be set ! Aborting.")
272            if options["add"] :   
273                allnames = names
274            else :   
275                if options["users"] :   
276                    allnames = self.storage.getPrinterUsers(printer)
277                else :   
278                    allnames = self.storage.getPrinterGroups(printer)
279            for name in [n for n in allnames if self.matchString(n, names)]:
280                if options["users"] :
281                    quota = self.storage.getUserPQuota(name, printer)
282                else :
283                    quota = self.storage.getGroupPQuota(name, printer)
284                if quota is None :
285                    # not found
286                    if options["add"] :
287                        # In case we want to add something, it is crucial
288                        # that we DON'T check with the system accounts files
289                        # like /etc/passwd because users may be defined
290                        # only remotely
291                        if options["users"] :
292                            if self.isValidName(name) :
293                                self.storage.addUserPQuota(name, printer)
294                                quota = self.storage.getUserPQuota(name, printer)
295                            else :   
296                                self.logger.log_message(_("Invalid user name %s") % name)
297                        else :
298                            if self.isValidName(name) :
299                                self.storage.addGroupPQuota(name, printer)
300                                quota = self.storage.getGroupPQuota(name, printer)
301                            else :   
302                                self.logger.log_message(_("Invalid group name %s") % name)
303                if quota is None :     
304                    self.logger.log_message(_("Quota not found for object %s on printer %s.") % (name, printer))
305                else :   
306                    if options["users"] :
307                        if options["noquota"] or ((softlimit is not None) and (hardlimit is not None)) :
308                            self.storage.setUserPQuota(name, printer, softlimit, hardlimit)
309                        if options["reset"] :
310                            self.storage.resetUserPQuota(name, printer)
311                    else :
312                        if options["noquota"] or ((softlimit is not None) and (hardlimit is not None)) :
313                            self.storage.setGroupPQuota(name, printer, softlimit, hardlimit)
314                        if options["reset"] :
315                            self.storage.resetGroupPQuota(name, printer)
316                     
317if __name__ == "__main__" : 
318    try :
319        defaults = { \
320                     "users"  : 1, \
321                     "groups" : 0, \
322                     "printer" : "*", \
323                   }
324        short_options = "vhnaugrp:P:S:H:"
325        long_options = ["help", "version", "noquota", "add", "users", "groups", "reset", "prototype=", "printer=", "softlimit=", "hardlimit="]
326       
327        # Initializes the command line tool
328        editor = EdPyKota(doc=__doc__)
329       
330        # parse and checks the command line
331        (options, args) = editor.parseCommandline(sys.argv[1:], short_options, long_options)
332       
333        # sets long options
334        options["help"] = options["h"] or options["help"]
335        options["version"] = options["v"] or options["version"]
336        options["add"] = options["a"] or options["add"]
337        options["users"] = options["u"] or options["users"] or defaults["users"]
338        options["groups"] = options["g"] or options["groups"] or defaults["groups"]
339        options["prototype"] = options["p"] or options["prototype"]
340        options["printer"] = options["P"] or options["printer"] or defaults["printer"]
341        options["softlimit"] = options["S"] or options["softlimit"]
342        options["hardlimit"] = options["H"] or options["hardlimit"] 
343        options["reset"] = options["r"] or options["reset"] 
344        options["noquota"] = options["n"] or options["noquota"]
345       
346        if options["help"] :
347            editor.display_usage_and_quit()
348        elif options["version"] :
349            editor.display_version_and_quit()
350        elif options["users"] and options["groups"] :   
351            raise PyKotaToolError, _("incompatible options, see help.")
352        elif (options["softlimit"] or options["hardlimit"]) and options["prototype"] :   
353            raise PyKotaToolError, _("incompatible options, see help.")
354        elif options["noquota"] and (options["prototype"] or options["hardlimit"] or options["softlimit"]) :
355            raise PyKotaToolError, _("incompatible options, see help.")
356        elif options["groups"] :   
357            raise PyKotaToolError, _("option --groups is currently not implemented.")
358        else :
359            sys.exit(editor.main(args, options))
360    except PyKotaToolError, msg :           
361        sys.stderr.write("%s\n" % msg)
362        sys.stderr.flush()
363        sys.exit(-1)
Note: See TracBrowser for help on using the browser.