root / pykota / trunk / bin / edpykota @ 855

Revision 848, 14.8 kB (checked in by jalet, 22 years ago)

Bad english

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