root / pykota / trunk / bin / edpykota @ 794

Revision 791, 12.8 kB (checked in by jalet, 21 years ago)

Now repykota should output the recorded total page number for each printer too.

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