root / pykota / trunk / bin / edpykota @ 824

Revision 824, 13.9 kB (checked in by jalet, 22 years ago)

WARN log priority doesn't exist.

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