root / pykota / trunk / bin / edpykota @ 769

Revision 769, 12.5 kB (checked in by jalet, 21 years ago)

Option --reset can now be used without having to use soft and hard limits
on the command line.

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