root / pykota / trunk / bin / edpykota @ 768

Revision 768, 12.0 kB (checked in by jalet, 21 years ago)

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